Lesson 2
Variables & Data Types
Learn how to store information in variables and understand the basic data types used in Python.
What is a variable?
A variable is a name that refers to a value. You can use variables to store information in your program.
For example, you can store someone's name and age in variables:
name = "Vee"
age = 17
Here, name refers to the text
"Vee", and age refers to
the number 17.
You can then use those variables later in your program.
name = "Vee"
print(name)
The output is:
Vee
Creating a variable
To create a variable, write a variable name,
followed by the = sign, followed by a value.
name = "Vee"
age = 17
city = "New York"
The = sign assigns a value to a variable.
In this example, Python stores "Vee"
under the name name.
You can create as many variables as you need.
name = "Vee"
age = 17
favorite_color = "red"
print(name)
print(age)
print(favorite_color)
The output is:
Vee
17
red
Changing a variable
A variable can refer to a different value later in your program.
age = 17
print(age)
age = 18
print(age)
The output is:
17
18
After age = 18, the variable
age refers to 18.
Naming variables
Choosing clear variable names makes your code easier to understand.
Python variable names can contain letters, numbers, and underscores.
first_name = "Vee"
age2 = 17
favorite_color = "red"
A variable name cannot start with a number.
This is incorrect:
2age = 17
A variable name also cannot contain spaces. Use an underscore instead.
favorite_color = "red"
Python is case-sensitive, so these are different variable names:
name = "Vee"
Name = "Kate"
It is also a good idea to choose names that describe the information they contain.
age = 17
is easier to understand than:
x = 17
What are data types?
A data type describes what kind of value you are working with.
Python has many data types. In this lesson, we will focus on four basic types:
int— whole numbersfloat— numbers with a decimal pointstr— textbool— true or false values
Understanding data types is important because different types of values can be used in different ways.
Integers — int
An integer is a whole number without a decimal point.
age = 17
year = 2026
temperature = 10
Positive numbers, negative numbers, and zero can all be integers.
positive = 9
negative = -69
zero = 0
Floating-point numbers — float
A floating-point number, usually called a float, is a number that contains a decimal point.
height = 1.59
temperature = 14.5
price = 199.99
Floats are useful when you need values that are not whole numbers.
Strings — str
A string is a sequence of characters used to represent text.
name = "Vee"
city = "New York"
message = "Hey!"
Strings are usually written inside single or double quotation marks.
name = "Vee"
name = 'Vee'
Both examples create a string containing the text
Vee.
Booleans — bool
A Boolean is a value that can be either
True or False.
is_student = True
is_tired = False
Booleans are especially useful when a program needs to make decisions. You will use them more when we learn about conditions.
Notice that True and False
start with capital letters. They are special Python
values.
Checking a data type with type()
Python has a built-in function called
type() that tells you what type of value
you are working with.
age = 17
print(type(age))
The output is:
<class 'int'>
You can also check other values:
name = "Vee"
height = 1.59
is_student = True
print(type(name))
print(type(height))
print(type(is_student))
Python will show that these values are
str, float, and
bool.
Basic arithmetic
Variables can store numbers that you can use in calculations.
boys = 3
girls = 17
total = boys + girls
print(total)
The output is:
20
Python supports several basic arithmetic operators:
+— addition-— subtraction*— multiplication/— division
a = 29
b = 5
print(a + b)
print(a - b)
print(a * b)
print(a / b)
Common beginner mistakes
Variables are simple once you understand the basic rules, but beginners often make a few common mistakes.
Starting a variable name with a number:
1name = "Vee"
This is not a valid Python variable name.
Using spaces in a variable name:
first name = "Vee"
Use an underscore instead:
first_name = "Vee"
Confusing strings and numbers:
age = "17"
age = 17
These two values look similar, but they have different data types. The first is a string, while the second is an integer.
Practice
Try these tasks before moving to the mini task.
Task 1: Create variables for your name, age, and favorite hobby. Print all three variables.
Task 2: Create two number variables and print their sum, difference, and product.
Task 3:
Create one variable for each of these data types:
int, float, str,
and bool. Use type() to
check each one.
Mini Task: About Me
Create a Python program that stores information about yourself in variables.
Your program should have variables for your name, age, favorite hobby, and whether you are currently learning Python.
Then use print() to display the values.
Try to choose clear variable names and use the correct data type for each value.