Python Programming · Week 3

Variables, Strings, and Input/Output:
In class

Hyeongmin Lee · SeoulTech, Dept. of Electronic Engineering

Today

Three things

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

= means store. Right side first.

A variable is a box with a name on it. = puts a value into the box.

the box x

x

5

after x = 5

→

x

6

after x = x + 1

a new value replaces the old one

right side first, then store

>>> x = 5

>>> x = x + 1

>>> x

6

trace it by hand

line

a

b

a = 3

3

–

b = a

3

3

a = 10

10

3

so b shows 3: b = a stored the value a had at that moment

a variable keeps its value until you store a new one · in math, x = x + 1 is impossible · in Python, it is an instruction

Recap · 2

Names: letters, digits, and _. Capital letters matter.

letters, digits, and _

agebirth_yearr2

no digit first · no spaces · no hyphens

2nd_valuemy namemy-name

not a Python word

ifforclassyou learn these later

capital letters matter

ageAgetwo different variables

a good name says what the value is: days, not d · lowercase words joined by _

Recap · 3

Quotes make a string.

with quotes, without quotes

>>> age = 20

>>> print("age")

age

>>> print(age)

20

two kinds of quotes

"Kim" · 'Kim'

the same string

"It's"

a quote inside: use the other kind outside

three kinds so far

>>> type(3)

<class 'int'>

>>> type(3.0)

<class 'float'>

>>> type("3")

<class 'str'>

with quotes: a string, shown as it is · without quotes: a variable, and Python uses its value · "3" and 3 look alike on the screen: type() tells you the kind

Recap · 4

Strings and numbers do not mix.

>>>"3" + "4"

'34'

+ joins strings

>>>"-" * 10

'----------'

* repeats a string

>>>len("Kim Lee")

7

len() counts characters · the space counts

>>>"3" + 4

TypeError

a string can only be joined to a string

the same +, a different meaning: numbers add, strings join · a loud error is good news: read its last line

Recap · 5

input() always gives a string.

the user typed 20

age = input("Age: ")

print(age * 3)

202020

no error, and a wrong answer: the worst case

print(age + 1)

TypeError

can only concatenate str (not "int") to str

age = int(input("Age: "))

from the inside out: input() gives "20", int() turns it into 20, = stores it

changing the kind

  • int("20") → 20
  • int("3.5") → ValueError
  • int("twenty") → ValueError
  • float("3.5") → 3.5
  • int(3.9) → 3 cut, not rounded

digits typed by the user are still a string · "20" is not 20

Recap · 6

f-strings: an f before the quote, values in braces.

print(f"{name}, you are about {days} days old.")

Kim, you are about 7300 days old.

print(f"{days * 24} hours")

175200 hours anything inside the braces is calculated

print(f"{1 / 3:.2f}")

0.33 :.2f · two digits after the point, rounded

print("Hello, {name}")

Hello, {name} no f: no error, the braces are printed as they are

one string · no plus signs · no str()

Quiz

What comes out?

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

1

>>>x = 4

>>>x = x * 2

>>>x

2

>>>a = 3

>>>b = a

>>>b = b + 1

>>>a

3

>>>"2" + "10"

4

>>>4 + "3"

5

>>>"4" * 2

>>>int("4") * 2

6

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

Number: 5

>>>n + n

7

>>>int(2.99)

8

>>>f"{2.99:.1f}"

predict first, then check · that is how you read code

Exercise 2

The problems: 1–3

Solved live, from the empty file.

  1. 1 Ask for the user's name. Print Hello, <name>! with an f-string.

    typed Kim

    Hello, Kim!
  2. 2 Ask for the width and the height of a rectangle (whole numbers). Print the area, then the perimeter.

    typed 7, 4

    Area: 28 Perimeter: 22
  3. 3 Ask for a length in cm. Print it in meters (2 digits after the point) and in inches (1 digit). 1 inch = 2.54 cm.

    typed 175

    1.75 m 68.9 in
Exercise 2

The problems: 4–5

  1. 4 Ask for a voltage (V) and a current (A). Print the power P = V × I in watts, 2 digits after the point.

    typed 5, 0.2

    1.00 W
  2. 5 Ask for a word. Print it inside a frame of stars that fits the word.

    typed Kim

    ******* * Kim * *******

Next week

Week 4: Comparisons and Conditionals

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

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

← All weeks
01 / 00
Scroll · ↓ · Space