Calvin (Deutschbein)
W3Mon: 9 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 |
|
>>> 1 + 2
3
>>> 1 - 2
-1
>>> 1 * 2
2
>>> 1 / 2
0.5 |
Note: Division takes whole numbers (integers) and returns numbers with a decimal ('floats').
|
>>> 5 // 2
2
>>> 5 % 2
1
>>> 5 ** 2
25
>>> 5 ^ 2
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)
...
0
1
>>> 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()
>>> type(5/2)
<class 'float'>
>>> type(5//2)
<class 'int'>
>>> type(5)
<class 'int'>
>>> type(5.0)
<class 'float'>
'for' loops need "<class 'int'>"
✓: if / elif / else
✓: + - * // / % ** - # operators
= == # assignment vs comparison
return
=
>>> x = 7
>>> 7
7
>>> x
7
>>> x + 6
13
It means "make this equal to that"
>>> x = 7
>>> x = x + 1
>>> x
8
>>>
def ten_even_numbers():
x = 0
for i in range(10):
x = x + 2
print(x)
==
>>> x = 7
>>> 7 == x
True
>>> x == 7
True
>>> 6 == x
False
>>> 1 == 2
False
>>> x + 6
13
|
>>> 1 >= 2
False
>>> 1 <= 2
True
>>> 1 != 2
True
>>> 1 > 2
False
>>> 1 < 2
True
>>> 1 < 1
False
>>> 1 <= 1
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")
>>> is_even(2)
even
>>> is_even(1)
odd
>>> is_even(1.5)
not a whole number
✓: if / elif / else
✓: + - * // / % ** - # operators
✓: = == # assignment vs comparison
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)
>>> is_divisible_by(10,2)
True
>>> print(is_divisible_by(10,2))
True
>>> x = is_divisible_by(10,2)
>>> x
True
>>> if is_divisible_by(10,2):
... print('10 is even')
...
10 is even
>>>
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