- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this tutorial, we are going to learn about the basic operators in Python.

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.

# 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}')

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 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.

# 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}')

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 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.

# 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}')

If you run the above code, you will get the following results.

and: False or: True not: False not: True

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.

# 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}')

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 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.

# = 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}')

If you run the above program, you will get the following results.

=:- 5 +=:- 6 -=:- 5 *=:- 10 /=:- 5.0 //=:- 2.0 %=:- 2.0

If you have any doubts regarding the tutorial, mention them in the comment section.

- Related Questions & Answers
- Basic operators in Java
- What are different basic operators in Python?
- Basic Operators in Relational Algebra
- Basic Operators in Shell Scripting
- What are basic JavaScript mathematical operators?
- How to do basic Maths with JavaScript Operators?
- Basic Python Programming Challenges
- Basic List Operations in Python
- Basic Tuples Operations in Python
- Basic Calculator II in Python
- Division Operators in Python?
- Overloading Operators in Python
- Basic calculator program using Python
- Python Arithmetic Operators
- Python Comparison Operators

Advertisements