Python Programming · Week 2

Numbers and Calculation:
In class

Hyeongmin Lee · SeoulTech, Dept. of Electronic Engineering

Today

Four things

  1. 1 Recap and quiz: the week 2 videos
  2. 2 Exercise 1: all five problems, solved live
  3. 3 One message, once more: errors are good news
  4. 4 A first look at week 3
Recap · 1

The prompt shows every answer. A file shows only what you print.

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

Recap · 2

int and float are different kinds.

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

Recap · 3

Seven operators. Three of them are new.

+ add 7 + 2 → 9
- subtract 7 - 2 → 5
* multiply 7 * 2 → 14
/ divide · always a float 7 / 2 → 3.5
// quotient 7 // 2 → 3
% remainder 7 % 2 → 1
** power 7 ** 2 → 49

// and % come as a pair: 258 s is 258 // 60 = 4 min and 258 % 60 = 18 s · ^ is not power

Recap · 4

Multiply before you add. When unsure, parentheses.

  1. 1 parentheses ( )
  2. 2 power **
  3. 3 * / // %
  4. 4 + -

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

Recap · 5

Python runs what you wrote, not what you meant.

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

Recap · 6

0.1 + 0.2 is not 0.3. That is not a bug.

>>> 0.1 + 0.2

0.30000000000000004

>>> 1 / 3

0.3333333333333333

why

  • a computer stores numbers in base 2
  • 0.1 has no exact form in base 2, like 1/3 in base 10
  • every float is an approximation, usually a very good one
Quiz

What comes out?

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

Exercise 1

Five problems

Solved live, from the empty file.

  1. 1 How many seconds are in one year? (365 days)
  2. 2 500 minutes: how many hours, and how many minutes? Use // and %.
  3. 3 A video is 9271 seconds long. Print hours, minutes, and seconds.
  4. 4 Three resistors in parallel: 150 Ω, 220 Ω, 330 Ω. The formula is in the file.
  5. 5 The tens digit of 7482, using // and %.

An error is good news.

A silent wrong answer is the worst case.

Errors

Loud or silent?

loud · Python stops and tells you

  • print(500 // 60 SyntaxError
  • 1 / 0 ZeroDivisionError
  • prnt(500 // 60) NameError

silent · it runs, and the answer is wrong

  • 2 ^ 10 8
  • 500 / 60 8.333…
  • 1 / 1/150 + 1/220 + 1/330 0.0142…
  • print(1,000) 1 0
Habits

Two habits

  1. 1 Read the error from its last line
  2. 2 Ask: does the answer make sense?
three resistors in parallel: below 150 Ωone year: about 30 million secondshours from 500 minutes: a whole number

Next week

Week 3: Strings and Data Types

Videos and exercise 2: up Tuesday–Wednesday.
Exercise 2: due before next Monday's class.

Slides: hyeongminlee.github.io/slides/python-programming

← All decks
01 / 00
Scroll · ↓ · Space