Variables and Data Types in Python, Explained Simply
A beginner-friendly walkthrough of Python variables and basic data types, with runnable examples based on the official Python tutorial.
Before anything else makes sense in programming, you need a clear mental model of what a variable is and why a value's type matters. Python is a good language to learn this with because it lets you try these ideas directly in an interactive prompt, one line at a time, and see the result immediately.
What a variable actually is
MDN's glossary defines a variable plainly: "A variable is a named reference to a value. That way an unpredictable value can be accessed through a predetermined name." In Python, you create one with the equals sign:
width = 20
height = 5 * 9
print(width * height)
Running this prints 900. The Python tutorial points out something that trips up many beginners: = here is not a math equals sign. It means "store this value under this name," not "these two things are equal." After this code runs, width refers to 20 and height refers to 45, and asking for width * height retrieves both values and multiplies them.
If you try to use a name that was never assigned a value, Python raises an error immediately rather than guessing:
>>> n
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
This is intentional. Python will not let you read a variable that doesn't exist yet, which catches a whole category of typos early.
Numbers have more than one type
Python distinguishes between whole numbers and numbers with a decimal part, and this distinction matters for how calculations behave. The official tutorial shows the two basic numeric types directly:
price = 100.50
tax = 12.5 / 100
print(price * tax)
Running this prints 12.5625. Here, price is a float (it has a decimal part) and so is the result of 12.5 / 100. You can check a value's type directly with the built-in type() function:
print(type(20)) # <class 'int'>
print(type(100.50)) # <class 'float'>
Division in Python always returns a float, even when the numbers divide evenly, which is a common surprise for new learners:
print(17 / 3) # 5.666666666666667
print(17 // 3) # 5 (floor division: drops the decimal part)
print(17 % 3) # 2 (the remainder)
Running these three lines confirms the pattern: / always gives you a float, // gives you the whole number part only, and % gives you what's left over. This remainder operator comes up constantly in beginner exercises, such as checking whether a number is even (n % 2 == 0).
Text has its own type
A sequence of characters in Python is a str (short for string), and you create one with either single or double quotes:
name = "Ada"
print(type(name)) # <class 'str'>
Strings and numbers don't mix automatically with +. Trying to add a string and a number directly raises an error:
>>> '2' + 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
This is a deliberate design choice in Python: unlike some other languages, it will not silently guess whether you meant to add the number 2 or the text character "2." You have to convert one to match the other, usually with str(2) or int('2'), depending on what you're trying to do.
True and False are their own type too
Python has a dedicated type for logical values, called bool, with exactly two possible values:
is_ready = True
print(type(is_ready)) # <class 'bool'>
You'll see this type constantly in if statements and comparisons (width > 10 evaluates to a bool), even if you never type the word True or False directly very often.
Why knowing the type matters in practice
Most beginner bugs trace back to a type mismatch: trying to do math on something that's actually text (often because it was typed in from user input, which always arrives as a string), or trying to combine text and numbers with + without converting first. Running type() on a value you're unsure about is one of the fastest ways to debug this, and it costs nothing to add temporarily while you're figuring out what went wrong.
Key takeaways
- A variable is a name that refers to a value;
=assigns a value to a name, it does not test for equality. - Using an undefined variable raises a
NameErrorimmediately rather than failing silently. - Python has separate numeric types,
intfor whole numbers andfloatfor decimals; division with/always returns a float. //gives floor division (whole number result) and%gives the remainder, both useful for everyday beginner exercises.- Strings (
str) and numbers do not combine with+without an explicit conversion; mixing them raises aTypeError. type()tells you exactly what kind of value you're working with, which is the fastest way to debug a confusing error.