Python Programming · Week 4

Comparisons and Conditionals:
In class

Hyeongmin Lee · SeoulTech, Dept. of Electronic Engineering

Today

Three things

  1. 1 Recap and quiz: the week 4 videos
  2. 2 Exercise 3: all four problems, solved live
  3. 3 A first look at week 5
Recap · 1

= stores. == compares.

six comparisons

3 < 5 → True 3 > 5 → False 5 <= 5 → True 7 >= 10 → False 5 == 5 → True 5 != 3 → True

one = stores, two == compare

>>> x = 5

>>> x == 5

True

>>> 5 = x

SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

bool: a value, like a number

>>> passed = 72 >= 60

>>> passed

Trueright side first, then store

>>> light_on = Truetyped by hand

>>> light_on = true

NameError: name 'true' is not defined. Did you mean: 'True'?

a comparison gives True or False · True and False: a capital letter, no quotes

Recap · 2

Floats: compare the distance, not ==.

✗exactly equal?

>>> 0.1 + 0.2 == 0.3

False

>>> 0.1 + 0.2

0.30000000000000004

✓close enough?

>>> abs(0.1 + 0.2 - 0.3)

5.551115123125783e-17≈ 0

>>> abs(0.1 + 0.2 - 0.3) < 0.000001tolerance

True

✗ a == b → ✓ abs(a - b) < 0.000001
Recap · 3

Strings are compared character by character.

"100" >= "90" → False
100
90

"1" < "9"

"95" >= "90" → True
95
90

"5" > "0"

"Kim" == "kim" → False
Kim
kim

"K" != "k"

"95" >= 90 →
TypeError
Recap · 4

if: a condition, a colon, an indented block.

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")

SyntaxError loud

if score >= 90:

print("Grade: A")

IndentationError loud

score = 72

if score >= 90:

····print("Grade: A")

print("Well done!")

Well done! silent
Recap · 5

if, elif, else: Python stops at the first True.

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")

→ Grade: A

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")

→ Grade: D

the same conditions, in a different order · the order matters

Recap · 6

not, and, or: bools in, a bool out.

not

not True → False
not False → True

and

True and True → True
True and False → False
False and True → False
False and False → False

or

True or True → True
True or False → True
False or True → True
False or False → False

a range, as in math

0 <= score <= 100 same as score >= 0 and score <= 100
Recap · 7

Numbers as True or False.

0 → False
7-32 any other number → True

the or trap

x = 5
if x == 1 or 2:
    print("x is 1 or 2")
x is 1 or 2 silent
x == 1 or 2 False True

→ False or True → True

better: real bools

if x:

✓ if x != 0:

if x == 1 or 2:

✓ if x == 1 or x == 2:

Recap · 8

Order: arithmetic, comparisons, not, and, or.

first to last

  1. 1( )
  2. 2**
  3. 3* / // %
  4. 4+ -
  5. 5< <= > >= == !=
  6. 6not
  7. 7and
  8. 8or

x = 12

x > 10 and x % 2 == 0

↓ 3

12 > 10 and 0 == 0

↓ 5

True and True

↓ 7

True

Not sure? Add parentheses.

Quiz

What comes out?

No computer. Say the answer, then I press Enter.

1

>>>x = 5

>>>x = x == 5

>>>x

2

>>>"3" + "4" == "7"

3

>>>n = input("Number: ")

Number: 7

>>>n > 5

4

>>>t = 25

>>>if t > 10:

... print("warm")

...elif t > 20:

... print("hot")

5

>>>x = 150

>>>x >= 0 or x <= 100

>>>0 <= x <= 100

6

>>>True or False and False

Exercise 3 · Problem 1

Password check

week04_problem1.py 2 points

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.

Exercise 3 · Problem 2

Multiples of 3 and 5

week04_problem2.py 2 points

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.

Exercise 3 · Problem 3

A logic gate

week04_problem3.py 2 points

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.

Exercise 3 · Problem 4

Leap year

week04_problem4.py 4 points

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

Week 5: Loops: while and for

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

← All weeks
01 / 00
Scroll · ↓ · Space