Jovian
⭐️
Sign In
In [2]:
# Jovian Commit Essentials
# Please retain and execute this cell without modifying the contents for `jovian.commit` to work
!pip install jovian --upgrade -q
import jovian
jovian.utils.colab.set_colab_file_id('1ku-Zgava7th0rjBS2_t6rm-oSw9gf565')

Writing Reusable Code Using Functions in Python

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

This tutorial is the third 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 zerotopandas 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 us to define our own functions.

You can define a new function using the def keyword.

In [1]:
def say_hello():
    print('Hello there!')
    print('How are you?')

Note the round brackets or parantheses () and colon : after the function's name. Both are essential parts of the syntax for defining a function. The body of the function can contain one or more statements which are to be executed when the function is called. Simlar to conditional statements and loops, the statements must be indented by 4 spaces.

The statements inside a function's body are not executed when a function is defined. To execute the statements, we need to call the function.

In [4]:
say_hello()
Hello there! How are you?

Function Arguments and Return Values

Functions can also accept one or more values as inputs, arguments or parameters. Arguments help us write flexible functions which can perform the same operation on different values.

In [5]:
def say_hello(name):
    print('Hello {}!'.format(name))
    print('How are you?')
In [6]:
say_hello('ahmed')
Hello ahmed! How are you?
In [7]:
say_hello('Belal')
Hello Belal! How are you?

Here's an example of a function with two arguments.

In [8]:
def greet(name, city):
    print('Hello {}!'.format(name))
    print("How's the weather in {}?".format(city))
In [9]:
greet('John', 'San Francisco')
Hello John! How's the weather in San Francisco?
In [10]:
greet('Jane', 'Berlin')
Hello Jane! How's the weather in Berlin?

TODO - This notebook is in progress

In [ ]:
!pip install jovian --upgrade --quiet
In [ ]:
import jovian
In [ ]:
jovian.commit(project='python-functions-and-scope')
[jovian] Attempting to save notebook..
In [ ]: