Output and input
Two tools you will use from the very first line you write: print() shows values in the terminal, input() gets text from the user. They are simple, but knowing how they behave saves you from a handful of surprises early on.
How Python runs your code
Python runs your code from top to bottom, one line at a time, in exactly the order you wrote it. No jumping around. The order you write things is the order they run. Always.
city = "Tokyo"
print(city)
print("Population: 14 million")city gets assigned first. The first print runs. The second print runs. Every time, in that order.
This matters because you cannot use a variable before you have assigned it. Python has not seen it yet and will raise an error:
print(country) # NameError: country is not defined yet
country = "Japan"Keep this in mind as your programs grow: anything you use must be defined before you use it.
Printing output
print() is how Python talks back to you. Pass it any value and it displays that value. It converts whatever you give it to text automatically.
print("Hello") # Hello
print(42) # 42
print(3.14) # 3.14
print(True) # TrueMultiple values
You can print multiple values at once by separating them with commas. Python puts a space between them by default. Change the separator with sep:
name = "Alice"
age = 28
print(name, age) # Alice 28
print("Name:", name) # Name: Alice
print("2024", "01", "15", sep="-") # 2024-01-15
print("a", "b", "c", sep=", ") # a, b, cControlling the line ending
Each print() call ends with a newline by default, so the next output starts on a fresh line. Change that with end. Setting end="" makes the next print continue on the same line:
print("Loading", end="")
print("...")
# Loading...
print("one", end=" | ")
print("two", end=" | ")
print("three")
# one | two | threeFormatting output with f-strings
The cleanest way to build messages is f-strings. Put f before the opening quote, then wrap any variable or expression in curly braces. Python fills it in at runtime. You can put any value, calculation, or method call inside the {}.
name = "Alice"
score = 980
# concatenation: clunky, requires str() for numbers
print("Player: " + name + ", Score: " + str(score))
# f-string: readable, no manual conversion
print(f"Player: {name}, Score: {score}")You can put any expression inside {}: arithmetic, method calls, format specs:
price = 49.99
tax = 0.2
total = price * (1 + tax)
print(f"Total: {total:.2f}") # Total: 59.99
print(f"Name: {name.upper()}") # Name: ALICE
print(f"2 + 2 = {2 + 2}") # 2 + 2 = 4The format spec after : controls how the value is displayed:
ratio = 0.8765
count = 1234567
label = "revenue"
print(f"{ratio:.1%}") # 87.7%
print(f"{count:,}") # 1,234,567
print(f"{label:>12}") # " revenue":.2f means "two decimal places". You will use it constantly for prices and measurements. Everything else is there when you need it. The main thing: anything can go inside {}, not just variable names.
Getting input from the user
input() pauses your program and waits for the user to type something. Whatever they type (and press Enter on) comes back as the return value. The string in the parentheses is the prompt the user sees.
name = input("What's your name? ")
print(f"Hello, {name}!")input() always returns a string, no matter what the user types. Type 42 and you get back "42", not the number 42. To do arithmetic with it, convert explicitly:
age = int(input("How old are you? "))
print(f"In ten years you'll be {age + 10}.")What if the user types something that cannot be converted? Python raises a ValueError. Handling that properly is covered in the Files and exceptions chapter.
In practice
A quiz that personalises itself from user input:
name = input("What's your name? ")
subject = input("Which subject? ")
print(f"Okay, {name}. Starting your {subject} quiz.")
print("Good luck!")Both inputs come back as strings and go directly into the f-strings. No conversion needed because you are using them as text, not numbers.

