-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-08-functions.qmd
More file actions
129 lines (90 loc) · 2.58 KB
/
Copy pathpython-08-functions.qmd
File metadata and controls
129 lines (90 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
---
title: "Writing functions"
---
* functions encapsulate complexity so that we can treat it as a single thing
* functions enable re-use: write one time, use many times
First define:
```py
def greeting():
print('Hello!')
```
and then we can run it:
```py
greeting()
```
```py
def printDate(year, month, day):
joined = str(year) + '/' + str(month) + '/' + str(day)
print(joined)
printDate(1871, 3, 19)
```
Every function returns something, even if it's None.
```py
a = printDate(1871, 3, 19)
print(a)
```
How do we actually return a value from a function?
```py
def average(values): # the argument is a list
if len(values) == 0:
return None
return sum(values) / len(values)
print('average of actual values:', average([1, 3, 4]))
```
Here is an example of a more complex calendar function returning an alphabetical day of the week:
```sh
def dayOfTheWeek(year, month, day):
import datetime
week = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
return week[datetime.datetime(year, month, day).weekday()]
dayOfTheWeek(2022, 11, 10) # 'Thu'
```
::: {.callout-caution collapse="true"}
## Exercise 8.1
Write a function to convert from Fahrenheit to Celsius, e.g. typing `celsius(77)` would produce 25.
:::
<!-- ```sh -->
<!-- def celsius(f): -->
<!-- return (f-32)*5/9 -->
<!-- ``` -->
::: {.callout-caution collapse="true"}
## Exercise 8.2
Write a function to convert from Celsius to Fahrenheit. Test it with celcius(), e.g. by converting Fahrenheit → Celsius
→ Fahrenheit, or Celsius → Fahrenheit → Celsius.
:::
::: {.callout-caution collapse="true"}
## Exercise 8.3
Now modify celsius() to take a list of Fahrenheit temperatures, e.g., `celcius([70,80,90,100])`, to return a list of
Celsius temperatures.
:::
<!-- ```py -->
<!-- def celsius(fs): -->
<!-- c = [] -->
<!-- for f in fs: -->
<!-- c.append((f-32.)*5./9.) -->
<!-- return c -->
<!-- ``` -->
::: {.callout-caution collapse="true"}
## Exercise 8.4
Write a function that takes two lists and returns True if they have at least one common member.
:::
Function arguments in Python can take default values becoming optional:
```py
def addNumber(a, b=1):
return a+b
print(addNumber(5))
print(addNumber(5,3))
```
With several optional arguments it is important to be able to differentiate them:
```py
def modify(a, b=1, coef=1):
return a*coef + b
print(modify(10))
print(modify(10, 1)) # which argument did we add?
print(modify(10, coef=2))
print(modify(10, coef=2, b=5))
```
Any complex python function will have many optional arguments, for example:
```py
?print
```