Skip to content
Back to course

2. Variables and data types

In the last lesson you learned to print text on screen. Now we go one step further: storing information so your program can remember and reuse it. This is done with variables. By the end of this lesson you will know what a variable is, how to create one, and the four main data types Python uses every day.

A variable is a named container that holds a piece of information. Think of it like a labelled jar in Almaz's kitchen: one jar says "salt", another says "sugar". You do not need to remember the exact amount inside — you just ask for the jar by its label.

In Python, you create a variable by writing a name, then an equals sign, then the value:

price = 500

name = "Abebe"

is_open = True

Now whenever you write price, Python replaces it with 500. Whenever you write name, Python gives you "Abebe".

Variable names can contain letters, numbers, and underscores (_), but they must start with a letter or underscore — not a number. Use descriptive names: shop_name is much clearer than x.

In Python, = means "store this value", not "these two things are equal". So price = 500 means "put 500 into the box called price". To check if two things are equal, Python uses == (two equals signs). You will see == when we cover conditions in a later lesson.

Every value in Python has a type that tells Python what kind of information it is. The four types you will use most often are:

1. int (integer) — whole numbers, positive or negative.

telebirr_balance = 1200

items_sold = 47

2. float (floating-point number) — numbers with a decimal point.

exchange_rate = 57.35

weight_kg = 2.5

3. str (string) — any text, enclosed in quotes.

city = "Addis Ababa"

customer = 'Sara Haile'

4. bool (boolean) — only two possible values: True or False.

shop_is_open = True

payment_received = False

Python figures out the type automatically from the value you give — you do not have to declare it. This is called dynamic typing.

Once you have a variable, you can use it directly inside print() — no quotes needed around the variable name:

balance = 350

print(balance) # prints: 350

You can also do arithmetic with int and float variables:

price = 80

quantity = 3

total = price * quantity

print(total) # prints: 240

To mix text and a variable in one print(), use an f-string — put f before the opening quote, then wrap variable names in curly braces {}:

name = "Dawit"

amount = 1500

print(f"Dear {name}, your TeleBirr balance is {amount} birr.")

# prints: Dear Dawit, your TeleBirr balance is 1500 birr.

F-strings are one of the most useful Python features for beginners — they make messages easy to read.

Variables can change — that is why they are called variables. You simply assign a new value to the same name:

stock = 20

print(stock) # 20

stock = stock - 5 # a customer just bought 5 items

print(stock) # 15

The right side is calculated first, then stored back into stock. This pattern — taking the current value, doing some arithmetic, and saving the result — is used constantly in real programs.

Python also offers shorthand operators to make this neater:

stock += 10 # same as: stock = stock + 10

stock -= 3 # same as: stock = stock - 3

stock *= 2 # same as: stock = stock * 2

For example, Abebe runs a small phone-accessories shop in Addis Ababa. He can track his inventory with just a few lines of Python, updating stock every time he makes a sale.

Strings (text values) have their own useful operations:

• Concatenation (+) — join two strings together:

greeting = "Selam, " + "Sara!"

print(greeting) # Selam, Sara!

• Repetition (*) — repeat a string multiple times:

line = "-" * 20

print(line) # --------------------

• len() — count how many characters a string has:

city = "Addis Ababa"

print(len(city)) # 11

• upper() / lower() — change case:

print(city.upper()) # ADDIS ABABA

print(city.lower()) # addis ababa

These tools let you build, format, and inspect text — essential for any program that works with names, messages, or user input.

Scenario

Sara keeps a small shop inventory in Python. She writes: apples = "30" apples = apples + 10 print(apples) When she runs this code, what happens?

Lesson recap: • A variable is a named container that stores a value — create one with name = value. • Python has four core data types: int (whole numbers), float (decimal numbers), str (text), bool (True/False). • Python determines the type automatically — this is called dynamic typing. • Use f-strings (f"...") to mix text and variable values in one print() call. • Variables can be updated: stock = stock - 1 or with shorthand stock -= 1. • type() tells you the type of any variable; int(), str(), float() convert between types. • Strings support +, *, len(), .upper(), and .lower().

Check your understanding

1/7 · 82 XP

Abebe wants to store his shop name in a variable. Which of the following is the correct Python syntax?