Last Updated : 07 Sep, 2021,GATE CS 2021 Syllabus
35
8
276
11.2
7
The equivalent expression is &=
for and
and |=
for or
.
>>> b = True >>> b &= False >>> b False
Note bitwise AND
and bitwise OR
and will only work (as you expect) for bool
types. bitwise AND
is different than logical AND
for other types, such as numeric
>>> bool(12) and bool(5) # logical AND True >>> 12 & 5 # bitwise AND 4
Unlike normal assignment operator, Augmented Assignment Operators are used to replace those statements where binary operator takes two operands says var1 and var2 and then assigns a final result back to one of operands i.e. var1 or var2.,For example: statement var1 = var1 + 5 is same as writing var1 += 5 in python and this is known as augmented assignment operator. Such type of operators are known as augmented because their functionality is extended or augmented to two operations at the same time i.e. we're adding as well as assigning.,Python OperatorsBitwise Operators in PythonChaining Comparison Operators in PythonTernary or Conditional Operator in PythonAugmented Assignment Operators in Python with Examples,Augmented Assignment Operators in Python with Examples
PROGRAM
# Addition a = 23 b = 3 a += b print('Addition = %d' % (a))
OUTPUT
Addition = 26
PROGRAM
# Subtraction a = 23 b = 3 a -= b print('Subtraction = %d' % (a))
OUTPUT
Subtraction = 20
PROGRAM
# Multiplication a = 23 b = 3 a *= b print('Multiplication = %d' % (a))
OUTPUT
Multiplication = 69
PROGRAM
# Division a = 23 b = 3 a /= b print('Division = %f' % (a))
OUTPUT
Division = 7.666667
PROGRAM
# Remainder or Modulo a = 23 b = 3 a %= b print('Remainder or Modulo = %d' % (a))
OUTPUT
Remainder or Modulo = 2
PROGRAM
# Power a = 23 b = 3 a **= b print('Power = %d' % (a))
OUTPUT
Power = 12167
In Python, we know how the arithmetic operators can be used to add, subtract, divide and multiply two variables.,In this article, we will learn how we can extend the functionality of an operator in a precise form at the time of evaluating an expression.,So, in this article, we learned how the augmented assignment expressions can be used in Python.,In the below program, we have evaluated the expression for the bitwise right shift between the variables x and y.
Value of z is: 25 Value of x is: 20
Value of z is: 5 Value of x is: 10
Value of z is: 150 Value of x is: 75
Value of z is: 1.5 Value of x is: 3.0
Value of z is: 5 Value of x is: 0
Value of z is: 225 Value of x is: 3375