-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides-10-01.qmd
212 lines (162 loc) · 3.84 KB
/
slides-10-01.qmd
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
---
title: "returning tuples (slides)"
format: revealjs
slide-number: true
df-print: kable
---
# CSc 110 - returning tuples
## What are tuples?
- **immutable** lists
- a sequence of zero or more values (just like lists)
- addressable by index (just like lists)
- iterable (just like lists)
## tuple syntax
Use `()` to create a new tuple:
```{python}
#| eval: true
#| echo: true
my_tuple = (2, 4, 6)
my_tuple[0]
```
```{python}
#| eval: true
#| echo: true
my_tuple[2]
```
Empty tuple:
```{python}
#| eval: true
#| echo: true
empty = ()
empty
```
## `tuples` are immutable
No assignment is allowed
```{python}
#| eval: false
#| echo: true
my_tuple = (2, 4, 6)
my_tuple[1] = 99 # will fail and cause an exception
```
## Why use tuples?
* They are a little more efficient than lists
* They can be used as dictionary keys (**important**)
* They are used in Python a lot, so you need to understand them, even if you don't use them
* When functions return multiple values, it returns a tuple
## Returning multiple values in a function
```{python}
#| eval: true
#| echo: true
def descr_stats(numbers):
total = 0
for n in numbers:
total += n
return total, total/len(numbers)
def main():
grades = [90, 87, 83, 95, 100]
print(descr_stats(grades))
total, mean = descr_stats(grades)
assert total == 455
assert mean == 91.0
main()
```
## Write a function
1. Its name is `low_high`
1. It takes two numbers as argument: `x` and `y`
1. It returns a tuple with the lowest number first, the highest number second
```{python}
#| eval: false
#| echo: true
assert low_high(4, 2) == (2, 4)
assert low_high(1, 5) == (1, 5)
```
## Write a function -- solution
```{python}
#| eval: true
#| echo: true
def low_high(x, y):
if x < y:
return x, y
else:
return y, x
def main():
assert low_high(4, 2) == (2, 4)
assert low_high(1, 5) == (1, 5)
print("Passed tests.")
main()
```
## Write a function
1. Its name is `sum_and_multiplication`
1. It takes a `dictionary` as argument
1. It iterates over the dictionary values, summing all the values and multiplying all the values (create two separate variables, one for the addition and another for the multiplication)
1. It returns the sum and the multiplication of the values in the dictionary
```{python}
#| eval: false
#| echo: true
test_dict = {"a": 2, "b": 3, "c": 5}
total, mult = sum_and_multiplication(test_dict)
assert total == 10
assert mult == 30
```
## Write a function -- solution 1
```{python}
#| eval: true
#| echo: true
def sum_and_multiplication(dictionary):
total = 0
multiplication = 1
for k in dictionary:
total += dictionary[k]
multiplication *= dictionary[k]
return total, multiplication
def main():
test_dict = {"a": 2, "b": 3, "c": 5}
total, mult = sum_and_multiplication(test_dict)
assert total == 10
assert mult == 30
main()
```
## Write a function -- solution 2
```{python}
#| eval: true
#| echo: true
def sum_and_multiplication(dictionary):
total = 0
multiplication = 1
for v in dictionary.values():
total += v
multiplication *= v
return total, multiplication
def main():
test_dict = {"a": 2, "b": 3, "c": 5}
total, mult = sum_and_multiplication(test_dict)
assert total == 10
assert mult == 30
main()
```
## Write a function
1. Its name is `odds_and_evens`
1. It takes one list of `integers` as argument
1. It returns two lists: one with all the odd numbers in `integers`, and another with all the even numbers in `integers`
```{python}
#| eval: false
#| echo: true
assert odds_and_evens([2,6,1]) == ([1], [2,6])
```
## Write a function -- solution
```{python}
#| eval: true
#| echo: true
def odds_and_evens(integers):
odds = []
evens = []
for n in integers:
if n % 2 == 0:
evens.append(n)
else:
odds.append(n)
return odds, evens
def main():
assert odds_and_evens([2,6,1]) == ([1], [2,6])
main()
```