# This function calculates Body Mass Index
def calculateBodyMassIndex(weight_kg, height_m):
= weight_kg / pow(height_m, 2)
body_mass_index = round(body_mass_index, 2)
rounded_bmi
return rounded_bmi
Flow Control
Functions
A function in python is a group statements that perform a particular task
# lets try
67, 1.6) calculateBodyMassIndex(
26.17
Creating a function
To create a function, we need the following: - The def
keyword - A function name - Round brackets ()
and a colon :
- A function body- a group of statements
def greeter():
= 'Hello'
message print(message)
- To execute a function, it needs to be called
- To call a function, use its function name with parentheses
()
greeter()
Hello
Function Parameters/Arguments
- When calling a function, we can pass data using parameters/arguments
- A parameter is a variable declared in the function. In the example below,
number1
andnumber2
are parameter - The argument is the value passed to the function when its called. In the example below
3
and27
are the arguments
# define the function
def addNumbers(number1, number2):
sum = number1 + number2
print(sum)
# Call the function
3, 27) addNumbers(
30
# setting a default argument
def greet(name='you'):
= 'Hello ' + name
message print(message)
'Tinye')
greet( greet()
Hello Tinye
Hello you
Return Statement
The return
statement is used to return a value to a function caller
def addNumbers(number1, number2):
sum = number1 + number2
return sum
= addNumbers(56, 4)
summation print(summation)
60
### lambda functions - Lambda functions are functions that donot have names - The body of a lambda function can only have one expression, but can have multiple arguments - The result of the expression is automatically returned
Syntax: python lambda parameters: expression
# Example of lambda function
= lambda weight_kg, height_m: round((weight_kg/(height_m ** 2)), 2)
calculateBMI # Calling a labda function
67, 1.7) calculateBMI(
23.18
Practice functions
Calculate CGPA
# Assume 4 course units
# 1. Math - A
# 2. Science - B
# 3. SST - B
# 4. English - C
def calculate_CGPA(GPs_list, CUs_list):
= len(GPs_list)
length = 0
product_sum
for item in range(length):
+= GPs_list[item] * CUs_list[item]
product_sum
= sum(CUs_list)
CUs_sum
= product_sum / CUs_sum
CGPA
return CGPA
# calculate_CGPA(4, 5)
Get someones age given birth month and year
def getAge(month, year):
= 12 - month
month_diff = 2023 - year
year_diff
return str(year_diff) + ' years ' + str(month_diff) + ' months'
= getAge(year=2000, month=10) # keyword argument
age = getAge(10, 2000) # positional argument
age2
print(age)
23 years 2 months
Loops
- Loops are used to repetitively execute a group of statements
- we have 2 types,
for
andwhile
loop
For Loop
A for
loop is used to loop through or iterate over a sequence or iterable objects
Syntax:
for variable in sequence:
statements
= ['cat', 'dog', 'rabbit']
pets # iterate through pets
for pet in pets:
print(pet)
cat
dog
rabbit
# convert all weights in list from kg to pounds
= [145, 100, 76, 80]
weights_kg = []
weights_pds
for weight in weights_kg:
= weight * 2.2
pounds = round(pounds, 2)
rounded_pds
weights_pds.append(rounded_pds)
print(weights_pds)
[319.0, 220.0, 167.2, 176.0]
# Display all letters in a name
= 'Shafara'
name
for letter in name:
print(letter)
S
h
a
f
a
r
a
# print 'Hello you' 5 times
for step in range(0, 5):
print('Hello you')
Hello you
Hello you
Hello you
Hello you
Hello you
While loop
- The
while
loop executes a given group of statements as long as the given expression isTrue
Syntax:
while expression:
statements
= 0
counter
while counter < 5:
print('Hello you')
+= 1 counter
Hello you
Hello you
Hello you
Hello you
Hello you
# Convert the weights in the list from kgs to pounds
= [145, 100, 76, 80]
weights_kg = []
weights_pds
= 0
counter = len(weights_kg)
end
while counter < end:
= weights_kg[counter] * 2.2
pound = round(pound, 3)
rounded_pds
weights_pds.append(rounded_pds)
+= 1
counter
print(weights_pds)
[319.0, 220.0, 167.2, 176.0]