05_Functions
05_Functions
05_Functions
Session 5
Functions
2
Defining Basic Functions
my_function(8, 4)
my_function(13, 2) 3
We use functions for…
4
Document your functions using docstrings
Arguments return
Function
6
Receiving and returning info
7
Programming Challenges
def sum_many_args(*args):
print(type(args))
return (sum(args))
result = sum_many_args(1, 2, 3, 4, 5)
result
* a tuple is a Python data type similar to a vector or list 9
Encapsulation of function variables
11
Common errors
12
Common errors
13
Programming Challenges
Programming challenge FUN.4
Write a Python function to check whether a number is in a given range
[a,b].
Programming challenge FUN.5
Write a Python function that takes a number as a parameter and check
the number is prime or not.
15
Programming Challenges: extra exercises
Create a function that receives the name of travelling departure city and
destination city from a user and sends back the price of estimated
flight cost during this year.
- Departure city can only be selected from Madrid, Roma and New York.
- Destination city can only be chosen from Beijing and Melbourne.
Comments:
- If you depart from Europe, the basic cost would start from 300 €
- If you depart from America, the basic cost would start from 400 €
- If your destination is in Asia, the cost will add 50 € more on basic cost
- If your destination is in Australia, the cost will add 100 € more on
basic cost
16
Programming Challenges: extra exercises
The user gets deducted 10 point if they party a lot or smoke and gains
10 point if they exercise.
Each point is worth a year. The initial count for every user in this country
is 90 years.
Hint: Think about the different number of points the user could get.
Take it further: Make sure the users inputs “yes” or “no”
17
Session Wrap-up
18
Glossary
def
return
map
math.sqrt
19