= [11.25, 18.0, 20.0, 10.75, 10.75, 9.5]
areas print(max(areas))
print(len(areas))
print(round(10.75,1))
print(areas.index(18.0))
20.0
6
10.8
1
In this lesson we will get to know and become experts in:
Functions are essential building blocks to reuse code and to modularize code.
We have already seen and used many built-in functions/methods such as print()
, len()
, max()
, round()
, index()
, capitalize()
, etc..
= [11.25, 18.0, 20.0, 10.75, 10.75, 9.5]
areas print(max(areas))
print(len(areas))
print(round(10.75,1))
print(areas.index(18.0))
20.0
6
10.8
1
But of course we want to define our own functions as well ! As a rule of thumb, if you anticipate needing to repeat the same or very similar code more than once, it may be worth writing a reusable function. Functions can also help make your code more readable by giving a name to a group of Python statements.
For example, we computed the BMI previously as follows:
= 1.79
height = 68.7
weight = weight/height**2
bmi print(bmi)
21.44127836209856
Functions are declared with the def
keyword. A function contains a block of code with an optional use of the return
keyword:
def compute_bmi(height, weight):
return weight/height**2
1.79, 68.7) compute_bmi(
21.44127836209856
Each function can have positional arguments and keyword arguments. Keyword arguments are most commonly used to specify default values or optional arguments. For example:
def compute_bmi(height, weight, ndigits=2):
return round(weight/height**2, ndigits)
print(compute_bmi(1.79, 68.7))
print(compute_bmi(1.79, 68.7,4))
21.44
21.4413
are easily possible in python:
def compute_bmi(height, weight, ndigits=2):
= round(weight/height**2, ndigits)
bmi #https://www.cdc.gov/healthyweight/assessing/index.html#:~:text=If%20your%20BMI%20is%20less,falls%20within%20the%20obese%20range.
if bmi < 18.5:
="underweight"
statuselif bmi <= 24.9:
="healthy"
statuselif bmi <= 29.9:
="underweight"
statuselif bmi >= 30:#note that a simple else would suffice here!
="obese"
statusreturn bmi, status
print(compute_bmi(1.79, 68.7))
print(compute_bmi(1.79, 55))
(21.44, 'healthy')
(17.17, 'underweight')
Recall from the previous lab how we
#find the maximum area:
= [11.25, 18.0, 20.0, 10.75, 10.75, 9.5]
areas = areas[0] # initialize to the first area seen
currentMax
for a in areas:
if a > currentMax:
= a
currentMax
print("The max is:", currentMax)
The max is: 20.0
#Clever IDB students: Compute the sum from 1 to 100:
=0
Total
for i in range(101):#strictly speaking we are adding the first 0
= Total + i
Total #Total += i
print(Total)
Write your own function
\[ \sum_{i=m}^{n}{i} \]
Functions seem straightforward. But one of the more confusing aspects in the beginning is the concept that we can have multiple instances of the same variable!
Functions can access variables created inside the function as well as those outside the function in higher (or even global) scopes. An alternative and more descriptive name describing a variable scope in Python is a namespace. Any variables that are assigned within a function by default are assigned to the local namespace. The local namespace is created when the function is called and is immediately populated by the function’s arguments. After the function is finished, the local namespace is destroyed.
Examples:
= 1.79
height = 68.7
weight = weight/height**2
bmi #print("height, weight, bmi OUTSIDE the function:",height, weight,bmi)
def compute_bmi(h, w):
= h
height = w
weight = round(weight/height**2,2)
bmi ="healthy"
statusprint("height, weight, bmi INSIDE the function:",height, weight,bmi)
print("status:", status)
return bmi
1.55, 50)
compute_bmi(
print("height, weight, bmi OUTSIDE the function:",height, weight,bmi)
#print(status)
height, weight, bmi INSIDE the function: 1.55 50 20.81
status: healthy
height, weight, bmi OUTSIDE the function: 1.79 68.7 21.44127836209856
A dictionary is basically a lookup table. It stores a collection of key-value pairs, where key and value are Python objects. Each key is associated with a value so that a value can be conveniently retrieved, inserted, modified, or deleted given a particular key.
The dictionary or dict
may be the most important built-in Python data structure. In other programming languages, dictionaries are sometimes called hash maps or associative arrays.
#This was the house defined as a list of lists:
= [['hallway', 11.25],
house 'kitchen', 18.0],
['living room', 20.0],
['bedroom', 10.75],
['bathroom', 9.5]]
[
#Remember all the disadvantages of accessing elements
#Better as a lookup table:
= {'hallway': 11.25,
house 'kitchen': 18.0,
'living room': 20.0,
'bedroom': 10.75,
'bathroom': 9.5}
= {'spain':'madrid', 'france' : 'paris'}
europe print(europe["spain"])
print("france" in europe)
print("paris" in europe)#only checks the keys!
"germany"] = "berlin"
europe[print(europe.keys())
print(europe.values())
madrid
True
False
dict_keys(['spain', 'france', 'germany'])
dict_values(['madrid', 'paris', 'berlin'])
How would we convert two lists into a key: value pair dictionary?
Method 1: using zip
=['hallway', 'kitchen', 'living room', 'bedroom', 'bathroom']
rooms=[11.25, 18.0, 20.0, 10.75, 9.5]
areas#create list of tuples
list(zip(rooms, areas))
[('hallway', 11.25),
('kitchen', 18.0),
('living room', 20.0),
('bedroom', 10.75),
('bathroom', 9.5)]
dict(zip(rooms, areas))
{'hallway': 11.25,
'kitchen': 18.0,
'living room': 20.0,
'bedroom': 10.75,
'bathroom': 9.5}
If you need to iterate over both the keys and values, you can use the items
method to iterate over the keys and values as 2-tuples:
#print(list(europe.items()))
for country, capital in europe.items():
print(capital, "is the capital of", country)
madrid is the capital of spain
paris is the capital of france
berlin is the capital of germany
Note: You can use integers as keys as well. However -unlike in lists- one should not think of them as positional indices!
#Assume you have a basement:
0] = 21.5
house[ house
{'hallway': 11.25,
'kitchen': 18.0,
'living room': 20.0,
'bedroom': 10.75,
'bathroom': 9.5,
0: 21.5}
#And there is a difference between the string and the integer index!
"0"] = 30.5
house[ house
{'hallway': 11.25,
'kitchen': 18.0,
'living room': 20.0,
'bedroom': 10.75,
'bathroom': 9.5,
0: 21.5}
Categorize a list of words by their first letters as a dictionary of lists:
= ["apple", "bat", "bar", "atom", "book"]
words
= {}
by_letter
for word in words:
= word[0]
letter if letter not in by_letter:
= [word]
by_letter[letter] else:
by_letter[letter].append(word)
{'a': ['apple', 'atom'], 'b': ['bat', 'bar', 'book']}
NumPy, short for Numerical Python, is one of the most important foundational packages for numerical computing in Python.
= [1.79, 1.85, 1.95, 1.55]
height = [70, 80, 85, 65]
weight
#bmi = weight/height**2
import numpy as np
= np.array([1.79, 1.85, 1.95, 1.55])
height = np.array([70, 80, 85, 65])
weight
= weight/height**2
bmi round(bmi,2) np.
array([21.84700852, 23.37472608, 22.35371466, 27.05515088])