Python Assignment and Bitwise Operators

Learn to perform an assignment and bitwise operations in Python 3.

Python Assignment and Bitwise Operators
Assignment and Bitwise Operations in Python

Assigning means allocating values to variables. Assignment operators can perform various operations like arithmetic, logical and bitwise operations and then assign the computed value to a variable.

Sections Covered

  1. What are Operands?
  2. Assignment Operators
  3. Bitwise Operations
  4. Walrus Operator
  5. Exercise to solve

What are Operands?

Before understanding assignment operators, let’s talk about Operands.

Operands are quantities or values on which an operation is being performed.

Let’s say you are adding 3 and 2. Here, addition is the operation and, 3 and 2 are the operands.

In this article, we will discuss various assignment operators and also a special operator introduced in Python 3.8 - Walrus operator.

OperatorDescriptionSyntax
+=Add and assignx += y
-=Subtract and assignx -= y
*=Multiply and assignx *= y
/=Divide and assignx /= y
%=Take Modulus and assignx %= y
//=Floor Divide and assignx //= y
**=Take exponent and assignx **= y
&=Perform bitwise “AND” operation and assignx &= y
|=Perform bitwise “OR” operation and assignx |= y
^=Perform bitwise “xOR” operation and assignx ^= y
>>=Perform bitwise “Right Shift” operation and assignx >>= y
<<=Perform bitwise “Left Shift” operation and assignx <<= y

Assignment Operators

+= operator - Add and assign

The += operator is used to adding 2 operands and assign the new value to the left operand.

Code

x = 3
y = 2

x += y
print(x)

Output

5

-= operator - Subtract and assign

The -= operator is used to subtracting 2 operands and assign the new value to the left operand.

Code

x = 3
y = 2

x -= y
print(x)

Output

1

*= operator - Multiply and assign

The *= operator is used to multiplying 2 operands and assign the new value to the left operand.

Code

x = 3
y = 2

x *= y
print(x)

Output

6

/= operator - Divide and assign

The /= operator is used to performing division between 2 operands and assign the new value to the left operand.

Code

x = 3
y = 2

x /= y
print(x)

Output

1.5

%= operator - Modulus and assign

The %= operator is used to performing modulus operation on 2 operands and assign the new value to the left operand.

Modulus operation returns the remainder from dividing 2 operands.

Code

x = 3
y = 2

x %= y
print(x)

Output

1

//= operator - Floor Divide and assign

The //= operator is used to performing floor division on 2 operands and assign the new value to the left operand.

Floor division returns the quotient from dividing 2 operands.

Code

x = 3
y = 2

x //= y
print(x)

Output

1

**= operator - Exponent and assign

The **= operator is used to performing an exponential operation on 2 operands and assign the new value to the left operand.

Code

x = 3
y = 2

x **= y
print(x)

Output

9

What are Bitwise operators?

Before moving onto Bitwise Assignment operators, let’s understand Bitwise operations.

In Python, bitwise operations are performed on integers. The integers are first converted into binary format and then operations are performed on every bit. The result is returned in decimal format.

Let’s learn how to convert an integer to binary format. The binary representation of 10 is 1010.

&= operator - Bitwise AND operation and assign

The & operator returns True if both the operands are True. This is the AND operation. From the article on Booleans, it is clear that Python treats 1 as True and 0 as False.

So, the &= operator performs the bitwise AND operation and assigns the new value to the left operand.

Code

x = 3
y = 2

x &= y
print(x)

Output

2

Explanation

x = 3 --> binary format = 0011
y = 2 --> binary format = 0010

x & y = 0011
         &
        0010
      = 0010

The decimal representation of 0010 is 2.

|= operator - Bitwise OR operation and assign

The | operator returns True if either of the operands is True. This is the OR operation. From the article on Booleans, it is clear that Python treats 1 as True and 0 as False.

So, the |= operator performs the bitwise OR operation and assigns the new value to the left operand.

Code

x = 3
y = 2

x |= y
print(x)

Output

3

Explanation

x = 3 --> binary format = 0011
y = 2 --> binary format = 0010

x & y = 0011
         |
        0010
      = 0011

The decimal representation of 0011 is 3.

^= operator - Bitwise xOR operation and assign

The ^ operator returns True if one of the operands is True and the other is False, else it returns False. This is the xOR operation. From the article on Booleans, it is clear that Python treats 1 as True and 0 as False.

So, the ^= operator performs the bitwise xOR operation and assigns the new value to the left operand.

Code

x = 3
y = 2

x ^= y
print(x)

Output

1

Explanation

x = 3 --> binary format = 0011
y = 2 --> binary format = 0010

x & y = 0011
         ^
        0010
      = 0001

The decimal representation of 0001 is 1.

>>= operator - Bitwise Right Shift operation and assign

When shifting right, the most significant bit is lost, and a 0 bit is inserted on the left side.

The right shift operator is written as >>.

0011 >> 1 --> 0001 (Moved right by one place)
0011 >> 2 --> 0000 (Moved right by two places)

Code

x = 3
y = 2

x >>= y
print(x)

Output

0

Explanation

x = 3 --> binary format = 0011
y = 2 

x >> y = 0011 >> 2 (Move right by two places)
       = 0000

The decimal representation of 0000 is 0.

<<= operator - Bitwise Left Shift operation and assign

When shifting left, the most significant bit is lost, and a 0 bit is inserted on the right side.

The left shift operator is written as <<.

0011 << 1 --> 0110 (Moved left by one place)
0011 << 2 --> 1100 (Moved left by two places)

Code

x = 3
y = 2

x <<= y
print(x)

Output

12

Explanation

x = 3 --> binary format = 0011
y = 2 

x << y = 0011 << 2 (Move left by two places)
       = 1100

The decimal representation of 1100 is 12.

Walrus Operator

The Walrus operator (:=) is a cool little feature that was introduced in Python 3.8.

It allows you to assign a value to a variable while also returning the value, without having to declare it before.

Example 1

Code - Without Walrus Operator

x = 3
print(x)

You can convert the above code into a one-liner by using the Walrus Operator.

Code - Using Walrus Operator

print(x:=3)

Output

3

Example 2

Code - Without Walrus Operator

list_of_names = []
name = input("Name a friend: ")
while name != "stop":
    list_of_names.append(name)
    name = input("Name a friend: ")

The above code asks the user for input and continues within the while loop until we ask to stop. It uses the Python built-in input() function. If you don’t know how to use it, check it out!

Using the Walrus operator, we can rewrite the above code the following way.

Code - Using Walrus Operator

list_of_names = []
while (name := input("Name a friend: ")) != "stop":
    list_of_names.append(name)

Exercise

Question 1

Calculate the results of the following Bitwise operations by hand and then verify your answers by running them in a Python interpreter.

1. 10&=4
2. 10^=4
3. 12|=3
4. 10<<=3

Question 2

Simplify the below operation to show the numerator and denominator and calculate the result.

print((a:=3)/(10+(y:=4)))

Question 3

Run the following code and figure out why Python is returning False?

print(0.1 + 0.2 == 0.3)

If you are able to solve them or you have any doubts, tweet to me @pylenin.

Subscribe to Pylenin

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe