Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why Variables Are Needed
📖 Scenario: Imagine you are organizing a small party and need to keep track of how many guests have arrived. Instead of remembering the number in your head, you write it down on a piece of paper. This piece of paper is like a variable in programming.
🎯 Goal: You will create a simple program that uses a variable to store the number of guests and then updates and shows this number.
📋 What You'll Learn
Create a variable to store the number of guests
Create a variable to store the number of new guests arriving
Update the total number of guests using these variables
Print the final number of guests
💡 Why This Matters
🌍 Real World
Variables are like labels on boxes that help you organize and keep track of things in everyday life.
💼 Career
Understanding variables is essential for all programming jobs because they let you store and manage data in your programs.
Progress0 / 4 steps
1
Create a variable to store the initial number of guests
Create a variable called guests and set it to 5 to represent the initial number of guests who have arrived.
Python
Hint
Think of guests as the paper where you write down the number 5.
2
Create a variable for new guests arriving
Create a variable called new_arrivals and set it to 3 to represent new guests arriving at the party.
Python
Hint
This variable will help you add more guests to the total later.
3
Update the total number of guests
Add the value of new_arrivals to guests and store the result back in guests to update the total number of guests.
Python
Hint
This is like writing the new total number of guests on your paper.
4
Print the final number of guests
Write a print statement to display the value of guests.
Python
Hint
This will show how many guests are at the party now.
Practice
(1/5)
1. Why do we use variables in a Python program?
easy
A. To create new Python functions
B. To make the program run faster
C. To print text on the screen
D. To store information that can be used later
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 D
Quick 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
A. int age = 25
B. age = 25
C. age := 25
D. 25 = age
Solution
Step 1: Recall Python variable assignment syntax
Python uses variable = value to 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 B
Quick 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
A. Hello, Anna
B. Hello, name
C. Hello,
D. Error
Solution
Step 1: Understand variable value and string concatenation
The variable name holds "Anna". The print joins "Hello, " and the value of name.
Step 2: Predict the printed result
Concatenation results in "Hello, Anna" being printed.
Final Answer:
Hello, Anna -> Option A
Quick 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
A. Missing quotes around number
B. Assignment operator is wrong
C. Variable name typo causes error
D. Print statement syntax error
Solution
Step 1: Compare variable names used
The variable is named number, but print uses numer, which is undefined.
Step 2: Identify error type
This typo causes a NameError because numer does not exist.
Final Answer:
Variable name typo causes error -> Option C
Quick 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
A. price1 = 10
price2 = 15
price3 = 20
total = price1 + price2 + price3
print(total)
B. print(10 + 15 + 20)
C. total = price1 + price2 + price3
price1 = 10
price2 = 15
price3 = 20
print(total)
D. total = 10 + 15 + 20
print(total)
Solution
Step 1: Check variable assignments before use
Variables price1, price2, and price3 must 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 A
Quick Check:
Assign variables before use, then sum [OK]
Hint: Assign variables before using them in calculations [OK]