Python Programming · Week 4
Hyeongmin Lee · SeoulTech, Dept. of Electronic Engineering
so far
every run: every line, top to bottom
this week · one program, four runs
Score: 95
Grade: A
Score: 72
Grade: C
Score: 45
Grade: F
Score: 150
A score is from 0 to 100.
it looks at the answer, then chooses what to do
1 · it asks a question about a value
comparisons
2 · it chooses which lines run
conditionals
comparisons and conditionals · this week's topic
score = int(input("Score: "))
if 0 <= score <= 100:
if score >= 90:
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")
else:
print("A score is from 0 to 100.")
five questions
each one answers True or False
comparisons
if · elif · else
the answers choose which lines run
conditionals
fourteen lines · by the end of this video, you can read and write every one
>>> score = 95
>>> score >= 90
True
>>> score = 72
>>> score >= 90
False
six comparisons
<
less than
3 < 5 → True
>
greater than
3 > 5 → False
<=
less than or equal to
5 <= 5 → True
>=
greater than or equal to
7 >= 10 → False
==
equal to
5 == 5 → True
!=
not equal to
5 != 3 → True
a comparison asks a question · the answer is True or False
one =
stores 5 in x
two ==
asks: is x equal to 5?
one = stores · two == ask a question
operators · two values in, one value out
72 + 60 → 132
72 >= 60 → True
>>> type(3 < 5)
<class 'bool'>bool: the kind for True and False
>>> passed = 72 >= 60
>>> passed
Trueright side first, then store
>>> light_on = True
>>> light_on
Truetyped by hand, like 3 or "Kim"
>>> light_on = true
NameError: name 'true' is not defined. Did you mean: 'True'?
four kinds so far
no quotes · a capital first letter
Comparisons
False
week 2: 0.1 + 0.2 → 0.30000000000000004
== asks “exactly equal?” · a float is often a tiny bit off
absolute value
abs gives the absolute value
in Python: abs(a - b)
The two values from the last slide: 0.1 + 0.2 and 0.3.
>>> abs(0.1 + 0.2 - 0.3)
5.551115123125783e-17the distance: almost zero
>>> abs(0.1 + 0.2 - 0.3) < 0.000001
Trueclose enough
floats: do not ask “exactly equal?” · ask “close enough?”
so far, at the prompt
>>> score >= 90 → True and that is all
in a program
True → print("Grade: A") the answer chooses what runs
a program, drawn as paths
Score: 95
Grade: A
Well done!
Done
Score: 72
Done
True: the side path runs · False: it is skipped · then both paths go on together
The same program, written in Python.
a file, run with the Run button
score = int(input("Score: "))
if score >= 90:
print("Grade: A")
print("Well done!")
print("Done")
if, a condition, a colon
the condition is the question: True or False
the block: indented under the if, 4 spaces
it belongs to the if: both lines run only when True
print("Done"): no indent
outside the block: it runs every time
no colon
score = 95
if score >= 90
print("Grade: A") a loud error · read the last line
no indent
score = 95
if score >= 90:
print("Grade: A") a loud error
Well done! not indented
score = 72
if score >= 90:
print("Grade: A")
print("Well done!") printed for 72: outside the block · no error, and a wrong answer
the block is the indented lines · VS Code adds the 4 spaces after a colon
When the condition is False.
score = int(input("Score: "))
if score >= 60:
print("Pass")
else:
print("Fail") else has no condition, just a colon
Score: 72
Pass
Score: 45
Fail
if or else · exactly one of the two blocks runs
Short for “else if”: one more question, asked only when the ones above were False.
score = int(input("Score: "))
if score >= 90:
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") the user typed 85
from the top · stops at the first True · the rest are skipped · else: when none is True
The same questions, upside down.
score = int(input("Score: "))
if score >= 60:
print("Grade: D")
elif score >= 70:
print("Grade: C")
elif score >= 80:
print("Grade: B")
elif score >= 90:
print("Grade: A")
else:
print("Grade: F") 95 prints Grade: D · no error, and a wrong answer: the worst case
the user typed 95
Python stops at the first True · so ask about 90 before 80, 80 before 70
score = input("Score: ")
if score >= 90:
print("Grade: A") Score: 95
TypeError: '>=' not supported between instances of 'str' and 'int'
quotes around 90?
if score >= "90":
no error now: two strings
a number typed by the user · change it to a number first, then compare
Operators on True and False: bools in, a bool out.
not
turns True into False, and False into True
and
True only when both sides are True
or
True when at least one side is True
score is 85
if score >= 0 and score <= 100:
→ True and True → True
the block runs
if score < 0 or score > 100:
→ False or False → False
the block is skipped
if 0 <= score <= 100:
a range, as in math:
exactly the same as score >= 0 and score <= 100
every number except 0
in not, and, or
Python allows this · but give not, and, or real bools: comparisons such as x != 0
x = 5
if x == 1 or 2:
print("x is 1 or 2") x is 5 · no error, and a wrong answer
how Python reads it: two sides, each on its own
→ False or True → True
the right side is always 2: True for every x
a whole comparison on each side
if x == 1 or x == 2:
x is 5: False or False → False · nothing is printed
first to last
grey: from week 2
step by step
x = 12
x > 10 and x % 2 == 0
12 > 10 and 0 == 0
arithmetic first: 12 % 2 is 0
True and True
then the comparisons
True
then and
Not sure? Add parentheses.
score = int(input("Score: "))
if 0 <= score <= 100:
if score >= 90:
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")
else:
print("A score is from 0 to 100.")
an if inside an if works like and
if 0 <= score <= 100:
if score >= 90:
print("Grade: A")
the same as
if 0 <= score <= 100 and score >= 90:
an inner if runs only when its outer block runs · so a grade is given only for a score from 0 to 100
score = int(input("Score: "))
if 0 <= score <= 100:
if score >= 90:
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")
else:
print("A score is from 0 to 100.") ←int(): the typed score, as a number
←the range first · a short form of and
←an if inside the if · the grade chain
←from the top · stops at the first True
←every comparison above was False
←the score is not from 0 to 100
fourteen lines · every idea on them comes from this video
score = int(input("Score: "))
if 0 <= score <= 100:
if score >= 90:
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")
else:
print("A score is from 0 to 100.")
four typed values
typed 95
Grade: A
typed 85
Grade: B
typed 59
Grade: F
typed 150
A score is from 0 to 100.
last week, every line ran every time
now the typed value chooses which lines run
Next steps
Have Python open. Type everything with me.
See you in class.