Python Programming · Week 3
Hyeongmin Lee · SeoulTech, Dept. of Electronic Engineering
The same steps as last week.
This video is all typing. Pause whenever you need to catch up.
One line, Enter, read the answer. Then the next one.
>>> a = 3
>>> a = a * 2
>>> a
>>> b = "3"
>>> b * 2
>>> int(b) * 2
>>> type(b)
then look back
Which answers are strings?
the ones the prompt shows with quotes:'33' is a string, 6 is a number
On purpose. At the prompt, first type name = "Kim".
>>> print(Name)
>>> "20" + 5
>>> int("3.5")
>>> print("Hello, {name}")
three loud errors: good news · one silent wrong answer: the worst case · read an error from its last line
In your week03 folder: the seven lines from the theory video. Run it and answer in the terminal.
name = input("Name: ")
age = int(input("Age: "))
days = age * 365
line = f"{name}, you are about {days} days old."
print("-" * len(line))
print(line)
print("-" * len(line)) Name: Kim
Age: 20
---------------------------------
Kim, you are about 7300 days old.
---------------------------------
run againtype twenty for the age · ValueError: read its last line
In a new file: temperature.py · F = C × 9 / 5 + 32
c = float(input("Celsius: "))
fahr = c * 9 / 5 + 32
print(f"{c} C is {fahr:.1f} F") Celsius: 36.5
36.5 C is 97.7 F
In a new file: current.py · I = V / R, and 1 A = 1000 mA
v = float(input("Voltage (V): "))
r = float(input("Resistance (ohms): "))
print(f"Current: {v / r * 1000:.2f} mA") Voltage (V): 5
Resistance (ohms): 220
Current: 22.73 mA
In a new file: title.py · the line under the title is exactly as long as the title
title = input("Title: ")
print(title)
print("=" * len(title)) Title: Python Programming
Python Programming
==================
All in one file: week03_exercise.py, on e-Class.
typed Kim
typed 7, 4
typed 175
typed 5, 0.2
typed Kim
Next steps
Before class. Everything you need is on e-Class.
See you in class.