Python Programming · Week 3

Variables, Strings, and Input/Output:
Practice

Hyeongmin Lee · SeoulTech, Dept. of Electronic Engineering

Setup

Before we start

The same steps as last week.

  1. 1 VS Code → open your python_course folder
  2. 2 New Folder → week03
  3. 3 Terminal → python3 → the three arrows

This video is all typing. Pause whenever you need to catch up.

Drill

At the prompt: type these

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

Drill

Break four things

On purpose. At the prompt, first type name = "Kim".

>>> print(Name)

NameError loud

>>> "20" + 5

TypeError loud

>>> int("3.5")

ValueError loud

>>> print("Hello, {name}")

Hello, {name} silent

three loud errors: good news · one silent wrong answer: the worst case · read an error from its last line

Files

Now a file: card.py

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

Example · 1

Celsius to Fahrenheit

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

Example · 2

Current through a resistor

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

Example · 3

Underline a title

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

==================

Exercise 2

The problems: 1–3

All in one file: week03_exercise.py, on e-Class.

  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 * *******
Exercise 2

How to submit

  1. 1 download week03_exercise.py from e-Class
  2. 2 put it in python_course/week03 · open it in VS Code
  3. 3 write the code · save · run it once and answer its questions in the terminal
  4. 4 upload the same file on e-Class · the deadline is in the post
wrong answers: no points lostlate: points lostsolutions: only in classAI: allowed, but try alone first

Next steps

Now: exercise 2.

Before class. Everything you need is on e-Class.

See you in class.

← All decks
01 / 00
Scroll · ↓ · Space