Variables & Data Types
Understand how to store and work with different types of data. Read the lesson first, then move through the exercises in order.
After reading
Practice Arena
Begin with the first exercise, then continue step by step through the module.
Start with Assign VariablesStudy Material
Read the full lesson
Variables & Data Types
When you write a Python program, you often need the program to remember something. It may need to remember a name, an age, a score, a price, or the result of a calculation.
That is what variables are for.
A variable is a name that points to a value.
pythonname = "Lina" age = 25
Here, name points to the text "Lina", and age points to the number 25.
The = sign does not mean "is equal to" in the math-class sense. In Python, it means "store this value in this variable."
Creating variables
You create a variable by choosing a clear name and assigning a value.
pythoncountry = "Japan" price = 19.99 is_open = True
Python creates the variable as soon as it sees the assignment. You do not need to declare the type first.
This is one reason Python feels friendly to beginners.
The main beginner data types
In this module, the most important types are:
strfor textintfor whole numbersfloatfor decimal numbersboolforTrueorFalse
pythonstudent_name = "Maya" # str score = 87 # int height = 1.72 # float is_ready = True # bool
These types matter because Python treats them differently. You can do math with numbers, but text behaves in a different way.
Strings, integers, and floats
Text must be wrapped in quotes.
pythoncity = "Cairo"
Whole numbers are int values.
pythonyear = 2026
Decimal numbers are float values.
pythontemperature = 36.5
A very common beginner mistake is forgetting quotes around text.
pythonname = Alice
Python will not read that as text. It will think Alice is the name of another variable.
Naming variables clearly
Good names make code easier to read.
Use names that explain what the value means.
pythonuser_age = 18 book_title = "Python Basics"
A few simple rules:
- start with a letter or underscore
- use letters, numbers, and underscores only
- do not use spaces
- do not use Python keywords such as
if,for, orclass
For multi-word names, Python usually uses snake_case.
Using variables in calculations
Variables become useful very quickly because you can use them in expressions.
pythonwidth = 8 height = 5 area = width * height print(area)
This is better than writing raw numbers everywhere because the names explain what the values mean.
If you change width or height, the rest of the calculation still makes sense.
Reassigning a variable
A variable can be updated.
pythonscore = 10 score = 15 print(score)
After the second assignment, score now holds 15, not 10.
This matters because programs often change values while they run.
Checking a value's type
Python can tell you what type a value has by using type().
pythonx = 42 y = 3.14 z = "hello" print(type(x)) print(type(y)) print(type(z))
This is useful when you are learning and also when you are debugging.
If something behaves in a strange way, checking the type often explains why.
Dynamic typing in Python
Python is dynamically typed. That means a variable name can later point to a value of a different type.
pythondata = 100 print(type(data)) data = "hundred" print(type(data))
Python allows this. That flexibility is helpful, but it also means you need to pay attention to what each variable currently holds.
Swapping values
Sometimes you want two variables to exchange values.
Python has a clean way to do that.
pythona = 10 b = 20 a, b = b, a
This is a useful Python pattern, and it is one of the reasons Python code often looks compact and readable.
What this lesson should give you
After this lesson, you should understand how to:
- store information in variables
- choose a sensible variable name
- tell the difference between text and numbers
- inspect a value's type
- use variables in calculations
- understand that variables can be reassigned
These ideas are the foundation for almost every module that comes next.
Interactive
Exercises for this topic
These exercises follow the exact order of the lesson. Move step-by-step from reading into coding.
Assign Variables
Assign an integer, float, and string to variables and print them.
Swap Variables
Swap two variables without a temporary variable.
Rectangle Area
Calculate the area of a rectangle.
Type Inspection
Use type() to inspect variable types.
Dynamic Typing
Reassign a variable to a different type.