Computer Science Researcher • Educator • Programming Instructor
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.
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.
name = "Khalid"
print("Your name is:", name)
Output:
Your name is: Khalid
age = 38
print("Age:", age)
Age: 38
height = 1.75
print("Height:", height)
Height: 1.75
score = 10
print("Score:", score)
score = 20
print("Updated score:", score)
Score: 10
Updated score: 20
a = 5
b = 7
total = a + b
print("Total:", total)
Total: 12
Age is different from age)if, for, class)1. What is a variable?
2. Which variable name is correct?
3. Which variable stores text?
4. What happens when you assign a new value to a variable?
5. Which variable name is invalid?
6. Which example stores a decimal?
7. Which is true about variables?
8. Which statement is correct?
9. Which example shows reassignment?
10. What is the value of x after this?
x = 4
x = x + 6