Variables help us store and remember information in a program. They let us use names to keep data so we can use or change it later.
Why variables are needed in Python
Introduction
Syntax
Python
variable_name = value
You choose a name for the variable to describe what it holds.
The equals sign (=) means you are giving the variable a value.
Examples
age.Python
age = 25name.Python
name = "Alice"score variable at zero, useful for games.Python
score = 0Sample Program
This program saves a name and age, then shows a greeting using those variables.
Python
name = "Bob" age = 30 print(f"Hello, {name}! You are {age} years old.")
Important Notes
Variable names should be clear and easy to understand.
You can change the value stored in a variable anytime.
Variables help make programs flexible and easier to update.
Summary
Variables store information to use later in a program.
They make programs easier to read and change.
Using variables is like giving a name to a box where you keep something.
Practice
1. Why do we use variables in a Python program?
easy
Solution
Step 1: Understand the role of variables
Variables hold data so the program can use or change it later.Step 2: Compare options with variable purpose
Only storing information matches what variables do; others are unrelated.Final Answer:
To store information that can be used later -> Option DQuick Check:
Variables store data for later use [OK]
Hint: Variables hold data for reuse later in code [OK]
Common Mistakes:
- Thinking variables speed up the program
- Confusing variables with functions
- Believing variables print output directly
2. Which of the following is the correct way to create a variable named
age with the value 25 in Python?easy
Solution
Step 1: Recall Python variable assignment syntax
Python usesvariable = valueto assign values.Step 2: Check each option
age = 25 matches correct syntax; B reverses assignment; C uses walrus operator incorrectly here; A is Java style.Final Answer:
age = 25 -> Option BQuick Check:
Variable assignment uses = sign correctly [OK]
Hint: Use variable = value to assign in Python [OK]
Common Mistakes:
- Putting value before variable
- Using := instead of = for simple assignment
- Using Java or other language syntax
3. What will be the output of this code?
name = "Anna"
print("Hello, " + name)medium
Solution
Step 1: Understand variable value and string concatenation
The variablenameholds "Anna". The print joins "Hello, " and the value ofname.Step 2: Predict the printed result
Concatenation results in "Hello, Anna" being printed.Final Answer:
Hello, Anna -> Option AQuick Check:
Concatenate string + variable value = Hello, Anna [OK]
Hint: Variables replace names with stored values in output [OK]
Common Mistakes:
- Printing variable name as text instead of value
- Forgetting to concatenate strings
- Expecting an error from string + variable
4. Find the error in this code:
number = 10 print(numer)
medium
Solution
Step 1: Compare variable names used
The variable is namednumber, butprintusesnumer, which is undefined.Step 2: Identify error type
This typo causes a NameError becausenumerdoes not exist.Final Answer:
Variable name typo causes error -> Option CQuick Check:
Variable names must match exactly [OK]
Hint: Check variable spelling carefully to avoid NameError [OK]
Common Mistakes:
- Assuming print needs quotes for variables
- Thinking assignment operator is wrong
- Believing print syntax is incorrect
5. You want to calculate the total price of 3 items costing 10, 15, and 20 dollars. Which code correctly uses variables to do this?
hard
Solution
Step 1: Check variable assignments before use
Variablesprice1,price2, andprice3must be assigned values before adding.Step 2: Analyze each option
price1 = 10 price2 = 15 price3 = 20 total = price1 + price2 + price3 print(total) assigns prices first, then sums them. total = 10 + 15 + 20 print(total) sums constants directly (no variables for prices). print(10 + 15 + 20) prints sum directly without variables. total = price1 + price2 + price3 price1 = 10 price2 = 15 price3 = 20 print(total) sums variables before assigning them, causing error.Final Answer:
price1 = 10 price2 = 15 price3 = 20 total = price1 + price2 + price3 print(total) -> Option AQuick Check:
Assign variables before use, then sum [OK]
Hint: Assign variables before using them in calculations [OK]
Common Mistakes:
- Using variables before assigning values
- Adding numbers directly without variables
- Confusing order of assignment and calculation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
