Calvin (Deutschbein)
W3Wed: 10 Sep
range()
and / or / not
def / return
range()
The Anapurna range of the Himalayas (that mountain is ~5 miles tall)
range()
def turn_right():
for i in range(3):
turn_left()
range()
print(range(10))
range(0, 10)
print(range(5,10))
range(5, 10)
range()
for i in range(3): # smaller so its easy to work with
print(i)
0
1
2
range()
for i in range(4,6):
print(i)
4
5
range()
for i in range(25,50):
if i % 5 == 0:
print(i)
25
30
35
40
45
range()
for i in range(25,50,10):
print(i)
25
35
45
range()
for i in range(15,10,-2):
print(i)
15
13
11
range()
for i in range(15,10,-2):
print(i)
i = 15
while i > 10:
print(i)
i = i - 2
✓: range()
and / or / not
def / return
type(1)
type(1.0)
type(1==1.0)
|
<class 'int'>
<class 'float'>
<class 'bool'> |
<class 'bool'>
a = True
b = False
a and b | True if 'a' and 'b' are True | False |
a or b | True if 'a' or 'b' be True | True |
not a | True if 'a' is not True ('a' is False) | False |
In general: if you aren't sure, just check.
0 | Non-zero values are treated like True |
0.0 | Non-zero values are treated like True |
"" | Imagine if we want to print nothing. |
None | None is the return value of a function with no return (???) |
if 0:
print('zero')
if "":
print('empty')
if print("anything"):
print('printing')
if .1:
print('.1')
print(bool(0))
anything
.1
False
|
for i in range(10,20):
if i % 3 == 0:
print(i)
if not i % 3:
print(i) 12
12
15
15
18
18 |
for i in range(0,50,5):
if not i % 3:
print(i)
0
15
30
45
for i in range(50):
if i % 3 == 0 and i % 5 == 0:
print(i)
I took a whole class in undergrad on "counting two ways" (it was fun!)
for i in range(50):
if i % 3 == 0 and i % 5 == 0:
print(i)
for i in range(50):
if ((i % 3) == 0) and ((i % 5) == 0):
print(i)
for i in range(50):
rem3 = (i % 3)
rem5 = (i % 5)
if not rem3 and not rem5: # 'not' any remainder,
print(i)
✓: range()
✓: and / or / not
def / return
def check_move():
if no_beepers_present():
move()
put_beeper()
else:
move()
def divisible_by_six_or_seven(x:int, y:int) -> int:
# there must be the possibility of something printing in here
return # must return an int