Python Programming · Week 2
Hyeongmin Lee · SeoulTech, Dept. of Electronic Engineering
at the prompt
>>> 3 * 7
21
in a file, then Run
3 * 7# nothing
print(3 * 7)# 21
in a file, print what you want to see · # starts a note for humans, Python skips the line
int · counting
2 -7 1000000
float · measuring, a decimal point
2.0 0.5 3e8
2 and 2.0: same value, different kind
int or float? three rules
int with int stays int
7 // 2 → 3
except /: always a float
6 / 2 → 3.0
any float in, float out
2 * 3.0 → 6.0
// and % come as a pair: 258 s is 258 // 60 = 4 min and 258 % 60 = 18 s · ^ is not power
same level: left to right · the exam expects you to know this order
>>> 2 + 3 * 4
14
>>> (2 + 3) * 4
20
>>> 2 ** 3 * 2
16
>>> 1 / (1/220 + 1/330)
132.0
>>> 1,000,000
(1, 0, 0)
>>> 2 ^ 10
8
>>> print("3 * 7")
3 * 7
the worst case
no error, and a wrong answer
good news
an error message: Python stopped and told you where
read it from its last line
>>> 0.1 + 0.2
0.30000000000000004
>>> 1 / 3
0.3333333333333333
why
No computer. Say the answer, then I press Enter.
>>> 7 / 2
>>> 6 / 2
>>> 7 // 2
>>> 7 % 2
>>> 2 * 3.0
>>> 2 + 3 * 4
>>> 2 ^ 10
>>> 0.1 + 0.2
why
This is what the exam looks like.
one line of Python, and you write what comes out
Solved live, from the empty file.
A silent wrong answer is the worst case.
loud · Python stops and tells you
silent · it runs, and the answer is wrong
Next week
Videos and exercise 2: up Tuesday–Wednesday.
Exercise 2: due before next Monday's class.
Slides: hyeongminlee.github.io/slides/python-programming