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
Assignment and Augmented Assignment
📖 Scenario: You are managing a small shop's daily sales. You want to keep track of the total sales amount as you add new sales throughout the day.
🎯 Goal: Build a simple program that starts with an initial sales amount, then adds more sales using assignment and augmented assignment operators, and finally shows the total sales.
📋 What You'll Learn
Create a variable to hold the initial sales amount.
Create a variable to hold the new sales amount.
Use an assignment operator to set the initial total sales.
Use an augmented assignment operator to add new sales to the total.
Print the final total sales amount.
💡 Why This Matters
🌍 Real World
Tracking sales or any running total is common in shops, restaurants, and many businesses.
💼 Career
Understanding assignment and augmented assignment is essential for managing data and calculations in programming jobs.
Progress0 / 4 steps
1
Create initial sales variables
Create a variable called initial_sales and set it to 150. Then create a variable called new_sales and set it to 75.
Python
Hint
Use simple assignment with = to set the values.
2
Set total sales with assignment
Create a variable called total_sales and assign it the value of initial_sales using the assignment operator =.
Python
Hint
Use = to copy the value from initial_sales to total_sales.
3
Add new sales using augmented assignment
Use the augmented assignment operator += to add new_sales to total_sales.
Python
Hint
The += operator adds the right side value to the left side variable.
4
Print the total sales
Write a print statement to display the value of total_sales.
Python
Hint
Use print(total_sales) to show the final total.
Practice
(1/5)
1.
What does the += operator do in Python?
easy
A. Adds the right value to the left variable and updates it
B. Assigns a new variable without changing the old one
C. Subtracts the right value from the left variable
D. Creates a new list from two variables
Solution
Step 1: Understand the meaning of +=
The += operator adds the value on the right to the variable on the left.
Step 2: Recognize it updates the variable
It changes the original variable by adding the new value to it.
Final Answer:
Adds the right value to the left variable and updates it -> Option A
Quick Check:
+= means add and assign [OK]
Hint: Think: add then store back in same variable [OK]
Common Mistakes:
Confusing += with simple assignment =
Thinking += creates a new variable
Mixing += with subtraction or multiplication
2.
Which of the following is the correct syntax to add 5 to variable x using augmented assignment?