BeginnerLesson firstCategory 3 of 20

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.

10 Sections5 Exercises

After reading

Practice Arena

Begin with the first exercise, then continue step by step through the module.

Start with Assign Variables

Study 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.

python
name = "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.

python
country = "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:

  • str for text
  • int for whole numbers
  • float for decimal numbers
  • bool for True or False
python
student_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.

python
city = "Cairo"

Whole numbers are int values.

python
year = 2026

Decimal numbers are float values.

python
temperature = 36.5

A very common beginner mistake is forgetting quotes around text.

python
name = 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.

python
user_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, or class

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.

python
width = 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.

python
score = 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().

python
x = 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.

python
data = 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.

python
a = 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.