Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Operators
Next article icon

ThePython Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic, logical, and bitwise computations. The value the operator operates on is known as the Operand.

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.

Python
# Assigning values using# Assignment Operatora=3b=5c=a+b# Outputprint(c)

Output

8

Addition Assignment Operator

The Addition Assignment Operator is used to add the right-hand side operand with the left-hand side operand and then assigning the result to the left operand.

Python
a=3b=5# a = a + ba+=b# Outputprint(a)

Output:

8

Subtraction Assignment Operator

The Subtraction Assignment Operator is used to subtract the right-hand side operand from the left-hand side operand and then assigning the result to the left-hand side operand.

Python
a=3b=5# a = a - ba-=b# Outputprint(a)

Output:

-2

Multiplication Assignment Operator

The Multiplication Assignment Operator is used to multiply the right-hand side operand with the left-hand side operand and then assigning the result to the left-hand side operand.

Python
a=3b=5# a = a * ba*=b# Outputprint(a)

Output:

15

Division Assignment Operator

The Division Assignment Operator is used to divide the left-hand side operand with the right-hand side operand and then assigning the result to the left operand.

Syntax:a /= b
Python
a=3b=5# a = a / ba/=b# Outputprint(a)

Output:

0.6

Modulus Assignment Operator

The Modulus Assignment Operator is used to take the modulus, that is, it first divides the operands and then takes the remainder and assigns it to the left operand.

Syntax:a %= b
Python
a=3b=5# a = a % ba%=b# Outputprint(a)

Output:

3

Floor Division Assignment Operator

The Floor Division Assignment Operator is used to divide the left operand with the right operand and then assigs the result(floor value) to the left operand.

Syntax:a //= b
Python
a=3b=5# a = a // ba//=b# Outputprint(a)

Output:

0

Exponentiation Assignment Operator

The Exponentiation Assignment Operator is used to calculate the exponent(raise power) value using operands and then assigning the result to the left operand.

Syntax:a **= b
Python
a=3b=5# a = a ** ba**=b# Outputprint(a)

Output:

243

Bitwise AND Assignment Operator

The Bitwise AND Assignment Operator is used to perform Bitwise AND operation on both operands and then assigning the result to the left operand.

Syntax:a &= b
Python
a=3b=5# a = a & ba&=b# Outputprint(a)

Output:

1

Bitwise OR Assignment Operator

The Bitwise OR Assignment Operator is used to perform Bitwise OR operation on the operands and then assigning result to the left operand.

Syntax: a |= b
Python
a=3b=5# a = a | ba|=b# Outputprint(a)

Output:

7

Bitwise XOR Assignment Operator 

The Bitwise XOR Assignment Operator is used to perform Bitwise XOR operation on the operands and then assigning result to the left operand.

Syntax: a ^= b
Python
a=3b=5# a = a ^ ba^=b# Outputprint(a)

Output:

6

Bitwise Right Shift Assignment Operator

The Bitwise Right Shift Assignment Operator is used to perform Bitwise Right Shift Operation on the operands and then assign result to the left operand.

Syntax: a >>= b
Python
a=3b=5# a = a >> ba>>=b# Outputprint(a)

Output:

0

Bitwise Left Shift Assignment Operator

The Bitwise Left Shift Assignment Operator is used to perform Bitwise Left Shift Opertator on the operands and then assign result to the left operand.

Syntax: a <<= b
Python
a=3b=5# a = a << ba<<=b# Outputprint(a)

Output:

96

Walrus Operator

TheWalrus Operator in Python is a new assignment operator which is introduced in Python version 3.8 and higher. This operator is used to assign a value to a variable within an expression.

Syntax: a := expression

Example: In this code, we have a Python list of integers. We have used Python Walrus assignment operator within thePython while loop. The operator will solve the expression on the right-hand side and assign the value to the left-hand side operand'x' and then execute the remaining code.

Python
# a lista=[1,2,3,4,5]# walrus operatorwhile(x:=len(a))>2:a.pop()print(x)

Output:

543

Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp