Python Tutorial
File handling, python modules, python numpy, python pandas, python matplotlib, python scipy, machine learning, python mysql, python mongodb, python reference, module reference, python how to, python examples, python assignment operators.
Assignment operators are used to assign values to variables:
Related Pages
COLOR PICKER
Contact Sales
If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected]
Report Error
If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected]
Top Tutorials
Top references, top examples, get certified.
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 : 7 November, 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
01 Career Opportunities
02 beginner, 03 intermediate, 04 training programs, assignment operators in python, what is an assignment operator in python, types of assignment operators in python, 1. simple python assignment operator (=), example of simple python assignment operator, 2. augmented assignment operators in python, 1. augmented arithmetic assignment operators in python, 2. augmented bitwise assignment operators in python, augmented arithmetic assignment operators in python, 1. augmented addition operator (+=), example of augmented addition operator in python, 2. augmented subtraction operator (-=), example of augmented subtraction operator in python, 3. augmented multiplication operator (*=), example of augmented multiplication operator in python, 4. augmented division operator (/=), example of augmented division operator in python, 5. augmented modulus operator (%=), example of augmented modulus operator in python, 6. augmented floor division operator (//=), example of augmented floor division operator in python, 7. augmented exponent operator (**=), example of augmented exponent operator in python, augmented bitwise assignment operators in python, 1. augmented bitwise and (&=), example of augmented bitwise and operator in python, 2. augmented bitwise or (|=), example of augmented bitwise or operator in python, 3. augmented bitwise xor (^=), example of augmented bitwise xor operator in python, 4. augmented bitwise right shift (>>=), example of augmented bitwise right shift operator in python, 5. augmented bitwise left shift (<<=), example of augmented bitwise left shift operator in python, walrus operator in python, syntax of an assignment expression, example of walrus operator in python, live classes schedule, about author.
IMAGES
VIDEO
COMMENTS
Assignment Operators are used to assign values to variables. This operator is used to assign the value of the right side of the expression to the left side operand. Output…
In this tutorial, you'll learn how to use Python's assignment operators to write assignment statements that allow you to create, initialize, and update variables in your code.
Python Assignment Operators. Assignment operators are used to assign values to variables: Operator. Example. Same As. Try it. =. x = 5. x = 5.
In simple terms := is a expression + assignment operator. It executes an expression and assigns the result of that expression in a single variable. Why is := operator needed? A simple useful …
Learn how to use assignment operators in Python for cleaner, efficient code. Simplify tasks like addition, subtraction, and bitwise shifts effectively.
In this tutorial, you'll learn about assignment expressions and the walrus operator. The biggest change back in Python 3.8 was the inclusion of the := operator, which you can use to assign variables in the middle of expressions. …
Assignment Operators. For demonstration purposes, let’s use a single variable, num. Initially, we set num to 6. We can apply all of these operators to num and update it accordingly. …
What is an Assignment Operator in Python? Assignment Operators in Python are used to assign values to the variables. "=" is the fundamental Python assignment operator. They require two operands to operate upon. The …