Calvin (Deutschbein)
W3Wed: 11 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)
<class 'int'>
>>> type(1.0)
<class 'float'>
>>> type(1==1.0)
<class 'bool'>
>>> type(False)
<class 'bool'>
|
>>> a = True
>>> b = False
>>> a and b
False
>>> a or b
True
>>> not a
False |
In general: if you aren't sure, just check.
|
>>> if 0:
... print('hi')
...
>>> if "":
... print('hi')
...
>>> if print():
... print('hi')
...
>>> if .1:
... print('hi')
...
hi
>>> bool(0)
False |
In general: if you aren't sure, just check.
|
>>> 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
>>> type('hi') # to be continued...
def add(x,y):
return x + y
def divide(x, y): # no hints
return x / y
def intdiv(x:int, y:int) -> int: # yes hints
return x // y
>>> type(None)
<class 'NoneType'>
>>> type(print('hi'))
hi
<class 'NoneType'>
>>> def print_sum_of(x:int,y:int) -> None:
... print(x + y)
...
>>> type(print_sum_of(3,4))
7
<class 'NoneType'>
>>>
>>> x = print_sum_of(3,4)
7
>>> x
>>>
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