Introduction
Assignment operators are vital in computer programming because they assign values to variables. Python stores and manipulates data with assignment operators like many other programming languages. First, let's review the fundamentals of Python assignment operators so you can understand the concept.
In Python, the following operators are often used for assignments:
Sign
Type of Python Operators
=
Assignment Operator
+=
Addition assignment
-=
Subtraction assignment
*=
Multiplication assignment
/=
Division assignment
%=
Modulus assignment
//=
Floor division assignment
**=
Exponentiation assignment
&=
Bitwise AND assignment
|=
Bitwise OR assignment
^=
Bitwise XOR assignment
>>=
Bitwise right shift assignment
<<=
Bitwise left shift assignment
:=
Walrus Operator
Assignment Operator
Overview
Python uses in-fix assignment operators to perform operations on variables or operands and assign values to the operand on the left side of the operator. It carries out calculations involving arithmetic, logical, and bitwise operations.
Python assignment operator provides a way to define assignment statements. This statement allows you to create, initialize, and update variables throughout your code, just like a software engineer. Variables are crucial in any code; assignment statements provide complete control over creating and modifying variables.
Understanding the Python assignment operator and how it is used in assignment statements can equip you with valuable tools to enhance the quality and reliability of your Python code.
Example
In Python, the equals sign (=) is the primary assignment operator. It assigns the variable's value on the left side to the value on the right side of the operator.
Here's a sample to think about:
The value of x is 6.
In this code snippet, the variable 'x' is given the value of 6. The assignment operator doesn't check for equality but assigns the value.
Addition Assignment Operator
Overview
The addition assignment operator (+=) adds the right-hand value to the left-hand variable.
Syntax
The addition assignment operator syntax is variable += value.
Example
a = 11
a += 3
a = a+3
print(a)
The addition assignment operator increments a by 5. The console displays 14 as the result.
Subtraction Assignment Operator
Overview
The subtraction assignment operator subtracts a value from a variable and stores it in the same variable.
Syntax
The subtraction assignment operator syntax is variable-=-value.
Example
a = 10
b = 4
a -= b is equivalent to a= a - b
print(a)
Output:6
Multiplication Assignment Operator
Overview
Using the multiplication assignment operator (=), multiply the value on the right by the variable's existing value on the left.
Syntax
The assignment operator for multiplication has the following syntax: variable *= value
Example
a = 5
a *= 2
print(a)
In this situation, the multiplication assignment operator multiplies the value of a by 2. The output, 10, is shown on the console.
Division Assignment Operator
Overview
Using the division assignment operator (/=), divide the value of the left-hand variable by the value of the right-hand variable.
Syntax
The assignment operator for division has the following syntax: variable /= value
Example
a = 15
a /= 3
print(a)
Using the division assignment operator, divide a value by 3. The console displays 5.0.
Modulus Assignment Operator
Overview
The modulus assignment operator (% =) divides the left and right variable values by the modulus. The variable receives the remainder.
Syntax
The modulus assignment operator syntax is variable %= value.
Example
a = 7
a %= 2
print(a)
The modulus assignment operator divides a by 2. The console displays the following: 1.
Floor Division Assignment Operator
Overview
Use "//" to divide and assign floors in one phrase. What "a//=b" means is "a=a//b". This operator cannot handle complicated numbers.
Syntax
The floor division assignment operator syntax is variable == value.
Example
a = 11
a //= 2
print(a)
The floor division assignment operator divides a by 2. The console displays 5.
Exponentiation Assignment Operator
Overview
The exponentiation assignment operator (=) elevates the left variable value to the right value's power.
Syntax
Operator syntax for exponentiation assignment:
variable**=value
Example
a = 3
a **= 2
print(a)
The exponentiation assignment operator raises a to 2. The console shows 9.
Bitwise AND Assignment Operator
Overview
The bitwise AND assignment operator (&=) combines the left and right variable values using a bitwise AND operation. Results are assigned to variables.
Syntax
The bitwise AND assignment operator syntax is variable &= value.
Example
a = 4; the Binary representation of 4 is 100
a &= 2 # Binary representation of 2 is 010
print(a) # Output: 2 (Binary representation of 2 is 010)
The bitwise AND assignment operator ANDes a with 2. The console displays 2 as the outcome.
Bitwise OR Assignment Operator
Overview
The bitwise OR assignment operator (|=) bitwise ORs the left and right variable values.
Syntax
The bitwise OR assignment operator syntax is variable == value.
Example
a = 6; the Binary representation of 6 is 110
a = 4; the Binary representation of 4 is 100
print(a) # Output: 6 (Binary representation of 6 is 110)
A is ORed with 4 using the bitwise OR assignment operator. The console displays 6.
Bitwise XOR Assignment Operator
Overview
Use the bitwise XOR assignment operator (^=) to XOR the left and right values of a variable. Results are assigned to variables.
Syntax
For bitwise XOR assignment, use the syntax: variable ^= value.
Example
a = 6; the Binary representation of 6 is 110
a ^= 4 # Binary representation of 4 is 100
print(a) # Output: 2 (Binary representation of 2 is 010)
The bitwise XOR assignment operator XORs a with 4. The console displays 2 as the outcome.
Bitwise Right Shift Assignment Operator
Overview
The right shift assignment operator (>>=) shifts the variable's left value right by the number of places specified on the right.
Syntax
The assignment operator for the bitwise right shift has the following syntax:
variable >>= value
Example
a = 6; the Binary representation of 6 is 110
a >>= 2 # Shift 2 positions to the right
print(a) # Output: 1 (Binary representation of 1 is 001)
The bitwise right shift assignment operator shifts 2 places to the right. The result is 1.
Bitwise Left Shift Assignment Operator
Overview
The variable value on the left moves left by the specified number of places on the right using the left shift assignment operator (<<=).
Syntax
The bitwise left shift assignment operator syntax is variable <<= value.
Example
a = 15
b = 1
a <<= b
print (a)
Output:30
When we execute a Bitwise right shift on 'a', we get 00011110, which is 30 in decimal.
Walrus Operator
Overview
Python gets new features with each update. Emily Morehouse added the walrus operator to Python 3.8's initial alpha. The most significant change in Python 3.8 is assignment expressions. The ":=" operator allows mid-expression variable assignment. This operator is called the walrus operator.
Syntax
variable := expression
It was named for the operator symbol (:=), which resembled a sideways walrus' eyes and tusks.
Walrus operators simplify code authoring, which is its main benefit. Each user input was stored in a variable before being passed to the for loop to check its value or apply a condition. It is important to note that the walrus operator cannot be used alone.
Example
With the walrus operator, you can simultaneously define a variable and return a value.
myVar=(value:=2346)
print("myVar:{}".format(myVar))
print("value:{}".format(value))
Output:myVar:2346
value:2346
Above, we created two variables, myVar and value, with the phrase myVar = (value = 2346). The expression (value = 2346) defines the variable value using the walrus operator. It returns the value outside the parenthesis as if value = 2346 were a function.
The variable myVar is initialized using the return value from the expression (value = 2346).
The output shows that both variables have the same value.
Learn more about other Python operators by reading our detailed guide here.
Conclusion
Discover how Python assignment operators simplify and optimize programs. Python assignment operators are explained in length in this guide, along with examples, to help you understand them. Start this intriguing journey to improve your Python knowledge and programming skills with Simplilearn's Python training course.
FAQs
1. What is the ":=" operator in Python?
Python's walrus operator ":" evaluates, assigns, and returns a value from a single sentence. Python 3.8 introduces it with this syntax (variable:=expression).
2. What does = mean in Python?
The most significant change in Python 3.8 is assignment expressions. The walrus operator allows mid-expression variable assignment.
3. What is def (:) Python?
The function definition in Python is (:). Functions are defined with def. A parameter or parameter(s) follows the function name. The function body begins with an indentation after the colon (:). The function body's return statement determines the value.