Values, variables and printing
Numbers, text, booleans, and how to store and show them.
Python reads your code line by line. Save a file as hello.py and run it with python3 hello.py, or type python3 alone to try lines interactively.
print("Hello, world")
name = "Ana"
age = 31
print(name, "is", age)Variables and types
A variable is a name that points to a value. You do not declare its type: Python works it out.
| Type | Example | Notes |
|---|---|---|
int | 42 | Whole numbers, any size |
float | 3.14 | Decimal numbers |
str | "text" | Text, single or double quotes |
bool | True, False | Capital first letter |
None | None | Means "no value" |
Operators
7 + 2 # 9
7 / 2 # 3.5 (division always gives a float)
7 // 2 # 3 (floor division)
7 % 2 # 1 (remainder)
2 ** 10 # 1024 (power)Text with f-strings
price = 19.5
print(f"Total: ${price * 2:.2f}") # Total: $39.00NoteUse type(x) to see a value's type and input("Your name: ") to read text typed by the user. input always returns a string.
Test yourself
Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.