Python

Variables, values, and basic types

The absolute starting point — what a variable actually is in Python, the handful of types you'll use constantly, and how to tell them apart.

Beginner

4 min read

A variable is a name pointing at a value

age = 25
name = "Ada"

age = 25 doesn't create a labeled box called age containing the number 25 — it creates the value 25 somewhere, then makes the name age point at it. This distinction matters later (it's why some behaviors around copying and mutation work the way they do), but for now the practical version is enough: = means "make this name refer to this value," read right to left. Names are case-sensitive (age and Age are different), can contain letters, numbers, and underscores, and can't start with a number.

The four types you'll use constantly

age = 25              # int — a whole number
price = 19.99          # float — a number with a decimal point
name = "Ada"            # str — text, in quotes
is_student = True      # bool — True or False, capitalized, no quotes

Every value in Python has a type, and you can always check it:

type(age)          # <class 'int'>
type(price)         # <class 'float'>
type(name)          # <class 'str'>
type(is_student)    # <class 'bool'>

int and float are both numbers, but they're different types for a reason: int is exact (whole numbers, no rounding involved), while float can represent fractions but with occasional tiny rounding imprecision — 0.1 + 0.2 prints 0.30000000000000004, not 0.3, because of how computers store decimal fractions in binary. This rarely matters for everyday code, but it's why you don't compare floats with == when exactness matters.

Strings: text, and what you can do with it

first = "Ada"
last = "Lovelace"
full = first + " " + last     # "Ada Lovelace" — + joins strings together
greeting = f"Hello, {first}!"  # "Hello, Ada!" — f-strings insert values into text

The f before the quote turns a string into an f-string — anything inside {} gets evaluated and inserted directly into the text. This is the standard, modern way to build a string that includes a variable's value; string concatenation with + works too but gets unwieldy fast once more than one or two values are involved.

name = "ada"
name.upper()       # "ADA"
name.capitalize()  # "Ada"
len(name)          # 3 — the number of characters

Strings come with built-in methods (functions attached to the value itself, called with a dot) for common operations — capitalizing, searching, splitting, and dozens more, all covered as they come up in later lessons.

Numbers: the operators, and one surprising one

7 + 3    # 10
7 - 3    # 4
7 * 3    # 21
7 / 3    # 2.3333333333333335  — always produces a float
7 // 3   # 2   — floor division: divide, then round down to a whole number
7 % 3    # 1   — modulo: the remainder left over after division
7 ** 3   # 343 — exponent: 7 to the power of 3

/ always returns a float, even when the numbers divide evenly (10 / 2 is 5.0, not 5) — this is a deliberate Python 3 design choice, so division never silently loses information. // and % are the pair worth remembering: // is "how many whole times does this fit," % is "what's left over" — together they show up constantly (checking if a number is even with n % 2 == 0, splitting a total number of seconds into minutes and seconds, and so on).

Converting between types

str(25)        # "25"   — int to str
int("25")      # 25     — str to int
float("3.14")  # 3.14   — str to float
int(3.99)      # 3      — float to int, truncates (doesn't round!) toward zero

A very common real bug for beginners: reading numeric-looking input (from a file, a form, input()) gives you a str, not a number — "5" + "3" is "53" (string concatenation, not addition), while int("5") + int("3") is 8. If a calculation isn't producing the number you expect, checking type() on the values involved is the first thing worth trying.

Further reading

Check your understanding

A quick comprehension check — not tracked, not graded, just for you.

1. What does `10 / 2` evaluate to in Python?

2. What is the type of the value produced by `int("5") + int("3")`?

3. What does `int(3.99)` return?

4. Which of these correctly inserts the value of `name` into a string?