Arithmetic
Calvin (Deutschbein)
W3Mon: 9 Sep
Announcements
- By MONDAY 11:59 PM: "Problem Set 1: Karel" assignment.
- One problem (probably) using "def" and "for"
- One problem using at least "while" and maybe the other three.
- Sections reminder: Wed & Thurs PM.
- Problems: email jjrembold@, you may cc ckdeutschbein@
- By NEXT MONDAY 11:59 PM: "Problem Set 2: Checkers" assignment.
- One Karel problem (probably) using "if" and "while"
- One Python problem using arithmetic and (probably) "for"
- One writing problem.
Today
- Do we need homework screencapture slides?
- Doing homework: It's fun.
- I can live-code any homework questions.
- Let's crush this homework... together.
- Introducing Python, cont.
if / elif / else
+ - * // / % ** - # operators
= == # assignment vs comparison
return
if / elif / else
- We know 'if', but 'else' is very useful on Checkerboard
- E.g., place beeper after moving ONLY if moving from an unbeeped square.
def check_move():
if no_beepers_present():
move()
put_beeper()
else:
move()
- What happens if we:
import karel
def main():
while front_is_clear():
check_move()
if / elif / else
- 'elif', short for 'else if', allows longer, more complicated checks.
def sign_check(x):
if x < 0:
print("negative")
elif x > 0:
print("positive")
else:
print("zero")
Today
- Introducing Python, cont.
✓: if / elif / else
+ - * // / % ** - # operators
= == # assignment vs comparison
return
Operators
- Most arithmetic in Python is "as expected" with a few differences.
- Easy ones:
Addition | a + b |
Subtraction | a - b |
Multiplication | a * b |
Division | a / b |
- Weirder ones:
Integer Division | a // b | Leaves a "Remainder" |
Modulo | a % b | calculates the "Remainder" |
Exponentiation | a ** b | Note: The caret ^ is something else |
Operators
- Most arithmetic in Python is "as expected" with a few differences.
- Easy ones:
Addition | a + b |
Subtraction | a - b |
Multiplication | a * b |
Division | a / b |
|
>>> 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').
Operators
- Most arithmetic in Python is "as expected" with a few differences.
- Weirder ones:
Integer Division | a // b | Leaves a "Remainder" |
Modulo | a % b | calculates the "Remainder" |
Exponentiation | a ** b | Note: The caret ^ is something else |
|
>>> 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")
Two divisions
- Integer division // is nice when we might want to calculate a value then loop that many times.
>>> 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()
- If you ever aren't sure what something will be, ask Python with "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'>"
Today
- Introducing Python, cont.
✓: if / elif / else
✓: + - * // / % ** - # operators
= == # assignment vs comparison
return
Weird thing
- We are allowed to set a variable, like x, equal to some arithmetic over x, like x = 1.
>>> x = 7
>>> x = x + 1
>>> x
8
>>>
- This is useful to e.g. count things.
- Imagine if we could count how many beepers Karel placed, or how many moves.
def ten_even_numbers():
x = 0
for i in range(10):
x = x + 2
print(x)
==
- We use '==', which I term 'doubles equals', 'equality test', or 'comparison', to 'check' if two things, either values or variables, are equal.
- '==' is kind like '+': given two things, it gives back a value - but that value is either true or false.
>>> x = 7
>>> 7 == x
True
>>> x == 7
True
>>> 6 == x
False
>>> 1 == 2
False
>>> x + 6
13
Comparison Operators
- Besides double equals, many other tests for equality/inequality.
- Easy ones:
Greater-than-or-equal | a >= b |
Less-than-or-equal | a <= b |
Not equal | a != b |
Greater-than | a > b |
Less-than | a > b |
|
>>> 1 >= 2
False
>>> 1 <= 2
True
>>> 1 != 2
True
>>> 1 > 2
False
>>> 1 < 2
True
>>> 1 < 1
False
>>> 1 <= 1
True |
Checking Numbers
- We check if numbers are even...
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")
- Test it:
>>> is_even(2)
even
>>> is_even(1)
odd
>>> is_even(1.5)
not a whole number
Today
- Introducing Python, cont.
✓: if / elif / else
✓: + - * // / % ** - # operators
✓: = == # assignment vs comparison
return
Return
- Print is how the computer tells us things.
- Sometimes we want it to tell itself things, like:
- Whether some number is divisible by another number...
- The greater of two numbers..
- Print has a limitation here.
def is_even(x):
print(is_divisible_by(x,2))
def bigger(x,y):
if x > y:
print(x)
else:
print(y)
- What if I want to assign some variable 'c' to be the greater of 'a' and 'b'?
Return
- If we write "is_divisible_by" like this:
def is_divisible_by(value,divisor):
print(value % divisor == 0) # has no remainder
}
- This code won't work:
def is_even(x):
print(is_divisible_by(x,2))
Return
- Why? 'is_divisible_by' prints True or False, but doesn't tell 'is_even' what it prints.
- The way to 'tell' a computer what happened in a def/function is with 'return':
def is_divisible_by(value,divisor):
return (value % divisor == 0) # has no remainder
- We use return on its own line, and it doesn't need parenthesis or colons.
def is_even(x):
return is_divisible_by(x,2)
- Something like 'front_is_blocked()" returned either True or False.
Return
- We can compare the two:
>>> 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
>>>
Return
- Until you write code with zero errors on the first time...
- You should never write a function (def) with no return.
- Most programers expect Python to return when it does not.
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
- Print is for you to read, return is for the computer or for you to use.
Exercise
- Do "Problem Set 1: Karel"
- Use comments
- The first problem "newspaper" benefits from functions, and perhaps for loops
- It is possible, but annoying, to do without either
- The second problem, "painting" benefits from 'while' loops (and could use 'if')
- I used four (4) 'while' loops, and no 'if' statements or 'for' loops
Announcements
- By MONDAY 11:59 PM: "Problem Set 1: Karel" assignment.
- One problem (probably) using "def" and "for"
- One problem using at least "while" and maybe the other three.
- Sections reminder: Wed & Thurs PM.
- Problems: email jjrembold@, you may cc ckdeutschbein@
- By NEXT MONDAY 11:59 PM: "Problem Set 2: Checkers" assignment.
- One Karel problem (probably) using "if" and "while"
- One Python problem using arithmetic and (probably) "for"
- One writing problem.