Python Programming · Week 3
Hyeongmin Lee · SeoulTech, Dept. of Electronic Engineering
last week · a calculation
>>> 20 * 365
7300
this week · a program
Name: Kim
Age: 20
---------------------------------
Kim, you are about 7300 days old.
---------------------------------
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
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
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
the name keeps its value until you store a new one · variable: its value can change
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
>>> 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
letters, digits, and _
no digit first · no spaces · no hyphens
not a Python word
capital letters 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 _
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
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
"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
>>> "Kim" + "!"
'Kim!'
+ joins strings
>>> "-" * 10
'----------'
* repeats a string
>>> len("Kim Lee")
7
len() counts characters · the space counts
>>> "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
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
The user typed 20.
age = input("Age: ") print(age * 3)
print(age + 1)
digits typed by the user are still a string · "20" is not 20
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
What int() and float() can change.
int() needs a whole-number string
float() also takes a decimal point
int() of a float cuts the decimal part
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
An f before the quote. Values go straight into the braces.
print(f"{name}, you are about {days} days old.") print(f"{days * 24} hours") anything inside the braces is calculated
print("{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()
>>> 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
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.
---------------------------------
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
Have Python open. Type everything with me.
See you in class.