Learn practical skills, build real-world projects, and advance your career

Writing Reusable Code Using Functions in Python

Part 4 of "A Gentle Introduction to Programming with Python"

This tutorial is the fourth in a series on introduction to programming using the Python language. These tutorials take a practical coding-based approach, and the best way to learn the material is to execute the code and experiment with the examples. Check out the full series here:

  1. First Steps with Python and Jupyter
  2. A Quick Tour of Variables and Data Types
  3. Branching using Conditional Statements and Loops
  4. Writing Reusable Code Using Functions
  5. Reading from and Writing to Files
  6. Object Oriented Programming with Classes

How to run the code

This tutorial hosted on Jovian.ml, a platform for sharing data science projects online. You can "run" this tutorial and experiment with the code examples in a couple of ways: using free online resources (recommended) or on your own computer.

This tutorial is a Jupyter notebook - a document made of "cells", which can contain explanations in text or code written in Python. Code cells can be executed and their outputs e.g. numbers, messages, graphs, tables, files etc. can be viewed within the notebook, which makes it a really powerful platform for experimentation and analysis. Don't afraid to experiment with the code & break things - you'll learn a lot by encoutering and fixing errors. You can use the "Kernel > Restart & Clear Output" menu option to clear all outputs and start again from the top of the notebook.

Option 1: Running using free online resources (1-click, recommended)

The easiest way to start executing this notebook is to click the "Run" button at the top of this page, and select "Run on Binder". This will run the notebook on mybinder.org, a free online service for running Jupyter notebooks. You can also select "Run on Colab" or "Run on Kaggle", but you'll need to create an account on Google Colab or Kaggle to use these platforms.

Option 2: Running on your computer locally

You'll need to install Python and download this notebook on your computer to run in locally. We recommend using the Conda distribution of Python. Here's what you need to do to get started:

  1. Install Conda by following these instructions. Make sure to add Conda binaries to your system PATH to be able to run the conda command line tool from your Mac/Linux terminal or Windows command prompt.

  2. Create and activate a Conda virtual environment called intro-to-python which you can use for this tutorial series:

conda create -n intro-to-python -y python=3.8 
conda activate intro-to-python

You'll need to create the environment only once, but you'll have to activate it every time want to run the notebook. When the environment is activated, you should be able to see a prefix (intro-to-python) within your terminal or command prompt.

  1. Install the required Python libraries within the environmebt by the running the following command on your terminal or command prompt:
pip install jovian jupyter numpy pandas matplotlib seaborn --upgrade
  1. Download the notebook for this tutorial using the jovian clone command:
jovian clone aakashns/python-functions-and-scope

The notebook is downloaded to the directory python-functions-and-scope.

  1. Enter the project directory and start the Jupyter notebook:
cd python-functions-and-scope
jupyter notebook
  1. You can now access Jupyter's web interface by clicking the link that shows up on the terminal or by visiting http://localhost:8888 on your browser. Click on the notebook python-functions-and-scope.ipynb to open it and run the code. If you want to type out the code yourself, you can also create a new notebook using the "New" button.

Creating and using functions

A function is a reusable set of instructions. A function takes one or more inputs, performs certain operations, and often returns an output. Python provides many in-built functions like print, and also allows you to define your own functions.

pip install jovian jupyter numpy pandas matplotlib seaborn
Requirement already satisfied: jovian in /srv/conda/envs/notebook/lib/python3.7/site-packages (0.2.18) Collecting jupyter Downloading jupyter-1.0.0-py2.py3-none-any.whl (2.7 kB) Collecting numpy Downloading numpy-1.19.1-cp37-cp37m-manylinux2010_x86_64.whl (14.5 MB) |████████████████████████████████| 14.5 MB 26 kB/s eta 0:00:01 Collecting pandas Downloading pandas-1.1.1-cp37-cp37m-manylinux1_x86_64.whl (10.5 MB) |████████████████████████████████| 10.5 MB 32.2 MB/s eta 0:00:01 Collecting matplotlib Downloading matplotlib-3.3.1-cp37-cp37m-manylinux1_x86_64.whl (11.6 MB) |████████████████████████████████| 11.6 MB 27.8 MB/s eta 0:00:01 Collecting seaborn Downloading seaborn-0.10.1-py3-none-any.whl (215 kB) |████████████████████████████████| 215 kB 47.9 MB/s eta 0:00:01 Requirement already satisfied: click in /srv/conda/envs/notebook/lib/python3.7/site-packages (from jovian) (7.1.2) Requirement already satisfied: pyyaml in /srv/conda/envs/notebook/lib/python3.7/site-packages (from jovian) (5.3.1) Requirement already satisfied: uuid in /srv/conda/envs/notebook/lib/python3.7/site-packages (from jovian) (1.30) Requirement already satisfied: requests in /srv/conda/envs/notebook/lib/python3.7/site-packages (from jovian) (2.24.0) Collecting qtconsole Downloading qtconsole-4.7.7-py2.py3-none-any.whl (118 kB) |████████████████████████████████| 118 kB 36.5 MB/s eta 0:00:01 Requirement already satisfied: ipywidgets in /srv/conda/envs/notebook/lib/python3.7/site-packages (from jupyter) (7.5.1) Collecting jupyter-console Downloading jupyter_console-6.2.0-py3-none-any.whl (22 kB) Requirement already satisfied: nbconvert in /srv/conda/envs/notebook/lib/python3.7/site-packages (from jupyter) (5.6.1) Requirement already satisfied: ipykernel in /srv/conda/envs/notebook/lib/python3.7/site-packages (from jupyter) (5.3.4) Requirement already satisfied: notebook in /srv/conda/envs/notebook/lib/python3.7/site-packages (from jupyter) (6.0.3) Collecting pytz>=2017.2 Downloading pytz-2020.1-py2.py3-none-any.whl (510 kB) |████████████████████████████████| 510 kB 34.0 MB/s eta 0:00:01 Requirement already satisfied: python-dateutil>=2.7.3 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from pandas) (2.8.1) Collecting cycler>=0.10 Downloading cycler-0.10.0-py2.py3-none-any.whl (6.5 kB) Collecting pillow>=6.2.0 Downloading Pillow-7.2.0-cp37-cp37m-manylinux1_x86_64.whl (2.2 MB) |████████████████████████████████| 2.2 MB 14.1 MB/s eta 0:00:01 Requirement already satisfied: certifi>=2020.06.20 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from matplotlib) (2020.6.20) Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from matplotlib) (2.4.7) Collecting kiwisolver>=1.0.1 Downloading kiwisolver-1.2.0-cp37-cp37m-manylinux1_x86_64.whl (88 kB) |████████████████████████████████| 88 kB 8.1 MB/s eta 0:00:01 Collecting scipy>=1.0.1 Downloading scipy-1.5.2-cp37-cp37m-manylinux1_x86_64.whl (25.9 MB) |████████████████████████████████| 25.9 MB 1.8 MB/s eta 0:00:011 |████▌ | 3.7 MB 35.0 MB/s eta 0:00:01 Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from requests->jovian) (1.25.10) Requirement already satisfied: chardet<4,>=3.0.2 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from requests->jovian) (3.0.4) Requirement already satisfied: idna<3,>=2.5 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from requests->jovian) (2.10) Requirement already satisfied: jupyter-client>=4.1 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from qtconsole->jupyter) (6.1.6) Requirement already satisfied: traitlets in /srv/conda/envs/notebook/lib/python3.7/site-packages (from qtconsole->jupyter) (4.3.3) Requirement already satisfied: ipython-genutils in /srv/conda/envs/notebook/lib/python3.7/site-packages (from qtconsole->jupyter) (0.2.0) Requirement already satisfied: pyzmq>=17.1 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from qtconsole->jupyter) (19.0.1) Requirement already satisfied: jupyter-core in /srv/conda/envs/notebook/lib/python3.7/site-packages (from qtconsole->jupyter) (4.6.3) Collecting qtpy Downloading QtPy-1.9.0-py2.py3-none-any.whl (54 kB) |████████████████████████████████| 54 kB 3.6 MB/s eta 0:00:01 Requirement already satisfied: pygments in /srv/conda/envs/notebook/lib/python3.7/site-packages (from qtconsole->jupyter) (2.6.1) Requirement already satisfied: ipython>=4.0.0; python_version >= "3.3" in /srv/conda/envs/notebook/lib/python3.7/site-packages (from ipywidgets->jupyter) (7.16.1) Requirement already satisfied: widgetsnbextension~=3.5.0 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from ipywidgets->jupyter) (3.5.1) Requirement already satisfied: nbformat>=4.2.0 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from ipywidgets->jupyter) (5.0.7) Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from jupyter-console->jupyter) (3.0.5) Requirement already satisfied: mistune<2,>=0.8.1 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from nbconvert->jupyter) (0.8.4) Requirement already satisfied: pandocfilters>=1.4.1 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from nbconvert->jupyter) (1.4.2) Requirement already satisfied: bleach in /srv/conda/envs/notebook/lib/python3.7/site-packages (from nbconvert->jupyter) (3.1.5) Requirement already satisfied: entrypoints>=0.2.2 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from nbconvert->jupyter) (0.3) Requirement already satisfied: jinja2>=2.4 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from nbconvert->jupyter) (2.11.2) Requirement already satisfied: testpath in /srv/conda/envs/notebook/lib/python3.7/site-packages (from nbconvert->jupyter) (0.4.4) Requirement already satisfied: defusedxml in /srv/conda/envs/notebook/lib/python3.7/site-packages (from nbconvert->jupyter) (0.6.0) Requirement already satisfied: tornado>=4.2 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from ipykernel->jupyter) (6.0.4) Requirement already satisfied: Send2Trash in /srv/conda/envs/notebook/lib/python3.7/site-packages (from notebook->jupyter) (1.5.0) Requirement already satisfied: prometheus-client in /srv/conda/envs/notebook/lib/python3.7/site-packages (from notebook->jupyter) (0.8.0) Requirement already satisfied: terminado>=0.8.1 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from notebook->jupyter) (0.8.3) Requirement already satisfied: six>=1.5 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from python-dateutil>=2.7.3->pandas) (1.15.0) Requirement already satisfied: decorator in /srv/conda/envs/notebook/lib/python3.7/site-packages (from traitlets->qtconsole->jupyter) (4.4.2) Requirement already satisfied: jedi>=0.10 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from ipython>=4.0.0; python_version >= "3.3"->ipywidgets->jupyter) (0.17.2) Requirement already satisfied: backcall in /srv/conda/envs/notebook/lib/python3.7/site-packages (from ipython>=4.0.0; python_version >= "3.3"->ipywidgets->jupyter) (0.2.0) Requirement already satisfied: pickleshare in /srv/conda/envs/notebook/lib/python3.7/site-packages (from ipython>=4.0.0; python_version >= "3.3"->ipywidgets->jupyter) (0.7.5) Requirement already satisfied: setuptools>=18.5 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from ipython>=4.0.0; python_version >= "3.3"->ipywidgets->jupyter) (49.2.0.post20200712) Requirement already satisfied: pexpect; sys_platform != "win32" in /srv/conda/envs/notebook/lib/python3.7/site-packages (from ipython>=4.0.0; python_version >= "3.3"->ipywidgets->jupyter) (4.8.0) Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from nbformat>=4.2.0->ipywidgets->jupyter) (3.2.0) Requirement already satisfied: wcwidth in /srv/conda/envs/notebook/lib/python3.7/site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->jupyter-console->jupyter) (0.2.5) Requirement already satisfied: packaging in /srv/conda/envs/notebook/lib/python3.7/site-packages (from bleach->nbconvert->jupyter) (20.4) Requirement already satisfied: webencodings in /srv/conda/envs/notebook/lib/python3.7/site-packages (from bleach->nbconvert->jupyter) (0.5.1) Requirement already satisfied: MarkupSafe>=0.23 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from jinja2>=2.4->nbconvert->jupyter) (1.1.1) Requirement already satisfied: parso<0.8.0,>=0.7.0 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from jedi>=0.10->ipython>=4.0.0; python_version >= "3.3"->ipywidgets->jupyter) (0.7.1)
Requirement already satisfied: ptyprocess>=0.5 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from pexpect; sys_platform != "win32"->ipython>=4.0.0; python_version >= "3.3"->ipywidgets->jupyter) (0.6.0) Requirement already satisfied: attrs>=17.4.0 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets->jupyter) (19.3.0) Requirement already satisfied: pyrsistent>=0.14.0 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets->jupyter) (0.16.0) Requirement already satisfied: importlib-metadata; python_version < "3.8" in /srv/conda/envs/notebook/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets->jupyter) (1.7.0) Requirement already satisfied: zipp>=0.5 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from importlib-metadata; python_version < "3.8"->jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets->jupyter) (3.1.0) Installing collected packages: qtpy, qtconsole, jupyter-console, jupyter, numpy, pytz, pandas, cycler, pillow, kiwisolver, matplotlib, scipy, seaborn Successfully installed cycler-0.10.0 jupyter-1.0.0 jupyter-console-6.2.0 kiwisolver-1.2.0 matplotlib-3.3.1 numpy-1.19.1 pandas-1.1.1 pillow-7.2.0 pytz-2020.1 qtconsole-4.7.7 qtpy-1.9.0 scipy-1.5.2 seaborn-0.10.1 Note: you may need to restart the kernel to use updated packages.
today = "Friday"
print("Today is", today)
Today is Friday