Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Assignment operators are used to store a value in a variable.
The most basic assignment operator in Python is:
For example:
This means:
20 in the variable age.You can then use the variable:
Output:
= means "put this value into this variable."Look at this example:
You can think of it like this:
The value "Rahul" is stored in name.
Another example:
Now marks contains the value 85.
Python also provides shortcuts for updating a variable.
For example, suppose:
Now you want to add 5.
You could write:
This works perfectly.
But Python gives us a shorter way:
Both statements do the same thing.
Here are the assignment operators you will commonly see in Python:
| Operator | Meaning | Example | Same As |
|---|---|---|---|
= | Assign | x = 10 | — |
+= | Add and assign | x += 5 | x = x + 5 |
-= | Subtract and assign | x -= 5 | x = x - 5 |
*= | Multiply and assign | x *= 5 | x = x * 5 |
/= | Divide and assign | x /= 5 | x = x / 5 |
//= | Floor divide and assign | x //= 5 | x = x // 5 |
%= | Modulus and assign | x %= 5 | x = x % 5 |
**= | Power and assign | x **= 2 | x = x ** 2 |
Let's understand them one by one.
= — Basic AssignmentThe = operator assigns a value to a variable.
Now:
Another example:
Now city contains:
= is used to assign or store a value.+= — Add and AssignThe += operator adds a value to the current variable value.
Output:
It is the same as:
Initially:
Then:
Python does:
So now:
Suppose you have ₹500 and receive another ₹200:
Output:
+= means add to the current value.-= — Subtract and AssignThe -= operator subtracts a value from the current variable value.
Output:
It is the same as:
Suppose your wallet contains ₹1,000 and you spend ₹250:
Output:
-= means subtract from the current value.*= — Multiply and AssignThe *= operator multiplies the current value and stores the new result.
Output:
It is the same as:
Another example:
Output:
*= means multiply the current value./= — Divide and AssignThe /= operator divides the current value and stores the result.
Output:
It is the same as:
/= means divide the current value.//= — Floor Divide and AssignThe //= operator performs floor division and stores the result.
Output:
It is the same as:
//= means perform floor division and store the result.%= — Modulus and AssignThe %= operator finds the remainder and stores it in the variable.
Output:
It is the same as:
Because:
%= means find the remainder and store it.**= — Power and AssignThe **= operator calculates a power and stores the result.
Output:
It is the same as:
Because:
**= means raise the current value to a power.Compound assignment is useful when you want to update the same variable again and again.
For example:
Output:
The variable keeps changing:
This makes compound assignment especially useful when a value needs to be updated.
Do not confuse:
with:
For assignment, use:
The operator == is used for comparison and belongs to a different topic.
For this lesson, remember only:
= → assign a value:=)Python also has another special assignment operator:
It is called the walrus operator.
The walrus operator allows you to assign a value and use that value in the same expression.
Here:
stores 20 in age.
At the same time, the value 20 is used in the condition.
So the program prints:
The symbol:
looks a little like the eyes and tusks of a walrus.
That's why Python developers commonly call it the walrus operator.
Consider this example:
Here, the assignment and use happen separately.
With the walrus operator, assignment can happen inside an expression.
For example:
The value "Rahul" is assigned to name, and then name is used by the print() statement.
Output:
:= everywhere. Normal assignment with = is still the most common way to assign a value.= vs += vs :=These operators have different purposes.
| Operator | Simple Meaning |
|---|---|
= | Assign a value |
+= | Add and update |
-= | Subtract and update |
*= | Multiply and update |
/= | Divide and update |
//= | Floor divide and update |
%= | Find remainder and update |
**= | Calculate power and update |
:= | Assign and use the value in the same expression |
Think of them like this:
Now practice each assignment operator.
Expected output:
Expected output:
Expected output:
Expected output:
Expected output:
Expected output:
Expected output:
Start with:
Perform these updates:
Try to write the Python code using compound assignment operators.
Output:
Assignment operators are used to store and update values.
The most important operators are:
+= do?-= do?%= do?**= do?:=?Before moving to the next topic, make sure you can explain these in your own words:
Try writing a few examples yourself. The more you practice these operators, the more natural they will become.
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Arithmetic Operators | Comparison Operators |
Test your understanding of this lesson with 9 questions. Each question has one correct answer.