Python Operators
We can do different kinds of operations in Python between the variables and values. We need operators to do such kinds of operations. For example, if we want to add two numbers, then we need the addition operator to complete this operation.
Basically, operators are the symbols that we applied between the operands. And every operator has their own meaning and work. See the below diagram:

In this example, we are adding two values. Here both values are called operands, and the add (+) symbol is called an operator. And because this operator does the addition of values, it is called an addition operator. So every operator has their own symbol and their own work.
In the upper example, we use the add symbol, but it is not necessary that every operator is denoted by symbols. Some operators are denoted by keywords like AND, OR, NOT; these are the logical operators in Python. They don’t use symbols. They used keywords. It is also possible that an operator is denoted by a combination of symbols. For example, the double equal sign (==) is used to denote the equality operator in Python.
Operators Vs Operands in Python
Before understanding Python operators, it is necessary to understand the difference between operators and operands. We already learned about operands in the upper example. Operators are the symbols and we used operators between the operands. For example:

In this example, the add (+) symbol is an operator and the numbers 5 and 3 are the operands. And because there are two operands, we are calling them the left and right operands. It is not necessary that the operator always use between two operands. We can use the operator with a single operand. For example:

Here the minus (-) symbol is an operator and the number 34 is an operand. It is also possible that we have multiple operators and multiple operands in a single statement. For example:

According to the use of operators between the operands, we can divide operators into two types:
1. Unary Operators |
2. Binary Operators |
Unary Operators
When we use an operator with a single operand. It is called the unary operators. For example: -44
Binary Operators
When we use an operator between the two operands. It is called the binary operators. For example: 4+5
NOTE: It is not necessary that only values can be operands. We can also use operators between the variables and expression. For example:
Code:
Num1 = 100
Num2 = 200
Result = Num1 + Num2
print(Result)
Here we are using the addition operator between two variables (Num1 and Num2). So here Num1 and Num2 are called left and right operands.
Types of Operators in Python
Python has several kinds of operators. According to their work, operators are divided into different categories. Basically, operators are divided into seven categories.
Python Operators |
1. Arithmetic Operators |
2. Assignment Operators |
3. Comparison Operators |
4. Logical Operators |
5. Identity Operators |
6. Membership Operators |
7. Bitwise Operators |
Arithmetic Operators in Python
We can do mathematical calculations with the help of arithmetic operators. We use symbols to denote arithmetic operators. Python has seven kinds of arithmetic operators.
1: Addition Operator (+)
We use the addition operator to add numbers. We can add two or more numbers with the help of the addition operator. We use the add (+) symbol to denote the addition operator. For example:
Code:
print(5+6)
Output:
11
Here we used the addition operator to add two numbers. We can use the addition operator with the variables. For example:
Code:
num1 = 5
num2 = 6
print(num1+num2)
Output:
11
We can add more than two numbers with the help of the addition operator. For example:
Code:
num1 = 5
num2 = 6
num3 = 6
print(num1+num2+num3)
Output:
17
So this is how we can use the addition operator. The addition operator works with both types, unary and binary operators. In the upper examples, we use the addition operator as a binary operator. We can also use addition operators as unary operators. For example:
Code:
num1 = +10
print(num1)
Output:
10
2: Subtraction Operator (-)
We use the subtraction operator to subtract numbers. We can subtract two or more numbers with the help of the subtraction operator. We use the minus (-) symbol to denote the addition operator. For example:
Code:
print(10-5)
Output:
5
Here we used the subtraction operator to subtract two numbers. We can use this with variables too.
Code:
num1 = 57
num2 = 41
print(num1 - num2)
Output:
16
If left operand is smaller than right operand.
Code:
num1 = 87
num2 = 104
print(num1 - num2)
Output:
-17
Because the left operand is smaller than the right operand, we get negative output. The subtraction operator also works with unary and binary operators. For example:
Code:
num1 = -104
print(num1)
Output:
-104
3: Multiplication Operator (*)
We use the multiplication operator to multiply numbers. We can multiply two or more numbers with the help of the multiplication operator. We use the asterisk (*) symbol to denote the multiplication operator. It is also called a star (*) symbol in common language. For example:
Code:
print(21*54)
Output:
1134
We can also use multiplication operators with variables.
Code:
num1 = 44
num2 = 74
num3 = 54
print(num1*num2*num3)
Output:
175824
It is not necessary to multiply only numbers with the multiplication operators. We can multiply the valid values. For example:
Code:
value1 = 'Python '
value2 = 3
print(value1*value2)
Output:
Python Python Python
Another example:
Code:
value1 = '*'
print(value1 * 1)
print(value1 * 2)
print(value1 * 3)
print(value1 * 4)
print(value1 * 5)
Output:
*
**
***
****
*****
So this is how we can use the multiplication operator to multiply the valid values. The multiplication operator works with only binary operators. If we try to work with an unary operator,. We will get an error in the output. For example:
Code:
value1 = *5
print(value1)
Output:
SyntaxError: can't use starred expression here
So we get an error. So this is how we used the multiplication operator.
4: Division Operator (/)
We use the division operator to divide numbers. We can divide two or more numbers with the help of the division operator. We use the forward slash (/) symbol to denote the division operator. For example:
Code:
print(1205/5)
Output:
241.0
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. We can also use the division operator with multiple values or variables. For example:
Code:
num1 = 1080
num2 = 2
num3 = 2
print(num1/num2/num3)
Output:
270.0
The division operator works with only binary operators. If we try to work with an unary operator,. We will get an error in the output. For example:
Code:
num1 = /88
print(num1)
Output:
SyntaxError: invalid syntax
So we get an error. So this is how we used the division operator.
5: Integer Division (//) or floor division
The integer division is also called floor division. We use integer division when we wanted the output into the integer data type. Or we can say, when we want the only whole number part into the output, not the remainder. Then we used integer division. We use a double forward slash (//) to denote the integer division. For example:
Code:
print(10/5)
print(10//5)
Output:
2.0
2
We can see the difference between the outputs. When we used the division operator, we got the remainder (zero) part in the output (2.0) but when we used the integer division, we only got the integer part in the output, not any remainder.
Basically, in the upper example, there is no remainder left because 10 is completely divided by 5. But if in any equation there is a remainder left and we’re using integer division, then we don’t get the remainder part in the output. For example:
Code:
print(9/2)
print(9//2)
Output:
4.5
4
Here, when we divided 9 by 2, number 5 is the remainder. But when we use integer division, we don’t get the remainder part in the output. In the same way, if we swap the numbers:
Code:
print(2/9)
print(2//9)
Output:
0.2222222222222222
0
In the second print section, we used integer division, so we don’t get the remainder part in the output. We only get zero in the output. So this is how we used integer division.
6: Modulus Operator (%) or Remainder Operator
We use the modulus operator to get the remainder from a division. We only get remainder in the output from the modulus operator. We use the percent symbol to denote modulus operator. For example:
Code:
print(4%2)
print(5%2)
Output:
0
1
We get zero from (4%2) because 4 is divided by 2 and the remainder is zero. We get 1 from (5%2) because 5 is divided by 2 and the remainder is one. We can use modulus operators with variables. For example:
Code:
num1 = 10
num2 = 6
print(num1%num2)
Output:
4
If the left operand is smaller than the right operand, then we get the left operand in the output. For example:
Code:
num1 = 6
num2 = 10
print(num1%num2)
Output:
6
In this example, the operand is smaller than the right operand (6%10). In such a case, when the left operand is smaller than the right operand, we always get the left operand in the output. For example:
Code:
print(114%120)
print(25%36)
print(74%99)
Output:
114
25
74
In the output, it returns the left operand. So this is how we use the modulus operator.
7: Exponentiation Operator (**) Or Exponent Or Power Operator
The exponentiation operator is used to raise the left operand to the power of the right operand. We use the double asterisk (**) symbol to denote the exponentiation operator. It is also called the exponent operator or power operator. For example:
Code:
print(2**3)
Output:
8
Here number 2 is the left operand and number 3 is the right operand. So number 2 will raise to the power of number 3. Means (2**3 = 2*2*2 = 8) , if (3**2 = 3*3 = 9).
We can use it with variables. For example:
Code:
num1 = 5
num2 = 3
print(5**3)
Output:
125
243
So this is how we can use the exponentiation operator.
End of the Python operators (Part 1) tutorial
2 thoughts on “Python Operators (Part 1): What Are The Operators in Python?”