One-sentence summary
A variable is a named box that stores a piece of information, and the type of that information (a whole number, a decimal, text, or true/false) decides how the program can use it.
Why does it matter?
In your first program you printed a fixed message with print("Hello"). But real programs store information, change it, and calculate with it. A game keeps your score, an app remembers your name, a robot records a distance reading.
Underneath all of this sits the variable. Writing a real program is very hard without understanding variables and their types. This lesson lays the foundation that every later lesson will build on.
What is a variable?
A variable is a label that gives a name to a value. Imagine putting the value in a box and writing a name on the box. Later you say that name to reach whatever is inside.
In Python we assign a value using the equals sign (=).
name = "Doruk"
age = 13
print(name)
print(age)
This program prints:
Doruk
13
Here the = sign does not mean "is equal to." It means "put the value on the right into the name on the left." So age = 13 means "put 13 into the age box." Python runs these lines one by one from top to bottom, so you must assign a value to a variable before you use it.
Changing a value
A variable is called "variable" because the value inside it can change. When we assign again, the old value is replaced.
score = 0
print(score)
score = 10
print(score)
You will see 0 first and then 10. The second line overwrote the first.
Meaningful names
You can give a variable any name you like, but good names make code readable. Writing age instead of x, or student_count instead of a, keeps your code clear even weeks later.
A few rules apply:
- A name starts with a letter or an underscore (
_); it cannot start with a digit. - Spaces are not allowed; words are joined with
an_underscore. - Upper and lower case differ:
nameandNameare two different variables.
birth_year = 2013
current_year = 2026
Stick to plain lowercase letters and underscores to keep names easy to read. Well-chosen names make the code explain itself, even without adding a comment.
Basic data types
Every value has a type. The type says what the value is and what you can do with it. Four types are enough to begin.
int — whole number
int (integer) holds whole numbers with no decimal part: 0, 13, -5, 2026.
age = 13
pencil_count = -4
float — decimal number
float holds decimal (fractional) numbers. The decimal separator is a dot, not a comma.
height = 1.62
temperature = 36.5
str — text (string)
str (string) holds text. We wrap text in quotes, single ('...') or double ("...").
name = "Doruk"
city = 'Izmir'
Notice: "13" is text, while 13 is a number. Quotes turn a value into text.
bool — true/false
bool (boolean) takes only two values: True or False. The first letter is capital.
passed_exam = True
is_raining = False
bool will be very useful for making decisions (conditions) in later lessons.
Finding the type: type()
If you are unsure of a variable's type, use the type() function.
name = "Doruk"
age = 13
height = 1.62
print(type(name))
print(type(age))
print(type(height))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
type() tells you the type of the value inside the box. It is very helpful when you are debugging.
Converting between types
Sometimes we need to change one type into another. For example, we cannot add a number to text that only looks like a number; we must convert it first. This is called type conversion.
int(...)converts a value to a whole number.float(...)converts a value to a decimal number.str(...)converts a value to text.
Converting text to a number
text_age = "13"
number_age = int(text_age)
future_age = number_age + 5
print(future_age)
This program prints 18. If we had not converted the text "13" with int(), we could not have added 5 to it.
Converting a number to text
To join a number with a piece of text, we first convert it to text with str().
age = 13
message = "My age is " + str(age)
print(message)
Output: My age is 13. If we had written just age instead of str(age), Python would give an error because it cannot add text and a number together.
Mini practice
Now let us combine what we have learned. We will write a small program that stores a person's name and birth year, then calculates their age.
name = "Doruk"
birth_year = 2013
current_year = 2026
age = current_year - birth_year
print("Name: " + name)
print("Age: " + str(age))
Output:
Name: Doruk
Age: 13
Three different types work together here: name is a str, while birth_year and current_year are each an int. When printing the age, we converted the result to text with str().
Try it yourself: change name and birth_year to your own, run the program, and check that it shows the correct age.
Common mistakes
Forgetting the quotes
name = Doruk
Without quotes, Python thinks Doruk is a variable name and gives a NameError. Text is always wrapped in quotes: name = "Doruk".
Trying to add text and a number
age = 13
print("Age: " + age)
This line gives a TypeError because text and a number cannot be joined directly. The fix is to use str(age).
Using a comma in a decimal
Writing height = 1,62 does not give the result you want; Python reads it as two separate values. Use a dot in decimals: height = 1.62.
Choosing unclear names
Names like a, x1, or test2 make code hard to read. Choose meaningful names such as age and birth_year.
Safety note
This lesson runs entirely on your own computer with code you write yourself, so there is no hazard. As a good habit, though: only run code that you wrote or that you trust. Also, never put real personal information such as passwords, addresses, or phone numbers into your code; use nicknames and sample values while learning.
Review questions
- Why should a variable name describe meaning rather than only its data type?
- How do integer, float, string and Boolean values differ?
- What happens when input() is used without converting a number-like string?
- Why can floating-point calculations produce surprising decimal results?
- How would you test a variable used as a sensor threshold?
- When is a constant-style name useful in a beginner project?
Answers
- Meaningful names explain the role of the value and remain useful if the implementation changes.
- Integers hold whole numbers, floats approximate decimals, strings hold text and Booleans represent true/false decisions.
- The value remains text, so arithmetic may fail or perform string operations such as joining characters.
- Many decimal fractions cannot be represented exactly in binary floating-point form.
- Test a value below the threshold, exactly at it and above it, including the smallest meaningful difference.
- It signals a value intended not to change during normal execution, such as a pin number or fixed safety limit.
Lesson summary
- A variable is a label that gives a name to a value; we assign with
=. - Meaningful names (
age,birth_year) keep code readable and clear. - There are four basic types:
int(whole number),float(decimal),str(text),bool(true/false). type()shows the type of a value and helps with debugging.- With
int(),float(), andstr()we can convert one type into another.
Check your understanding
- What does the
=sign mean in the linescore = 100? - What is the type of the value
1.62:int,float,str, orbool? - What is the difference between
"7"and7? - When
age = 13, why doesprint("Age: " + age)cause an error, and how do you fix it? - What is the type of the variable
passed_exam = True?
Answers
- It means "put the value on the right into the name on the left," so the value
100is placed into the variablescore. It is not the "equals" of mathematics. float, because it is a decimal (fractional) number."7"is text (str) because it is inside quotes;7is a whole number (int). You cannot do arithmetic with text, so you must first convert it withint("7").- Text (
str) and a number (int) cannot be joined directly, so it gives aTypeError. To fix it, convert the number to text:print("Age: " + str(age)). bool, because its value isTrueorFalse.
Source and verification note
For “Variables and Data Types”, verification focuses on whether the relationship between What is a variable? and Meaningful names remains consistent across examples. Code examples follow Python 3 syntax. Small differences may appear between environments, so examples should first be tested in a safe online editor or a local development setup.
Next lesson
Getting Input from the User: We will learn to collect information from the user with the input() function, store it in variables, and convert the incoming text into the correct type.