
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
What does the &= operator do in Python?
The += operator is syntactic sugar for object.__iand__() function. From the python docs:
These methods are called to implement the augmented arithmetic assignments (+=, -=, *=, @=, /=, //=, %=, **=, <<=, >>=, &=, ^=, |=). These methods should attempt to do the operation in-place (modifying self) and return the result (which could be, but does not have to be, self).
Example
So when you do something like −
a = 6 # 110 in binary b = 5 # 101 in binary a &= b # a changes to and of 110 and 101, ie, 100, ie, 4 print(a)
Output
This will give the output −
15
a is being modified in place here. You can read more about such operators on https://docs.python.org/3/reference/datamodel.html#object.__iand__.
- Related Articles
- What does the >> operator do in Python?
- What does 'in' operator do in Python?
- What Does the // Operator Do?
- What does 'is' operator do in Python?
- What does colon ':' operator do in Python?
- What does the "===" operator do in Kotlin?
- What does 'not in' operator do in Python?
- What does 'is not' operator do in Python?
- What does "?:" do in Kotlin? (Elvis Operator)
- What does the Star operator mean in Python?
- What does the bitwise left shift operator do in Java?
- What does the bitwise right shift operator do in Java?
- What does the operator || do in a var statement in JavaScript?
- What does the Double Star operator mean in Python?
- What does "print >>" do in python?

Advertisements