Review
- Return floating point values without modification
- print values “nicely” e.g. using
{foo:0.02f}
- The formatting won’t modify the value – it will just change how it looks
- So you can see it “nicely”, but still have all of the precision available.
div/mod and debugging
- Look at IC-3, Task 2
- Make sure you understand the problem and can solve it by hand.
- If you can’t solve it without a computer, then you can’t solve it with.
- Be sure to ask me for clarification if the problem description doesn’t make sense.
- Use helper variables
- make a mistake for middle digit
x // 10
- Observe the incorrect result.
- Use the debugger to narrow down where the problem is.
- Make sure you understand the problem and can solve it by hand.
input
- Explain IC-4, Task 3 better
- Add I/O to IC-3, Task 2 or 3 as an example
Function interfaces
- Emphasize the importance of not changing the interface.
- e.g., can’t switch to three parameters for the
sum_digits
problem.
- e.g., can’t switch to three parameters for the
- Discuss how tests work
- My tests are written to call the function as described. If you change the interface, then the code won’t run..
- Try to call a 1-param function with 2 parameters.
Conditionals
Boolean Values
- Remember:
- values are groups of 1s and 0s in RAM.
- type tells us how to interpret the 1s and 0s.
- The set/range of values
- The set of legal operations
- A
boolean
type with only two values- traditionally
True
andFalse
- traditionally
- They are not strings. Written without quotation marks
- Also need to be capitalized.
Boolean Expressions
- Remember: An expression is a set of operations that can evaluate to a value
4 + 3*2
- A _boolean expression` is an expression that evaluates to a boolean value.
- A boolean operator is an operator that evaluates to either
True
orFalse
==
is the equality operator. It is eitherTrue
orFalse
5 == 5
isTrue
5 == 6
isFalse
- Emphasize difference between assignment (
=
) and equality (==
) - Other comparisons
x != y # x is not equal to y
x > y # x is greater than y
x < y # x is less than y
x >= y # x is greater than or equal to y
x <= y # x is less than or equal to y
- Logical operators
x = True
y = False
print(x or y)
print(x and y)
print(not x)
in
:'a' in 'apple'
- Can’t do this:
'a' in 'dog' or 'cat'
. Need'a' in 'dog' or 'a' in 'cat'
If/else
def even_or_odd(num : int) -> str:
if (num % 2 == 0):
return 'even'
else:
return 'odd'
x = 6
print(f"{x} is {even_or_odd(x)}.")
- The
else
clause is optional — Sometimes you don’t need it. - You can nest conditional statements (
even_odd_neither
).- Notice the nested indentation
-
‘You get a B!’
- With if-else chain, order can be important.
- is_leap_year
Old notes
- if
- if-else
max_value
- if-elsif-else
- too hot / too cold / just right
- if-elsif-elsif-elsif-else
- Letter grades
- Indentation matters
- Spaces different than tabs
=
vs==
- Operator chaining
a < b < c
- somewhat unique to Python
and
/or
/not
!=
as shortcut fornot x == y
- implicit vs explicit ranges
- i.e., choosing order of if statements carefully
- Look at letter grades again.
- chain of if vs. else-if
- nested if
- Compute bonus for different structures
- Structure A: 10% for sales over 10,000
- Structure B: $100 if sales is a multiple of 5.
- Compute bonus for different structures
- integers and floats can be compared. (One is coerced)
- strings can be compared using
==
,<
,<=
, etc. - can’t directly compare strings and integers
- Avoid comparing
float
using==
- Why?
is
vs ==- watch for
if x = 9:
(instead of==
)-
Example
def print_discount(has_discount: bool): if (has_discount = True): print('You have a discount!') else print(f"No discount. (Your status {has_discount})") print_discount(False)
- Actually, this is a syntax error. Need to use
:=
. - The requirement of the
:
is to avoid this common mistake.
-
- Order of operations
- precedence:
not
,and
,or
not x or y
is evaluated as(not x) or y
.x == 5 or y == 10 and z != 10
is evaluated as(x == 5) or ((y == 10) and (z != 10))
becauseand
has precedence overor
.- When in doubt, just use parentheses.
- precedence:
-
conditional/ ternary operator
x = 18 parity = 'even' if x %2 == 0 else 'odd'
- Expression trees