Posted in

Python Operators (Part 2): How Many Types of Operators in Python?

Python Operators Part 2
Python Operators Part 2

Click for the Python operators (Part 1) tutorial.

How Many Types of Python Operators?

We already learned about Python operators in the previous tutorial. We also learned Python operators are divided into seven categories. We have completed the arithmetic operators in the previous tutorial. Now continue with the rest of the operators in Python.

Assignment Operators in Python

We use the assignment operator to assign values to variables, update values, or change values. The equal sign (=) is used in assignment operators. It worked with left and right operands. Mostly, the left operand is a variable and the right operand is a value or an expression.

NOTE: The assignment operator does not create expressions; it creates a kind of statement that returns a value.

There are several kinds of assignment operators in Python.

1: Assignment Operator (=)

It is also called a basic assignment operator. We use the assignment operator to assign the right-side expression to the left-side operand. Or we can change, set, update, and delete the value of the right side. For example:

name = 'Python'

Here name is a variable and we assign 'Python' as value to this variable. We use equal sign (=) to assign value. We can assign multiple values. For example:

Code:
name1, name2 = 'Python', 'Programming'
print(name1)
print(name2)

Output:
Python
Programming

So this is how we can assign multiple values with the help of an assignment operator.

2. Addition Assignment Operator (+=)

We can mix another operator with the assignment operator; for example, if we use the addition operator (+) with the assignment operator (=), then it is called the addition assignment operator (+=). With the help of the addition assignment operator, we can add the right side operand to the left side operand. And after the addition, the result will be assigned to the left side operand.

Syntax: num1 += num2
Here num1 and num2 are two operands. And when we use num1 += num2 this is the short for:
num1 = num1 + num2
So it means add num1 with num2 and the result will be assigned to num1.

For example:

Code:
num1 = 10
num1 += 10
print(num1)

Output:
20

So here num1 += 10 means num1 = num1 + 10 And num1 = 10 so 10+10 = 20 is the output.

Another example:

Code:
num1 = 10
num2 = 15
num1 += num2
print(num1)

Output:
25 

Same as above, num1 += num2 means num1 = num1 + num2 so we get 25 in the output.

Another example:

Code:
text1 = 'Python '
text2 = 'Programming'
text1 += text2
print(text1)

Output:
Python Programming

If we try to add two different kinds of data types, we will get an error:

Code:
text1 = 'Python'
num1 = 10
text1 += num1
print(text1)

Output:
TypeError: can only concatenate str (not "int") to str

Here we are trying to add two different kinds of data types. text1 is a string and num1 is an integer. To add different kinds of data types, we have to use type casting.

Code:
text1 = 'Python'
num1 = str(10)
text1 += num1
print(text1)

Output:
Python10

Here we changed the integer data type into a string data type. Click to learn about string type casting in Python.

3. Subtraction Assignment Operator (-=)

Just like addition, we can use subtraction (-) with the assignment operator (=). Same as addition, it will subtract the right side operand from the left side operand and assign the result to the left side operand. For example:

Code:
num1 = 50
num2 = 10
num1 -= num2
print(num1)

Output:
40

Same as addition, num1 -= num2 means num1 = num1 - num2. So we get 40 in the output. Another example:

Code:
num1 = 50
num2 = 60
num1 -= num2
print(num1)

Output:
-10

We get -10 in the output. Because num1 -= num2 means num1 = num1 - num2 so 50-60 = -10 is the result.

4. Multiplication Assignment Operator (*=)

We can use multiplication (*) with the assignment operator (=). We can multiply the right side operand with the left side operand and the result will be assigned to the left side operand. For example:

Code:
num1 = 10
num1 *= 5
print(num1)

Output:
50

Same as above, here num1 *= 5 means num1 = num1 * 5 so we get 50 in the output. Another example:

Code:
num1 = 10
num2 = 5
num1 *= num2
print(num1)

Output:
50

Another example:

Code:
num1 = 10
num1 *= num1
print(num1)

Output:
100

So this is how we can use the multiple assignment operator.

5. Division Assignment Operator (/=)

Same as above, we can use the division assignment operator. The left side operand will be divided by the right side operand and the result will be assigned to the left side operand. For example:

Code:
num1 = 4
num1 /= 2
print(num1)

Output:
2.0

Here we write, num1 /= 2 which means, num1 = num1 / 2, so we get 2.0 in the output. We get the output in float data type because every time we use the division operator, the output always comes in float data type. It’s default. Another example:

Code:
num1 = 4
num2 = 7
num1 /= num2
print(num1)

Output:
0.5714285714285714

This is how we can use the division assignment operator.

6. Modulus Assignment Operator (%=)

We can use the modulus assignment operator. In modulus assignment operator, the interpreter did the modulus operation between the left and right operand, which means the left operand will be divided by the right operand and the remainder will be assigned to the left operand. For example:

Code:
num1 = 4
num1 %= 2
print(num1)

Output:
0

Here we write, num1 %= 2 which means, num1 = num1 % 2 so number 4 will be divided by number 2. And we get zero as the remainder. Another example:

Code:
num1 = 5
num2 = 2
num1 %= num2
print(num1)

Output:
1

Same as above, number 5 will be divided by number 2 and we get number 1 as the remainder. Another example:

Code:
num1 = 11
num2 = 20
num1 %= num2
print(num1)

Output:
11

Here our left operand (11) is smaller than the right operand (20). In such a situation, we always get the smaller number (left operand) as the remainder. So that’s why we get 11 in the output. Another example:

Code:
num1 = 110
num2 = 111
num1 %= num2
print(num1)

Output:
110

Same as the above example, here the left operand has a small number so we get number 110 in the output.

7. Floor Division Assignment Operator (//=)

Floor division assignment operator is similar to division assignment operator. The only different between both of them is, the division assignment operator

One thought on “Python Operators (Part 2): How Many Types of Operators in Python?

Leave a Reply

Your email address will not be published. Required fields are marked *