Variables and Data Types: How Python Remembers and Labels Information

Assign names to values, learn the four core types (str, int, float, bool), use type() and f-strings, follow naming rules, and see how dynamic typing behaves in small programs you can run today.

Chapter 2 of 20 · Beginner · 25 min read · Free

From Day 1 to real programs

On Day 1 you ran instructions top to bottom — mostly literals inside print(). That is enough to see how Python executes code, but real programs remember things: a username, a score, whether a user is logged in, the price in a cart.

Variables are how you give those values a stable name. Data types describe what kind of value you are storing — text, whole numbers, decimals, or true/false. This chapter teaches both, in the order you will actually use them when you write your own scripts.

Assign your first variables

Creating a variable in Python is a single step: pick a name, use =, and give a value. The = symbol means assignment here — "take this value and attach it to this name" — not mathematical equality.

name = "Alex"
age = 25
height_metres = 1.75
print(name)

Output:

Alex

You now have three names Python can reuse anywhere below this point in the file: name, age, and height_metres. Change a value once at the top of your script, and every later line that uses the name sees the update.

Try it now

Create three variables: your own name (text), your city (text), and your favourite whole number. Print all three with separate print() calls, then again on one line using a single f-string (you will see f-strings in depth later this chapter).

The four types that cover most beginner programs

Python figures out the type of a value from how you write it. You do not declare types in advance — that idea is called dynamic typing. Still, you should recognise the four types you will use in almost every early program:

TypeWhat it holdsExamples
`str`Text (strings)`"hello"`, `"42"`, `"Alex"`
`int`Whole numbers`0`, `-3`, `1024`
`float`Numbers with a decimal part`3.14`, `-0.5`, `2.0`
`bool`Boolean: only two values`True`, `False` (capitalised)

The classic beginner trap: `"42"` and `42` are not the same thing. One is text; one is an integer. Python enforces that difference when you try to do math or combine values.

a = 10
b = 20
print(a + b)  # 30 — integer addition

x = "10"
y = "20"
print(x + y)  # "1020" — string concatenation

Output:

30
1020

Ask Python what type something is

When you are unsure — or debugging — call type() on any value or variable. The result uses Python's internal class names, which map directly to the types above.

score = 97
label = "Perfect"
ratio = 0.5
done = False

print(type(score))
print(type(label))
print(type(ratio))
print(type(done))

Output (exact class text can vary slightly by version):

<class 'int'>
<class 'str'>
<class 'float'>
<class 'bool'>

Reassignment and dynamic typing

A variable name is not locked to one type forever. You can point the same name at a new value — even a value of a different type. That flexibility is convenient while you sketch ideas; later, when you build larger programs, you will still want each name to mean one kind of thing for readability.

mood = "happy"
print(mood)

mood = 10
print(mood)

Output:

happy
10

Name variables so future-you understands them

Python only requires that names follow the language rules — but good names are documentation. Prefer clear snake_case over cryptic abbreviations.

Clear names: user_name, total_price, is_logged_in

Weaker names: x, data, tmp2 — fine for three-line experiments, confusing in real code.

Rules Python enforces:

  • Start with a letter or underscore, not a digit (user_1 is valid; 1user is not).
  • Names are case-sensitive (Score and score are different variables).
  • Do not use spaces inside a name; use underscores instead of first name.
  • Never shadow keywords such as print, if, for, or True — Python already means something by those tokens.

Convention for constants: use ALL_CAPS for values that should not change during a run, such as MAX_RETRIES = 3. Python will not stop you from reassigning them, but readers know your intent.

# Descriptive names + tuple unpacking (preview of a later chapter)
first_name, last_name, age = "Marcus", "Johnson", 30
print(f"{first_name} {last_name}, age {age}")

PI = 3.14159
print(f"PI to two decimals: {PI:.2f}")

Output:

Marcus Johnson, age 30
PI to two decimals: 3.14

f-strings: readable output without glue code

An f-string starts with f before the opening quote. Anything inside { } is evaluated as a Python expression and inserted into the string. This is the default choice for readable output in Python 3.

item = "notebook"
quantity = 3
unit_price = 4.50
total = quantity * unit_price

print(f"{quantity} x {item} @ {unit_price} each = {total:.2f}")

Output:

3 x notebook @ 4.5 each = 13.50

Put the chapter together: a tiny profile program

This program uses all four common types, clear names, and f-strings. Read it line by line before you run it — that habit is what turns syntax into understanding.

student_name = "Priya"
age = 19
gpa = 3.8
is_enrolled = True

print(f"Student: {student_name}")
print(f"Age: {age}, GPA: {gpa}")
print(f"Enrolled: {is_enrolled}")

Output:

Student: Priya
Age: 19, GPA: 3.8
Enrolled: True

Challenge before you move on

Write a short profile card for yourself: variables for favourite subject (string), study hours per day (float or int), and whether you prefer morning study (boolean). Print a short summary using one or more f-strings. If you want stretch goals, format hours to one decimal place ({hours:.1f}) and add a second boolean such as on_track_this_week.

Where this connects in the course

Variables hold information; operators (Day 3) let you combine and compare that information. After operators you will spend several chapters on richer values — strings, lists, dictionaries — then functions and objects. If anything in this lesson felt fuzzy, re-run the small code blocks by hand in your editor: typing beats reading for memory.

When you are ready: Continue to Day 3: Operators and Expressions. To revisit setup and how Python runs line by line, open Day 1: Introduction to Python. For extra reps, use the Python exercises hub and filter by variables and types.

Chapter navigation

Use the Previous and Next links at the bottom of this lesson. Previous returns you to Day 1; Next opens operators and expressions.

Frequently asked questions: Variables and data types

What is a variable in Python?

A variable is a name you choose that refers to a value stored in memory. When you write `score = 10`, Python remembers the number 10 and lets you use `score` anywhere instead of repeating the literal. Names make programs readable and easier to change later.

What is the difference between a string and an integer in Python?

A string (`str`) stores text — characters inside quotes such as `"42"`. An integer (`int`) stores whole numbers such as `42`. Python will happily add two integers, but adding a string to an integer without converting first raises a `TypeError`. That distinction trips up many beginners — always notice the quotes.

Does Python require you to declare variable types?

No. Python uses dynamic typing: the type comes from the value you assign, and you can reassign a name to a different type later (for example from text to a number). That flexibility helps when you are learning, but it also means you should still think carefully about what each variable is supposed to represent.

What are the rules for naming variables in Python?

Names may contain letters, digits, and underscores, but they cannot start with a digit. Names are case-sensitive (`total` and `Total` are different). Avoid Python keywords like `print` or `if` as variable names. The community convention for multi-word names is `snake_case` (words separated by underscores).

How do I print variables neatly in Python?

Use an f-string: `print(f"Score: {score}")`. The `f` prefix tells Python to substitute the current values of variables inside `{` `}`. This is the standard way to format readable output in modern Python 3.

What should I study after variables and data types?

Day 3 covers operators and expressions — how to combine and compare values you store in variables. After that, you will work with richer text (strings), collections (lists), and control flow (conditionals and loops).