Calvin (Deutschbein)
W3Mon: 8 Sep
if / elif / else
+ - * // / % ** - # operators
= == # assignment vs comparison
return
if / elif / else
def check_move():
if no_beepers_present():
move()
put_beeper()
else:
move()
import karel
def main():
while front_is_clear():
check_move()
if / elif / else
def sign_check(x):
if x < 0:
print("negative")
elif x > 0:
print("positive")
else:
print("zero")
if / elif / else # ✓
+ - * // / % ** - # operators
= == # assignment vs comparison
return
Addition | a + b |
Subtraction | a - b |
Multiplication | a * b |
Division | a / b |
Integer Division | a // b | Leaves a "Remainder" |
Modulo | a % b | calculates the "Remainder" |
Exponentiation | a ** b | Note: The caret ^ is something else |
|
print(1 + 2)
print(1 - 2)
print(1 * 2)
print(1 / 2) |
3
-1
2
0.5 |
Note: Division takes whole numbers (integers) and returns numbers with a decimal ('floats').
|
print(5 // 2)
print(5 % 2)
print(5 ** 2)
print(5 ^ 2) |
2
1
25
7 |
It may seem weird, but % ("modulo" or "remainder") is extremely useful, and I use it more than anything except maybe + ("plus" or "addition")
for i in range(5//2):
print(i)
for i in range(5/2):
print(i)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>>
TypeError: 'float' object cannot be interpreted as an integer
type()
print(type(5/2))
print(type(5//2))
print(type(5))
print(type(5.0)) |
<class 'float'>
<class 'int'>
<class 'int'>
<class 'float'> |
'for' loops need "<class 'int'>"
if / elif / else # ✓
+ - * // / % ** - # ✓
= == # assignment vs comparison
return
=
x = 7
print(x)
print(7)
print(x+6) |
7
7
13 |
7 = x
(We aren't allowed to set seven equal to any other value, like 3 or "hi")
x = 7
x = x + 1
print(x)
def ten_even_numbers():
x = 0
for i in range(10):
x = x + 2
print(x)
==
x = 7
print(7 == x)
print(x == 7)
print(6 == x)
print(1 == 2)
print(x + 6) |
# Blank line to line up.
True
True
False
False
13 |
|
print(1 >= 2)
print(1 <= 2)
print(1 != 2)
print(1 > 2)
print(1 < 2)
print(1 < 1)
print(1 <= 1) |
False
True
True
False
True
False
True |
def is_even(x): # check if x is even
if x % 2 == 0: # no remainder when divided by two
print("even")
elif x % 2 == 1:
print("odd")
else:
print("not a whole number")
print(is_even(2))
print(is_even(1))
print(is_even(1.5)) |
even
odd
not a whole number |
if / elif / else # ✓
+ - * // / % ** - # ✓
= == # ✓
return
def is_even(x):
print(is_divisible_by(x,2))
def bigger(x,y):
if x > y:
print(x)
else:
print(y)
def is_divisible_by(value,divisor):
print(value % divisor == 0) # has no remainder
def is_even(x):
print(is_divisible_by(x,2))
def is_divisible_by(value,divisor):
return (value % divisor == 0) # has no remainder
def is_even(x):
return is_divisible_by(x,2)
def is_divisible_by(value,divisor):
# checks if value is evenly divided by divisor
check = value % divisor == 0 # compare remainder to zero
if check: # print what we find
print("Value is evenly divided by divisor")
else:
print("Value is not evenly divided by divisor")
# do not forget to return
return check