Unit 1 • Lesson 5

Variables and Data Types

Overview

Variables are containers that store data in your program. You'll learn how to create variables, assign values to them, and understand Python's basic data types: integers, floats, strings, and booleans.

Beginner 15–20 min

What You Will Learn in This Lesson

By the end of this lesson, you will know:

  • What variables are and why we use them: Understand how variables store and organize data in your programs.
  • How to create and assign variables: Learn the syntax for creating variables and storing values.
  • Python's basic data types: Master integers, floats, strings, and booleans.
  • Variable naming rules: Learn what makes a good variable name and what Python allows.
  • How Python infers types: Understand how Python automatically determines what type of data you're working with.

Why Variables Matter

Variables are like labeled boxes. Instead of remembering "the number 42" or "the text 'Hello'", you can give them names like age or greeting. This makes your code readable and allows you to reuse values throughout your program.

Step 1: What Are Variables?

A variable is a named container that stores a value. Think of it like a labeled box. You put something in the box (a value) and write a label on it (the variable name). Later, you can use the label to get what's inside.

Real-World Analogy

Imagine a filing cabinet. Each drawer has a label (variable name) and contains papers (the value). You can open "Age" drawer to get the number 25, or "Name" drawer to get "Alice".

In Python

You create a variable by giving it a name and assigning a value using the equals sign =. Python stores the value and remembers it by the name you gave it.

age = 25
name = "Alice"

Key Concept: The equals sign = in Python means "assign" or "store", not "equals" like in math. When you write age = 25, you're saying "store the value 25 in a variable called age", not "age equals 25".

Step 2: Creating Your First Variable

Creating a variable in Python is simple. You write the variable name, then an equals sign, then the value you want to store:

Basic Variable Assignment
name = "Python"
number = 42
price = 19.99
1

Choose a Name

Pick a descriptive name that tells you what the variable stores. Use lowercase letters and underscores for multiple words.

student_name = "Alice"
2

Use the Equals Sign

The equals sign = tells Python "store this value in this variable".

student_name = "Alice"
3

Provide the Value

Give Python the actual data you want to store. This can be a number, text, or other types of data.

student_name = "Alice"

Mini Practice #1: Create Variables

Try It Yourself

Try creating some variables. Notice how Python stores them and you can use them later:

Press Run to see output

What happened? You created two variables (name and age), stored values in them, and then printed those values. Variables let you store data and use it whenever you need it.

Step 3: Python's Basic Data Types

Python has several built-in data types. Each type represents a different kind of data. Understanding these types helps you write correct code and avoid errors.

1. Integers (int)

Whole numbers without decimals. Can be positive, negative, or zero.

age = 25
count = -5
zero = 0

2. Floats (float)

Numbers with decimal points. Used for measurements, prices, and calculations requiring precision.

price = 19.99
temperature = -5.5
pi = 3.14159

3. Strings (str)

Text data enclosed in quotes. Can use single or double quotes.

name = "Alice"
message = 'Hello'
text = "Python is fun"

4. Booleans (bool)

True or False values. Used for conditions and logic.

is_student = True
is_adult = False

Python Infers Types Automatically

Unlike some languages, you don't need to tell Python what type a variable is. Python figures it out automatically based on the value you assign. This is called "type inference".

x = 5        # Python knows this is an int
y = 3.14     # Python knows this is a float
z = "Hello"  # Python knows this is a string

Mini Practice #2: Different Data Types

Try It Yourself

Try creating variables with different data types. Notice how Python handles each one:

Press Run to see output

Notice: Each variable stores a different type of data. Python automatically knows what type each one is based on the value you assigned.

Step 4: Variable Naming Rules

Python has specific rules for naming variables. Following these rules ensures your code works correctly and is readable.

1

Rules You Must Follow

  • Variable names can contain letters, numbers, and underscores
  • Must start with a letter or underscore (not a number)
  • Cannot use Python keywords (like if, for, print)
  • Case-sensitive: age and Age are different variables
2

Good Naming Practices

  • Use descriptive names that explain what the variable stores
  • Use lowercase letters
  • For multiple words, use underscores: student_name
  • Keep names short but clear
✅ Good Variable Names
student_name = "Alice"
age = 25
total_price = 19.99
is_active = True
❌ Bad Variable Names
n = "Alice"        # Too short, unclear
studentName = 25   # Should use underscores
2age = 25          # Can't start with number
if = 5             # Can't use keyword

Step 5: Using Variables

Once you create a variable, you can use it anywhere in your code. You can print it, use it in calculations, or assign it to another variable.

Using Variables in Calculations
price = 10
quantity = 3
total = price * quantity
print(total)  # Prints 30
Changing Variable Values
age = 25
print(age)  # Prints 25
age = 26    # Change the value
print(age)  # Prints 26

Important: When you assign a new value to a variable, the old value is replaced. Variables can change throughout your program, which makes them powerful tools for storing data that might change.

Mini Practice #3: Using Variables

Try It Yourself

Try using variables in calculations and see how they work:

Press Run to see output

What happened? You created variables, used them in a calculation, and printed the result. This shows how variables make your code flexible and reusable.

End-of-Lesson Exercises

Exercise 1: Create Variables

Create three variables: one integer, one float, and one string. Then print all three.

Remember: integers are whole numbers, floats have decimals, and strings are text in quotes.

Write your code above and click "Check Answer" to verify it's correct.

Exercise 2: Use Variables in Calculation

Create two variables: price (set to 15.50) and quantity (set to 4). Calculate and print the total cost.

Multiply price by quantity to get the total.

Write your code above and click "Check Answer" to verify it's correct.