Lesson 3 – Variables

A variable is a name used to store information in a program. Think of it as a box with a label. The label is the variable name, and the information inside the box is the value. Your program can open the box at any time to read or change the value stored inside.

Why Do We Use Variables?

Variables allow your program to remember information and use it again later. Instead of writing the same number or text repeatedly, you store it once in a variable.

Example 1: Storing Text

name = "Khalid"
print("Your name is:", name)
        

Output:

Your name is: Khalid

Example 2: Storing Whole Numbers

age = 38
print("Age:", age)
        
Age: 38

Example 3: Storing Decimal Values

height = 1.75
print("Height:", height)
        
Height: 1.75

Example 4: Changing a Variable

score = 10
print("Score:", score)

score = 20
print("Updated score:", score)
        
Score: 10
Updated score: 20
        

Example 5: Adding Variable Values

a = 5
b = 7
total = a + b
print("Total:", total)
        
Total: 12

Variable Naming Rules


Quiz: Test Your Understanding

1. What is a variable?





Correct answer: A box that stores information

2. Which variable name is correct?





Correct answer: student_age

3. Which variable stores text?





Correct answer: name = "Aisha"

4. What happens when you assign a new value to a variable?





Correct answer: The old value is replaced

5. Which variable name is invalid?





Correct answer: my variable

6. Which example stores a decimal?





Correct answer: height = 1.65

7. Which is true about variables?





Correct answer: They can store different data types

8. Which statement is correct?





Correct answer: Age and age are different

9. Which example shows reassignment?





Correct answer: x = 5; x = 9

10. What is the value of x after this?

x = 4
x = x + 6
            




Correct answer: 10
Back to Top