Skip to content
Badr Academy Education
العربية
Badr Academy Education

Browse by topic

Programming

Variables and data types in Python: a practical explanation with examples

How Python stores information: variables, numbers, strings, booleans, lists, dictionaries and type conversion – with examples and exercises.

  • 8 min read
Variables and data types in Python: a practical explanation with examples

Every program you use deals with information: a weather app deals with temperatures, an online shop with prices and product names, and a learning platform with students’ names and marks. To work with that information, a program needs somewhere to keep it and a way to know what kind of information it is. That is exactly what variables and data types provide in Python. In this article we explain the topic step by step with short examples you can try yourself, then finish with exercises and common mistakes, so that you come away with real understanding rather than memorised rules.

What you will learn

  • What a variable is, how to create one, and the rules for naming it.
  • The basic data types: integers, decimals, strings and booleans.
  • Collections: lists, dictionaries, tuples and sets.
  • Type conversion, beginners’ mistakes, and exercises to practise.

What is a variable?

Picture a variable as a box with a name label, in which you put a value to use later. In Python you create a variable simply by giving it a value with the equals sign:

student_name = "Mariam"
age = 14
average = 91.5

Here we created three variables. The = sign in programming does not mean “equals” as in mathematics; it means “store the value on the right in the variable on the left”. That is why you can write age = age + 1 – a sentence that makes no sense in maths, but in programming means: take the current value, add one, and store the result in the same variable.

Rules for naming variables

  • A name starts with a letter or an underscore, never a digit.
  • It contains no spaces; use an underscore instead, as in total_score.
  • Python is case-sensitive, so Age and age are different variables.
  • Do not use the language’s reserved words, such as if, for and class.
  • Choose clear names that describe the content. price is better than p, and student_count is better than x.

The basic data types

Every value in Python has a type that decides what you can do with it. You do not need to write the type yourself – Python infers it from the value – and you can find it with the type() function.

Type Python name Example Used for
Integer int 25, -3, 0 Ages, quantities and counting
Decimal number float 3.14, 91.5 Prices, averages and measurements
String (text) str “Ahmed”, ‘Python’ Names, messages and addresses
Boolean bool True, False Yes-or-no answers: did the student pass?
No value NoneType None Representing the absence of a value

Numbers and arithmetic

Python supports the usual operations: addition +, subtraction -, multiplication * and division /. There are three useful operations many beginners do not know: floor division //, which gives the result without the fraction; the remainder %; and the power **. For example, 17 // 5 gives 3, 17 % 5 gives 2, and 2 ** 10 gives 1024. Notice that ordinary division / always gives a decimal number, even when there is no remainder.

Strings

A string is a sequence of characters between single or double quotation marks. You can join strings with the plus sign, repeat them with the multiplication sign, and find their length with len(). The newest and clearest way to put variables inside text is the formatted string, or f-string:

name = "Mariam"
score = 91.5
print(f"{name} scored {score} marks")

Booleans

A boolean is either True or False, and it usually comes from comparisons such as score >= 50. These values are the basis of decision-making in a program with the if statement.

Collections: when you need more than one value

What if you want to store the marks of thirty students? You will not create thirty variables. Python provides types that group several values:

  • List: an ordered, changeable collection, such as scores = [90, 75, 88]. You add to it with append, and reach the first item with index zero: scores[0].
  • Dictionary (dict): key–value pairs, such as student = {"name": "Mariam", "age": 14}. You reach a value by its key: student["name"].
  • Tuple: like a list but unchangeable once created – suitable for fixed values such as coordinates.
  • Set: values with no duplicates and no order, useful for removing duplicates quickly.
1One value?A number, a string ora boolean2A list of items?Use a list when theorder matters3Named data?Use a dict when youlook values up byname4Fixed values?Use a tuple for datathat never changes5No duplicates?Use a set to removerepeats1One value?A number, a string or a boolean2A list of items?Use a list when the order matters3Named data?Use a dict when you look values up byname4Fixed values?Use a tuple for data that neverchanges5No duplicates?Use a set to remove repeats
How to choose the right data type for your information

An example makes the choice clear: if you want to store Mariam’s marks in five subjects in the order of the tests, a list fits. But if you want to find the mark for a particular subject by name, such as maths or science, a dictionary is better, because you write scores["math"] instead of remembering that maths is the third item. Choosing the right type makes code shorter, clearer and less prone to mistakes.

What happens in memory? Names and values

The box picture is useful at the start, but in Python the truth is slightly more precise: a variable is a name that points to a value held in memory – more like a label stuck on a thing than a box containing it. The difference shows when you write b = a and a is a list: the list is not copied; you now have two names pointing to the same list. If you add an item through b, you will also see it when you print a. This behaviour puzzles many beginners, and it connects to an important idea: some types are mutable, such as lists, dictionaries and sets, and some are immutable, such as numbers, strings and tuples. When you “change” a string, Python is really creating a new one. And if you want an independent copy of a list, use b = a.copy().

Converting types

One of the most familiar situations: you ask the user for their age with input(), then try to add one to it and get an error. The reason is that input() always returns a string, even if the user typed a number. The fix is conversion:

age = int(input("How old are you? "))
print("Your age next year:", age + 1)

The functions int(), float() and str() convert between types. But be careful: trying to convert text such as “five” into a number causes an error, because Python does not understand numbers written in words. In real programs we handle that possibility so the program does not stop at a wrong entry.

If you would like a detailed walkthrough of these basics and more, the full course from freeCodeCamp.org covers them thoroughly:

Video: “Learn Python – Full Course for Beginners [Tutorial]” – freeCodeCamp.org on YouTube (in English).

Common beginner mistakes

  • Adding a string to a number: "Age: " + 14 causes an error. Use str(14) or an f-string.
  • Forgetting quotation marks: writing name = Mariam makes Python look for a variable called Mariam.
  • Confusing = with ==: the first stores a value; the second compares.
  • Forgetting that indexing starts at zero: the first item in a list is [0], not [1].
  • Naming a variable after an existing function: such as list or print, which hides the original function.

Exercises to practise

  1. Write a program that asks for the user’s name and year of birth, then prints their approximate age in a friendly message.
  2. Create a list of five marks and calculate their total and average with sum() and len().
  3. Create a dictionary for a book with its title, author and number of pages, and print a sentence describing it.
  4. Write a program that converts a temperature from Celsius to Fahrenheit using the formula F = C × 9 ÷ 5 + 32.

Once you have mastered variables, the next step is organising your code into functions. If you have not installed Python yet, start with why start programming with Python. In the Python course at Badr Academy Education we practise these ideas with graded exercises and review from the instructor. For further resources visit the learning library, and for a complete plan try the Find your path tool.

The takeaway

A variable is a name that points to a value, and every value has a type that decides what you can do with it. Get to know the basic types and collections, and remember that input() always returns a string and that indexing starts at zero. Try every example yourself and change it – experimenting is the fastest route to understanding.

© 2026 Badr Academy Education – this content is exclusive to Badr Academy Education. Short quotations are welcome with the source and a link to this page.

Share this article

Comments (0)

You can comment without an account; your comment appears after review, and the academy may edit it before publishing.

Add a comment

Your e-mail will not be published. Required fields are marked *

No comments yet – be the first to join in.