What you will learn
- Create and update variables.
- Use strings, integers, floating-point numbers, and Boolean values.
- Use
input()and convert text to numeric types. - Format readable output with f-strings.
The code from this chapter
Type these programs yourself. The predictions below are the exercises; the explanations are in the book.
2.1 Variables Are Names for Values
Type this into a file, predict the output, then run it.
name = "Maya"
age = 16
score = 92.5
active = True
print(name)
print(age)
print(score)
print(active)
TPRM Lab 2.1: Values That Look Similar
Type this into a file, predict the output, then run it.
a = 5
b = "5"
print(a)
print(b)
print(type(a))
print(type(b))
2.2 input() Always Gives You Text
TPRM Lab 2.2: A Small Age Calculator
Type this into a file, predict the output, then run it.
name = input("Your name: ")
age = int(input("Your age: "))
next_age = age + 1
print(f"{name}, next year you will be {next_age}.")
2.4 f-strings: Putting Values into Text
TPRM Lab 2.4: Build a Receipt Line
Type this into a file, predict the output, then run it.
item = "Notebook"
price = 4.5
quantity = 2
print(f"{quantity} x {item} = ${price * quantity:.2f}")
print(f"Next year you will be {16 + 1}.")