Code Safari

Chapter 146·Beginner·9 min read

Variables and Data Types in Python

How Python remembers information — variables as named labels for values, the core data types (int, float, str, bool), dynamic typing, and how to convert between types. The vocabulary every Python program is built from.

July 27, 2026

In the first chapter you made Python say something. But a program that can't remember anything can't do much — it needs to hold onto values: a user's name, a running total, whether someone is logged in. Storing information under a name is the job of variables, and the different kinds of information — numbers, text, true/false — are data types. Together they're the raw vocabulary of every program you'll ever write. This chapter makes them second nature.

Variables: names for values

A variable is a name that refers to a value. You create one with a single = sign, which in Python means "assign" — not "equals" in the maths sense:

age = 25
name = "Ada"
price = 19.99

Read age = 25 as "let age refer to the value 25." From now on, wherever you write age, Python substitutes 25. The power is that variables can change — that's why they're called variable:

score = 0
score = score + 10   # score is now 10
score = score + 5    # score is now 15
print(score)         # 15

That middle line looks strange as maths (nothing equals itself plus ten), but as an instruction it's clear: "take the current score, add 10, and store the result back in score." This pattern — read a variable, compute, store it back — is one of the most common things you'll ever write.

The four core data types

Every value in Python has a type that determines what it is and how it behaves. Four types cover the vast majority of what a beginner touches:

TypeNameExampleHolds
intinteger25, -3, 1000whole numbers
floatfloating-point19.99, 3.14, -0.5numbers with decimals
strstring"Ada", 'hello'text
boolbooleanTrue, Falsea yes/no value

Integers (int) are whole numbers, and Python handles arbitrarily huge ones without complaint. Floats carry a decimal point, for anything measured or divided. Strings (str) are text — any characters inside single or double quotes (Python accepts either). Booleans (bool) are the simplest of all: just True or False, the foundation of every decision your code makes, which we'll build on heavily in the next chapter.

You can always ask Python a value's type with the built-in type():

print(type(25))       # <class 'int'>
print(type(19.99))    # <class 'float'>
print(type("Ada"))    # <class 'str'>
print(type(True))     # <class 'bool'>

Types decide behaviour

Here's why types matter so much in practice, and it's a genuine beginner "aha": the same operation does different things depending on the types involved. The clearest example is the + operator.

print(2 + 2)          # 4    — adds the numbers
print("2" + "2")      # 22   — joins the text
print("Ha" + "Ha")    # HaHa — joins the text

With numbers, + means add. With strings, + means join together (called concatenation). So "2" + "2" is "22", not 4, because those are strings — text that happens to look like numbers — and Python glues them. Mixing types often just fails: "age: " + 25 raises a TypeError, because Python won't guess whether you meant to add or to join. Recognising a value's type lets you predict what an operation will do, and that predictive power is a big step toward writing code that works the first time.

Dynamic typing: flexible, occasionally tricky

Coming from many other languages, one thing about Python stands out: you never declare what type a variable is. You don't say "this is an integer." You just assign a value, and Python figures out the type on its own. This is called dynamic typing, and it goes one step further — the same variable can hold different types at different times:

data = 42        # data is an int
data = "hello"   # now data is a str — perfectly legal
data = True      # now it's a bool

Python doesn't object. This flexibility keeps code short and is part of why Python feels so light to write. The flip side is that Python won't catch type mix-ups before running — a variable you think holds a number might actually hold text, and you'll only find out when a line fails. It's a trade-off: freedom while writing, and a bit more care needed while debugging.

Converting between types

Because types behave differently, you constantly need to convert a value from one type to another. Python provides a conversion function named after each type:

int("25")       # the string "25" → the number 25
float("3.14")   # the string "3.14" → the number 3.14
str(100)        # the number 100 → the string "100"

This matters more than it looks, for one everyday reason: input from users always arrives as text. When you ask someone to type a number, Python hands you a string, and you must convert it before doing maths:

raw = input("Enter your age: ")   # raw is a string, e.g. "30"
age = int(raw)                    # convert to a number
print(f"Next year you'll be {age + 1}")

Without the int() conversion, raw + 1 would fail — you can't add a number to text. This "convert on the way in" step is one of the most common things beginners forget, and now you won't.

Where this leaves us

You've got the vocabulary of Python: variables as named labels you can read and reassign, the four core types (int, float, str, bool), the way types decide what operations do, Python's flexible dynamic typing, and how to convert between types when you need to. That's the material every program is assembled from.

So far, though, our programs run in a straight line — every statement, top to bottom, every time. Real programs need to make decisions ("if the user is an adult…") and repeat work ("for each item in the list…"). Giving your code that power is the leap from a script to a program, and it's next: Control Flow — conditionals and loops.

Variables and Data Types in Python | Code Safari