Skip to content
Snippets Groups Projects
Commit 360d7777 authored by Anna Meyer's avatar Anna Meyer
Browse files

review

parent ab25a19e
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:482016d5 tags: %% Cell type:markdown id:482016d5 tags:
# Exam 1 review # Exam 1 review
%% Cell type:markdown id:96330469 tags: %% Cell type:markdown id:96330469 tags:
## Arguments ## Arguments
%% Cell type:code id:138a92cc tags: %% Cell type:code id:138a92cc tags:
``` python ``` python
def person(name, age, color='blue', animal='cat'): def person(name, age, color='blue', animal='cat'):
print(name, " is ", age, " years old ", end="") print(name, " is ", age, " years old ", end="")
print("their favorite color is ",color, end="") print("their favorite color is ",color, end="")
print(" and their favorite animal is ",animal) print(" and their favorite animal is ",animal)
``` ```
%% Cell type:code id:a5f2edbc tags: %% Cell type:code id:a5f2edbc tags:
``` python ``` python
# TODO: what are all the ways we can (or cannot) call person()? # TODO: what are all the ways we can (or cannot) call person()?
``` ```
%% Cell type:markdown id:6f623b4b tags: %% Cell type:markdown id:6f623b4b tags:
## Function Scope ## Function Scope
%% Cell type:markdown id:e54320ef tags: %% Cell type:markdown id:e54320ef tags:
### Example 1 -- basic function scope ### Example 1 -- basic function scope
%% Cell type:code id:7e4d570a tags: %% Cell type:code id:7e4d570a tags:
``` python ``` python
pi = 3.14 pi = 3.14
alpha = 4 alpha = 4
def my_func(alpha, beta): def my_func(alpha, beta):
# TODO: what variables are accessible at line 5? what values do they have? # TODO: what variables are accessible at line 5? what values do they have?
delta = alpha + beta delta = alpha + beta
gamma = delta ** 2 gamma = delta ** 2
return gamma return gamma
answer = my_func(alpha, pi) answer = my_func(alpha, pi)
answer2 = my_func(answer, alpha) answer2 = my_func(answer, alpha)
# TODO what variables are accessible here? What values do they have? # TODO what variables are accessible here? What values do they have?
``` ```
%% Cell type:markdown id:33a302bf tags: %% Cell type:markdown id:33a302bf tags:
### Example 2 -- global vs. local ### Example 2 -- global vs. local
%% Cell type:code id:7a5ed848 tags: %% Cell type:code id:7a5ed848 tags:
``` python ``` python
two = 3.14 two = 3.14
alpha = 4 alpha = 4
def my_func1(alpha, beta): def my_func1(alpha, beta):
# TODO: what variables are accessible at line 5? what values do they have? # TODO: what variables are accessible at line 5? what values do they have?
delta = alpha + beta delta = alpha + beta
gamma = delta * two gamma = delta * two
return gamma return gamma
answer = my_func(alpha, two) answer = my_func(alpha, two)
# TODO what variables are accessible here? What values do they have? # TODO what variables are accessible here? What values do they have?
``` ```
%% Cell type:code id:04428811 tags: %% Cell type:code id:04428811 tags:
``` python ``` python
two = 2 two = 2
alpha = 14 alpha = 14
def my_func2(alpha, beta): def my_func2(alpha, beta):
# TODO: what variables are accessible at line 5? what values do they have? # TODO: what variables are accessible at line 5? what values do they have?
delta = alpha + beta delta = alpha + beta
two = 7 two = 7
gamma = delta * two gamma = delta * two
return gamma return gamma
answer = my_func(alpha, two) answer = my_func(alpha, two)
# TODO what variables are accessible here? What values do they have? # TODO what variables are accessible here? What values do they have?
``` ```
%% Cell type:code id:51696c45 tags: %% Cell type:code id:51696c45 tags:
``` python ``` python
two = 2 two = 2
alpha = 14 alpha = 14
def my_func3(alpha, beta): def my_func3(alpha, beta):
# TODO: what variables are accessible at line 5? what values do they have? # TODO: what variables are accessible at line 5? what values do they have?
delta = alpha + beta delta = alpha + beta
gamma = delta * two gamma = delta * two
two = 7 two = 7
return gamma return gamma
answer = my_func(alpha, two) answer = my_func(alpha, two)
# TODO what variables are accessible here? What values do they have? # TODO what variables are accessible here? What values do they have?
``` ```
%% Cell type:code id:6cdb7a02 tags: %% Cell type:code id:6cdb7a02 tags:
``` python ``` python
two = 2 two = 2
alpha = 14 alpha = 14
def my_func3(alpha, beta): def my_func3(alpha, beta):
# TODO: what variables are accessible at line 5? what values do they have? # TODO: what variables are accessible at line 5? what values do they have?
delta = alpha + beta delta = alpha + beta
global two global two
gamma = delta * two gamma = delta * two
two = 7 two = 7
return gamma return gamma
answer = my_func(alpha, two) answer = my_func(alpha, two)
# TODO what variables are accessible here? What values do they have? # TODO what variables are accessible here? What values do they have?
``` ```
%% Cell type:markdown id:2ebbfa8b tags: %% Cell type:markdown id:2ebbfa8b tags:
## Refactoring ## Refactoring
%% Cell type:markdown id:c61c0d46 tags: %% Cell type:markdown id:c61c0d46 tags:
### Example 1 ### Example 1
%% Cell type:code id:7e25ba85 tags: %% Cell type:code id:7e25ba85 tags:
``` python ``` python
def some_func(): def some_func():
if some_cond1: if some_cond1:
if some_cond2: if some_cond2:
if some_cond3: if some_cond3:
return True return True
return False return False
``` ```
%% Cell type:code id:d6dc250a tags: %% Cell type:code id:d6dc250a tags:
``` python ``` python
# TODO how can we rewrite some_func() to only use 1 line of code? # TODO how can we rewrite some_func() to only use 1 line of code?
def some_func_short(): def some_func_short():
pass pass
``` ```
%% Cell type:markdown id:b9da2d00 tags: %% Cell type:markdown id:b9da2d00 tags:
### Example 2 ### Example 2
%% Cell type:code id:678f8ec2 tags: %% Cell type:code id:678f8ec2 tags:
``` python ``` python
def some_func_1(x, y, z): def some_func_1(x, y, z):
if x >= 7: if x >= 7:
return True return True
if y < 18: if y < 18:
return True return True
if z == 32: if z == 32:
return True return True
else: else:
return False return False
``` ```
%% Cell type:code id:d1bf3938 tags: %% Cell type:code id:d1bf3938 tags:
``` python ``` python
# TODO which of these refactoring options is correct? # TODO which of these refactoring options is correct?
def some_func_1a(x, y, z): def some_func_1a(x, y, z):
return x >= 7 or y < 18 or z == 32 return x >= 7 or y < 18 or z == 32
def some_func_1b(x, y, z): def some_func_1b(x, y, z):
return x >= 7 and y < 18 and z == 32 return x >= 7 and y < 18 and z == 32
``` ```
%% Cell type:code id:063829c0 tags: %% Cell type:code id:063829c0 tags:
``` python ``` python
# TODO # TODO
# Discuss: given two possible versions of a function (where one is correct), # Discuss: given two possible versions of a function (where one is correct),
# what is the general strategy for figuring out which one is correct? # what is the general strategy for figuring out which one is correct?
``` ```
%% Cell type:markdown id:d1ba721c tags:
## Iteration
%% Cell type:markdown id:851c3e95 tags:
### Example 1 (Reimann sums)
%% Cell type:code id:85e203c9 tags:
``` python
# Let's try the values from 1 to 5
start_x = 1
end_x = 5
total_area = 0
current_x = start_x
# Try out increasing values of width, make sure to comment the other width values
# delta_x = 1
delta_x = 0.1
# delta_x = 0.01
# delta_x = 0.001
while current_x <= end_x:
y = ??? # TODO: use f(x) defined previously
rect_area = ???
total_area += ???
current_x += delta_x
print("Area found using approximation is:", total_area)
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment