Python Programming · Week 3

Variables, Strings,
and Input/Output

Hyeongmin Lee · SeoulTech, Dept. of Electronic Engineering

This week

From a calculation to a program

last week · a calculation

>>> 20 * 365

7300

  • the numbers are typed into the code
  • only you know what 7300 means

this week · a program

Name: Kim

Age: 20

---------------------------------

Kim, you are about 7300 days old.

---------------------------------

  • anyone can run it, with their own name and age
  • it answers in a sentence

1 · it asks the user

input()

2 · it remembers the answers

variables

3 · it answers in words

strings and output

these three turn a calculation into a program · this week's topic

This week

The whole program: seven lines

asks

name = input("Name: ")

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

calculates

days = age * 365

answers

line = f"{name}, you are about {days} days old."

print("-" * len(line))

print(line)

print("-" * len(line))

seven lines · by the end of this video, you can read and write every one

Variables

Keeping a value

Seconds in a year, then minutes in a year: the same long calculation, typed twice.

print(365 * 24 * 60 * 60)
print(365 * 24 * 60 * 60 // 60)

better: calculate once, keep the answer, use it again

>>> seconds = 365 * 24 * 60 * 60

(nothing comes out)

>>> seconds

31536000

>>> seconds // 60

525600

seconds is a variable: a name that keeps a value

Variables

A variable is a name for a value

seconds 31536000
age 20 after age = 20

the name keeps its value until you store a new one · variable: its value can change

Variables

= means store

Not “is equal to”. Python works out the right side first, then stores it in the variable on the left.

>>> x = 5

>>> x = x + 1

>>> x

6

trace it by hand

line

right side

x

x = 5

5

5

x = x + 1

5 + 1 → 6

6

in math, x = x + 1 is impossible · in Python, it is an instruction

Variables

What does b hold?

>>> a = 3

>>> b = a

>>> a = 10

>>> b

3

trace it by hand

line

a

b

a = 3

3

b = a

3

3

a = 10

10

3

b = a stores the value a has at that moment · a new value for a does not change b

Variables

Rules for variable names

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
Variables

Good names matter

poor names

a = input("Name: ")
b = int(input("Age: "))
c = b * 365
d = f"{a}, you are about {c} days old."

good names

name = input("Name: ")
age = int(input("Age: "))
days = age * 365
line = f"{name}, you are about {days} days old."

Python runs both, with the same output · only good names tell a person what each value is

one more tip: lowercase words joined by _

good birth_year avoid BirthYear
Variables

Quotes or no quotes

After age = 20:

>>> print("age")

age

>>> print(age)

20

with quotes: text, printed as it is · without quotes: a variable, and Python uses its value

Strings

Text is a value too

A string: characters inside quotes.

two kinds of quotes

"Kim"   'Kim'

the same string

a quote inside the string

"It's"

use the other kind of quote outside

>>> "Kim"

'Kim'

>>> print("Kim")

Kim

the prompt shows a string with quotes · print shows it without quotes

Strings

Three kinds so far

"3" and 3 look alike on the screen. They are not the same.

>>> type(3)

<class 'int'>

>>> type(3.0)

<class 'float'>

>>> type("3")

<class 'str'>str is short for string

three kinds so far: int, float, str · type() tells you the kind

Strings

Joining, repeating, counting

>>> "Kim" + "!"

'Kim!'

+ joins strings

>>> "-" * 10

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

* repeats a string

>>> len("Kim Lee")

7

len() counts characters · the space counts

Strings

Strings and numbers do not mix

>>> "3" + "4"

'34'

>>> "3" + 4

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

read the last line: a string can only be joined to a string · a loud error is good news

Input

Asking the user: input()

A file, run with the Run button.

name = input("Name: ")
print(name)

the string inside input( ) is the question the user sees

terminal

Name: Kim

Kim

  1. 1the program stops and waits
  2. 2the user types in the terminal, then presses Enter
  3. 3what was typed is stored in name
Input

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

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

Input

Changing the kind

Put a value in parentheses after int, float, or str: you get the value as that kind.

between int and float

>>> float(3)

3.0

>>> int(3.9)

3cut, not rounded

between strings and numbers

>>> int("20")

20

>>> float("1.75")

1.75

>>> str(20)

'20'

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

from the inside out: input() gives the string "20" · int() turns it into 20 · = stores it in age

Input

Conversion rules

What int() and float() can change.

int() needs a whole-number string

int("20") 20 int("3.5") ValueError int("twenty") ValueError

float() also takes a decimal point

float("3.5") 3.5 float("20") 20.0

int() of a float cuts the decimal part

int(3.9) 3
Output

Building the answer with +

We want to print:

Kim, you are about 7300 days old.

name = "Kim"
days = 7300
print(name + ", you are about " + str(days) + " days old.")

1 variables give their values

"Kim" + ", you are about " + str(7300) + " days old."

2 str() gives a string

"Kim" + ", you are about " + "7300" + " days old." without str(): TypeError

3 + joins the strings

"Kim, you are about 7300 days old." print shows it without quotes

it works, but: four pieces, three plus signs, spaces inside the quotes, and str() around every number

Output

f-strings

An f before the quote. Values go straight into the 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("{name}, you are about {days} days old.")
{name}, you are about {days} days old.

no f: no error, the braces are printed as they are

one string · no plus signs · no str()

Output

Digits after the point

>>> 1 / 3

0.3333333333333333

>>> f"{1 / 3:.2f}"

'0.33'

>>> f"{2 / 3:.2f}"

'0.67'

after the colon, inside the braces

:.2f

two digits after the point, rounded

:.1f

one digit after the point

Output

A frame that fits

line = f"{name}, you are about {days} days old."
print("-" * len(line))
print(line)
print("-" * len(line))

len(line) is 33 · "-" * 33 draws 33 dashes

terminal

---------------------------------

Kim, you are about 7300 days old.

---------------------------------

This week

The whole program

name = input("Name: ")

input() asks and gives a string

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

int() turns the string into a number

days = age * 365

a new variable days · week 2 math

line = f"{name}, you are about {days} days old."

an f-string puts the values into the string

print("-" * len(line))

len() counts · * repeats

print(line)

shows what line holds

print("-" * len(line))

the same frame again

last week, 20 * 365: one answer · now, age * 365: a rule for any age

Next steps

Next: the practice video.

Have Python open. Type everything with me.

See you in class.

← All decks
01 / 00
Scroll · ↓ · Space