Start Learning
Data Science
Future Tech
Accelerator Program in
Business Analytics and Data Science
In collaboration with
Certificate Program in
Financial Analysis, Valuation, & Risk Management
DevOps & Cloud Engineering
Strategic Management and Business Essentials
Assignment Operators in Python – A Comprehensive Guide
Updated on July 27, 2024
Ever felt confused about assigning values to variables in Python ? Wondering how to simplify operations like addition, subtraction, or even bitwise shifts?
Many of us face these questions when diving into Python programming .
Assignment operators in Python are here to make our lives easier. These operators help us assign values to variables in a clean and efficient way and make our code efficient and compact.
We’ll explore how these operators work and how we can use them to streamline our code.
Basic Assignment Operator
Let’s start with the basics.
The equal sign (=) is the simple assignment operator in Python. It assigns the value on its right to the variable on its left.
In this example:
- We assign the values 10 to a and 5 to b.
- We add a and b and assign the result to c.
- We print the value of c, which is 15.
Augmented Assignment Operators in Python
Now, let’s move on to augmented assignment operators.
These operators combine an operation with an assignment in one step. They make our code more concise and easier to read.
Addition Assignment Operator (+=)
How It Works: This operator would thus add to the right operand and assign the result to the left operand.
Here, a is initially 10.
The ‘+=’ operator adds 5 to a and assigns the result back to a. So, a becomes 15.
Subtraction Assignment Operator (-=)
How It Works: It subtracts the right operand from the left operand and assigns the result to the left operand.
Initially, a is 10.
The ‘-=’ operator subtracts 5 from a and assigns the result back to a. Thus, a becomes 5.
Multiplication Assignment Operator (*=)
How It Works: This is the operator for multiplication of the left operand by the right operand, with the result being assigned to the left operand.
The ‘*=’ operator multiplies a by 5 and assigns the result back to a. Hence, a becomes 50.
Division Assignment Operator (/=)
How It Works: It divides the left operand by the right and assigns the outcome to the left operand.
The ‘/=’ operator divides a by 5 and assigns the result back to a. Thus, a becomes 2.0.
Modulus Assignment Operator (%=)
How It Works: This operator divides the left operand by the right operand and assigns the remainder to the left operand.
The ‘%=’ operator divides a by 3 and assigns the remainder back to a. Therefore, a becomes 1.
Floor Division Assignment Operator (//=)
How It Works: It performs floor division of the left operand by the right operand and, therefore, modifies the value of the left operand.
The ‘//=’ operator divides a by 3 and assigns the floor value back to a. So, a becomes 3.
Exponentiation Assignment Operator (**=)
How It Works: The operator raises the left operand to the power of the right one and puts the result back into the left operand.
Initially, a is 2.
The ‘**=’ operator raises a to the power of 3 and assigns the result back to a. Therefore, a becomes 8.
Bitwise AND Assignment Operator (&=)
How It Works: It performs a bitwise AND operation on the operands and assigns the result to the left operand.
Initially, a is 5 (0101 in binary).
The ‘&=’ operator performs a bitwise AND with 3 (0011 in binary) and assigns the result back to a. Thus, a becomes 1 (0001 in binary).
Bitwise OR Assignment Operator (|=)
How It Works: This operator performs a bitwise OR operation on the operands and assigns the result to the left operand.
The ‘|=’ operator performs a bitwise OR with 3 (0011 in binary) and assigns the result back to a. So, a becomes 7 (0111 in binary).
Bitwise XOR Assignment Operator (^=)
How It Works: It performs a bitwise XOR operation on the operands and assigns the result to the left operand.
The ‘^=’ operator performs a bitwise XOR with 3 (0011 in binary) and assigns the result back to a. Thus, a becomes 6 (0110 in binary).
Bitwise Right Shift Assignment Operator (>>=)
How It Works: This operator performs a bitwise right shift on the left operand and assigns the result to the left operand.
Initially, a is 8 (1000 in binary).
The ‘>>=’ operator right shifts a by 2 and assigns the result back to a. So, a becomes 2 (0010 in binary).
Bitwise Left Shift Assignment Operator (<<=)
How It Works: It performs a bitwise left shift on the left operand and assigns the result to the left operand.
Initially, a is 3 (0011 in binary).
The ‘<<=’ operator left shifts a by 2 and assigns the result back to a. Therefore, a becomes 12 (1100 in binary).
Introducing the Walrus Operator (:=)
Ever wondered if there’s a way to simplify assignments within expressions?
Meet the Walrus Operator.
The Walrus Operator (:=) allows us to assign values to variables as part of an expression. It makes our code more concise and readable.
Imagine you’re iterating over a list and want to process elements until the list becomes short. The Walrus Operator lets us do this efficiently.
- n is assigned the length of a within the while loop condition.
- The loop runs until a has two or fewer elements.
- We reduce our code’s length without sacrificing clarity.
Practical Examples of Assignment Operators
Let’s dive into more practical examples of assignment operators in Python.
These examples will help us see how assignment operators make our code cleaner and more efficient.
Addition Assignment Operator (+=):
- We use the += operator to accumulate the sum of numbers from 0 to 4.
Subtraction Assignment Operator (-=):
- We use the -= operator to update the balance after each withdrawal.
Multiplication Assignment Operator (*=):
Division Assignment Operator (/=):
Modulus Assignment Operator (%=):
Floor Division Assignment Operator (//=):
Exponentiation Assignment Operator (**=):
Bitwise AND Assignment Operator (&=):
Bitwise OR Assignment Operator (|=):
Bitwise XOR Assignment Operator (^=):
Bitwise Right Shift Assignment Operator (>>=):
Bitwise Left Shift Assignment Operator (<<=):
Mastering assignment operators in Python can significantly improve our coding skills. These operators help us write cleaner, more efficient code. In this comprehensive guide, we explored assignment operators in Python, from the basic operator to the more advanced augmented operators and the Walrus Operator.
Whether we’re using simple assignments or the Walrus Operator, understanding these tools is essential. As we practice and apply these operators, our Python programming becomes more intuitive and effective.
- = operator is used for value assignment.
- == does a comparison to check if the two values are equal.
- += operator adds the right operand to the left operand and assigns the result to the left operand.
- + simply adds the operands without assignment.
Programs tailored for your success
3 ASSURED INTERVIEWS
Part-time · 10 months
Apply by : 21 December, 2024
Download Brochure
Part-time · 7 months
INTERNSHIP ASSURANCE
Part-time · 7.5 months
Part-time · 6 months
Upskill with expert articles
Accelerator Program in Business Analytics & Data Science
Integrated Program in Data Science, AI and ML
Accelerator Program in AI and Machine Learning
Advanced Certification Program in Data Science & Analytics
Certification Program in Data Analytics
Certificate Program in Full Stack Development with Specialization for Web and Mobile
Certificate Program in DevOps and Cloud Engineering
Certificate Program in Application Development
Certificate Program in Cybersecurity Essentials & Risk Assessment
Integrated Program in Finance and Financial Technologies
Certificate Program in Financial Analysis, Valuation and Risk Management
Certificate Program in Strategic Management and Business Essentials
Executive Program in Product Management
Certificate Program in Product Management
Certificate Program in Technology-enabled Sales
Certificate Program in Gaming & Esports
Certificate Program in Extended Reality (VR+AR)
Professional Diploma in UX Design
© 2024 Hero Vired. All rights reserved
- Career Schools - Jaipur - Jodhpur
- Apply as Mentor
IMAGES
VIDEO