-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides-02-01.qmd
267 lines (202 loc) · 5.76 KB
/
slides-02-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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
---
title: "functions (slides)"
format: revealjs
slide-number: true
df-print: kable
---
# CSc 110 - Functions
## Functions
- Functions are named operations that are available to do tasks
- Some functions are *built-in* functions that Python provides
- Programmers can also define their own functions
- Functions are **called** (or **invoked**)
## Function definitions
```{python}
#| echo: true
#| eval: false
def two():
return 2
```
This function definition has many parts:
- `two` is the name of the function
- `()` is the **parameter** list (Here, it is empty)
- the body (or content) of the function is indented
- `return 2` is a statement that causes the function to cease and produce the value 2
## Example of a simple function
```{python}
#| echo: true
#| eval: false
def add_one(n):
return n + 1
```
- `add_one` is the name of the function
- `(n)` is the **parameter** list
- the body (or content) of the function is indented
- `return n + 1` is a statement that causes the function to cease and produce the value n + 1
## Another example
```{python}
#| echo: true
#| eval: true
def cook_food(order):
message = "Your " + order + " is ready!"
return message
meal = cook_food("pasta")
print(meal)
meal = cook_food("burger")
print(meal)
```
- The function `cook_food` prepares a meal.
- Then returns a string that says the meal is ready.
- The returned message is stored in the `meal` variable.
- Finally, `print` function displays that message to the caller.
## Function to calculate area of a circle
Remember this from the last set of slides?
```{python}
#| echo: true
#| eval: false
# assign a radius value
radius = 3
# compute the rounded area of a circle
area = round(3.1415 * radius ** 2, 2)
# print the area
print(area)
```
## Function to calculate area of a circle
Function name is `calculate_area`. Given a `radius` parameter, it returns the rounded `area` of the circle.
```{python}
#| echo: true
#| eval: false
def calculate_area(radius):
area = 3.1415 * radius ** 2
return round(area, 2)
def main():
print(calculate_area(3)) # 28.27
print(calculate_area(6)) # 113.09
main()
```
## Write a function
Write a function that calculates the volume of a sphere:
1. Its name is `sphere_volume`
2. It takes one argument: `radius`
3. It calculates the volume of the sphere (use `3.1415` for $\pi$):
$$
v = {4 / 3} \cdot \pi \cdot radius^3
$$
4. It returns the rounded value for the calculated volume
5. Test case: `sphere_volume(.75)` should return `1.77`.
## Write a function -- sphere volume
```{python}
#| echo: true
#| eval: true
def sphere_volume(radius):
"calculates the volume of a sphere of given radius"
volume = (4 / 3) * 3.1415 * radius**3
return round(volume, 2)
def main():
print(sphere_volume(.75)) # 1.77
print(sphere_volume(2)) # 33.51
print(sphere_volume(5.5)) # 696.89
main()
```
## Write a function
Write a function that calculates the area of a sphere:
1. Its name is `sphere_area`
2. It takes one argument: `radius`
3. It calculates the volume of the sphere (use `3.1415` for $\pi$):
$$
a = 4 \cdot \pi \cdot radius^2
$$
4. It returns the value for the calculated sphere area
5. Test case: `sphere_area(.75)` should return `7.07`.
## Write a function -- solutions 1
```{python}
#| echo: true
#| eval: true
def sphere_area(radius):
"calculates the area of a sphere of given radius"
area = 4 * 3.1415 * radius**2
return round(area, 2)
def sphere_volume(radius):
"calculates the volume of a sphere of given radius"
volume = (4 / 3) * 3.1415 * radius**3
return round(volume, 2)
def main():
r = .75
v = sphere_volume(r)
a = sphere_area(r)
print(v, a)
main()
```
## Write a function
Comparing two formulas:
$$
a = 4 \cdot \pi \cdot radius^2
$$
$$
v = {4 / 3} \cdot \pi \cdot radius^3
$$
We can use area when calculating volume:
$$
v = {1 / 3} \cdot a \cdot radius
$$
Modify your `sphere_volume` function by calling `sphere_area` inside the function.
## Write a function -- solutions 2
```{python}
#| echo: true
#| eval: true
def sphere_area(radius):
"calculates the area of a sphere of given radius"
area = 4 * 3.1415 * radius**2
return round(area, 2)
def sphere_volume(radius):
"calculates the volume of a sphere of given radius"
volume = (1 / 3) * sphere_area(radius) * radius
return round(volume, 2)
def main():
r = .75
v = sphere_volume(r)
a = sphere_area(r)
print(v, a)
main()
```
## Write a function
Write a Python function named `hypotenuse` that takes two arguments: `a` and `b` representing the length of the two non-hypotenuse sides of a right triangle.
The function calculate the hypotenuse according to the Pythagorean theorem: $c = \sqrt(a^2 + b^2)$. Return it rounded at two decimals.
Test cases: `hypotenuse(3, 4)` should return `5.0`, `hypotenuse(10, 10)` should return `14.14`
Name your file `hypotenuse.py` and submit to gradescope.
## Write a function
```{python}
#| eval: true
#| echo: true
def sqrt(n):
'''
This function calculates the square root of a number
Args:
n: integer or float
Returns:
The square root of n
'''
return n**0.5
def hypotenuse(a, b):
'''
This function calculates the hypotenuse of a right angle triangle.
Args:
a: number (integer or float) representing one of the non-hypotenuse sides
b: number (integer or float) representing one of the non-hypotenuse sides
Returns:
Float representing the length of the hypotenuse given a and b
'''
h = sqrt(a**2 + b**2)
return round(h, 2)
def main():
'''
This function calls the hypotenuse function to calculate and then
print out the hypotenuse of a right angle triangle of sides 3 and 4
and the hypotenuse of a right angle triange of sides 10 and 10
'''
result = hypotenuse(3, 4)
print(result)
result = hypotenuse(10, 10)
print(result)
main()
```