Python Programming · Week 4
Hyeongmin Lee · SeoulTech, Dept. of Electronic Engineering
six comparisons
one = stores, two == compare
bool: a value, like a number
a comparison gives True or False · True and False: a capital letter, no quotes
✗exactly equal?
✓close enough?
"1" < "9"
"5" > "0"
"K" != "k"
score = int(input("Score: "))
if score >= 90True or False:
····print("Grade: A")
····print("Well done!")
only when True
print("Done")
every time
three common mistakes
if score >= 90
····print("Grade: A")
if score >= 90:
print("Grade: A")
score = 72
if score >= 90:
····print("Grade: A")
print("Well done!")
90 first · the user typed 95
score = int(input("Score: "))
if score >= 90: True
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
else:
print("Grade: F")
60 first · the user typed 95
score = int(input("Score: "))
if score >= 60: True
print("Grade: D")
elif score >= 70:
print("Grade: C")
elif score >= 80:
print("Grade: B")
elif score >= 90: never reached
print("Grade: A")
else:
print("Grade: F")
the same conditions, in a different order · the order matters
not
and
or
a range, as in math
the or trap
x = 5
if x == 1 or 2:
print("x is 1 or 2") → False or True → True
better: real bools
if x:
✓ if x != 0:
if x == 1 or 2:
✓ if x == 1 or x == 2:
first to last
x = 12
x > 10 and x % 2 == 0
↓ 3
12 > 10 and 0 == 0
↓ 5
True and True
↓ 7
True
Not sure? Add parentheses.
No computer. Say the answer, then I press Enter.
>>>x = 5
>>>x = x == 5
>>>x
>>>"3" + "4" == "7"
>>>n = input("Number: ")
Number: 7
>>>n > 5
>>>t = 25
>>>if t > 10:
... print("warm")
...elif t > 20:
... print("hot")
>>>x = 150
>>>x >= 0 or x <= 100
>>>0 <= x <= 100
>>>True or False and False
Ask for a password, then ask for it again. Different: They do not match. The same but shorter than 8 characters: Too short. Otherwise: Password set.
on the screen · one run
Password: python314
Type it again: python314
Password set.
examples · typed values in order
typed
prints
python314, python314
Password set.
python314, Python314
They do not match.
abc, abc
Too short.
Ask for a whole number. Is it a multiple of 3, of 5, of both, or of neither? 0 or less: Please type a number greater than 0.
on the screen · one run
Number: 30
Multiple of 3 and 5
examples · typed values in order
typed
prints
30
Multiple of 3 and 5
9
Multiple of 3
10
Multiple of 5
7
Neither
-3
Please type a number greater than 0.
Ask for a gate (AND, OR, or XOR, typed in capital letters), then inputs A and B, each 0 or 1. Print the output. AND: 1 only when both are 1. OR: 1 when at least one is 1. XOR: 1 when exactly one is 1. Any other gate: Unknown gate.
on the screen · one run
Gate: XOR
A: 1
B: 0
Output: 1
examples · typed values in order
typed
prints
AND, 1, 1
Output: 1
OR, 0, 0
Output: 0
XOR, 1, 0
Output: 1
NAND, 1, 1
Unknown gate.
Ask for a year. Print whether it is a leap year: divisible by 4 but not by 100, or divisible by 400.
on the screen · one run
Year: 2024
2024 is a leap year.
examples · typed values in order
typed
prints
2024
2024 is a leap year.
1900
1900 is not a leap year.
2000
2000 is a leap year.
2026
2026 is not a leap year.
Next week
Videos and exercise 4: up Tuesday–Wednesday.
Exercise 4: due Monday, October 5, 11:59 a.m.
No class on October 5 (a holiday).
Slides: hyeongminlee.github.io/slides/python-programming