English
/

Variables and Data Types in Python, Explained Simply

Last edited Nov 6, 2026 · 3 min read

A Python variable is a name that refers to a value; = assigns that value, and type() tells you its type. The type determines how operations such as addition behave, including whether text and numbers need conversion.

On this page▾
  1. What a variable actually is
  2. Numbers have more than one type
  3. Text has its own type
  4. True and False are their own type too
  5. Why knowing the type matters in practice

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 says the equal sign (=) is used to assign a value to a variable. 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.

Using True and False to choose what runs is covered in If, Elif, and Else in Python, How Programs Make Decisions.

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.

Sources

  1. Python Software Foundation, The Python Tutorial, An Informal Introduction to Python
  2. Python Software Foundation, The Python Standard Library, Built-in Types
  3. MDN Web Docs, Glossary: Variable

About this guide

Howlane operator · Checked against the official sources listed above. It is general information, not professional advice.

AboutEditorial standardsCorrectionsReport an error

Edit history

  • Updated
  • Updated
  • Updated
  • Updated
  • Published