In this tutorial, we are going to learn about the basic operators in Python.
Arithmetic Operators
Arithmetic operators are useful in performing mathematical operations like addition, subtraction, multiplication, etc..,
- Addition ----- Adds two numbers ----- +
- Subtraction ----- Substracts one number from another ----- -
- Multiplication ----- Multiplies two numbers ----- *
- Division ----- Divides one number with another ----- /
- Floor Division ------ It returns integer after division ----- //
- Modulus ----- It gives remainder ----- %
Let's see the examples.
Example
# initialising two numbers
a = 5
b = 2
# addition
print(f'Addition: {a + b}')
# substraction
print(f'Substraction: {a - b}')
# multiplication
print(f'Multiplication: {a * b}')
# division
print(f'Division: {a / b}')
# floor division
print(f'Floor Division: {a // b}')
# modulus
print(f'Modulus: {a % b}')Output
If you run the above program, you will get the following results.
Addition: 7 Substraction: 3 Multiplication: 10 Division: 2.5 Floor Division: 2 Modulus: 1
Relational Operators
Relational operators return either True or False as a result. These operators are used to compare the same type of objects in Python. Let's see a list of relational operators.
- Greater than ----- > ----- Checks whether a number is greater than other or not
- Greater than or equal to ----- >= ----- Checks whether a number is greater than or equal to other or not
- Less than ----- < ----- Checks whether a number is less than other or not
- Less than or equal to ----- <= ----- Checks whether a number is less than or equal to other or not
- Equal to ----- == ----- Checks whether a number is similar to other or not
- Not equal to ----- != ----- Checks whether a number is not similar to other or not
Let's see the examples.
Example
# initialising two numbers
a = 5
b = 2
# greater than
print(f'Greater than: {a > b}')
# greater than or equal to
print(f'Greater than or equal to: {a >= b}')
# less than
print(f'Less than: {a < b}')
# less than or equal to
print(f'Less than or qual to: {a <= b}')
# equal to
print(f'Equal to: {a == b}')
# not equal to
print(f'Not equal to: {a != b}')Output
If you run the above code, you will get the following results.
Greater than: True Greater than or equal to: True Less than: False Less than or qual to: False Equal to: False Not equal to: True
Logical Operators
Logical operators are used to performing logical operations like and, or, and not.
- and ----- True if both are True
- or ----- False if both are False
- not ----- Inverts the operand
Let's see the examples.
Example
# initialising variables
a = True
b = False
# and
print(f'and: {a and b}')
# or
print(f'or: {a or b}')
# not
print(f'not: {not a}')
print(f'not: {not b}')Output
If you run the above code, you will get the following results.
and: False or: True not: False not: True
Bitwise Operators
Bitwise operators are used to performing bitwise operators like and, or, and not.
- & ----- True if both are True
- | ----- False if both are False
- ~ ----- Inverts the operand
Let's see the examples.
Example
# initialising numbers
a = 5
b = 2
# bitwise and
print(f'Bitwise and: {a & b}')
# bitwise or
print(f'Bitwise or: {a | b}')
# bitwise not
print(f'Bitwise not: {~a}')
# bitwise not
print(f'Bitwise not: {~b}')Output
If you run the above program, you will get the following results.
Bitwise and: 0 Bitwise or: 7 Bitwise not: -6 Bitwise not: -3
Assignment Operators
Assignment operators are used to assigning values to the variables. We have the following assignment operators.
- = ----- assign a number to a variable
- += ----- adds a number and assigns to the variable
- -= ----- subtracts a number and assigns to the variable
- *= ----- multiplies a number and assigns to the variable
- /= ----- divides a number and assigns to the variable
- //= ----- divides(floor division) a number and assigns to the variable
- %= ----- modulus a number and assigns to the variable\
Let's see the examples.
Example
# =
a = 5
print(f'=:- {a}')
# +=
a += 1 # a = a + 1
print(f'+=:- {a}')
# -=
a -= 1 # a = a - 1
print(f'-=:- {a}')
# *=
a *= 2 # a = a * 1
print(f'*=:- {a}')
# /=
a /= 2 # a = a / 1
print(f'/=:- {a}')
# //=
a //= 2 # a = a // 1
print(f'//=:- {a}')
# %=
a %= 10 # a = a % 1
print(f'%=:- {a}')Output
If you run the above program, you will get the following results.
=:- 5 +=:- 6 -=:- 5 *=:- 10 /=:- 5.0 //=:- 2.0 %=:- 2.0
Conclusion
If you have any doubts regarding the tutorial, mention them in the comment section.