#### World's Best AI Learning Platform with profoundly Demanding Certification Programs

Designed by IITians, only for AI Learners.

Designed by IITians, only for AI Learners.

New to InsideAIML? Create an account

Employer? Create an account

Download our e-book of Introduction To Python

Exception Type: JSONDecodeError at /update/ Exception Value: Expecting value: line 1 column 1 (char 0) How can I write Python code to change a date string from "mm/dd/yy hh: mm" format to "YYYY-MM-DD HH: mm" format? How to leave/exit/deactivate a Python virtualenvironment What is the process of executing a Python script within Power Automate? Change the hyperparameter means ? Why is my f1_scores different when i calculate them manually vs output by sklearn.metrics How can a sentence be matched to a cluster of keywords? How Python interpreter Works? Join Discussion

4.5 (1,292 Ratings)

559 Learners

Jun 8th (7:00 PM) 289 Registered

Pallavi Dhotre

2 years ago

Introduction to Operators

Types of operators

- Arithmetic Operators in python

- Assignment Operators in python

- Comparison / Relational Operators in python

- Logical Operators / Boolean operators in python

- Identity Operators in python

- Membership Operators in python

- Bitwise Operators in python

Summary

In this article, we will know about the operators in python and the types of operators in python. In mathematics, to write an expression or equation, we use operators. For example, a+b in this expression ‘+’ operator is used. Similarly, in python, there are various types of operators, and they are used to write the expression or any condition. But the equations in mathematics and equations in python are a little bit different. In general, we write equation a+b=5 in this way. But in python, expression is always written to the right side of the ‘=’ operator. For example, 5=a+b. Now, let’s have a brief look at the types of operators.

Python operators are divided into the following categories

- Arithmetic Operators
- Assignment Operators
- Comparison / Relational Operators
- Logical Operators
- Identity Operators
- Membership Operators
- Bitwise Operators

Python supports all arithmetic operators like +, -, /, *, etc. Following are the arithmetic operators in python.

Look at the following code to know the working of each arithmetic operator in python

```
x=10
y=5
# Addition
print("Addition:",x+y)
# Subtraction
print('Subtraction:',x-y)
# Multiplication
print('Multiplication:',x*y)
# Division
print('Division:',x/y)
# Division in python, Quotient after integer (whole number) division
print("Floor division",x//y)
# Remainder after integer (whole number) division; also called modulo operator python
print('Modulo:',x%y)
# Exponentiation or power (x power of y)
print("Exponential:",x**y)
```

Output

```
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
Floor division 2
Modulo: 0
Exponential: 100000
```

Python has a special set of operators known as compound assignment operators, which can help you write code efficiently because they work as a shortcut. Compound operators are a combination of two operators. Following are the assignment operators in python.

See the following code to have a brief idea about assignment operators

```
x=20
y=4
x += y # Addition also equivalent to x = x + y
print(x)
x -= y # Subtraction also equivalent to x = x - y
print(x)
x *= y # Multiplication also equivalent to x = x * y
print(x)
x /= y # Division also equivalent to x = x / y
print(x)
x //= y # Floor Division also equivalent to x = x // y
print(x)
x %= y # Modulus also equivalent to x = x % y
print(x)
x **= y # Exponentiation also equivalent to x = x ** y
print(x)
```

Output

```
24
20
80
20.0
5.0
1.0
1.0
```

Relational operators are also known as comparison operators in python. Comparison operators are mostly used in Boolean expressions. And the value of the expression is always true or false. Syntax to use relational operators is as follow:

Operand1 Comparison Operator operand2

Following are the comparison operators in python.

See the following code to have a brief idea about relational operators:

```
x=20
y=4
print('Greater than',x>y)
print('Less than',x<y)
print('Greater than equals to',x>=y)
print('Less than equals to',x<=y)
print('Equals to ',x==y)
print('Not equals to',x!=y)
```

Output

```
Greater than True
Less than False
Greater than equals to True
Less than equals to False
Equals to False
Not equals to True
```

The relationship between two Boolean expressions is denoted by logical operators in python. Logical operators are also known as boolean operators python. Two or more conditions can be combined using logical operators. The syntax for using logical operator is:

Condition1 logical_operator Condition2

Following are the logical operators used in python

See the following code to have a brief idea about logical operators

```
a=10
b=a+2
print((a>5) and (b>a)) #and operator
print((a>15) or (b>a)) #or operator
print(a!=b) #not operator
```

Output

```
True
True
True
```

Identity operators in python are used to check if both the operators have the same memory location or not. Following are the identity operators used in python:

{1, 2, 3, 4, 5, 6}

set()

>

Coding example:

`x=10y=xprint(x is y)print(x is not y)`

Output

```
True
False
```

Membership operators in python are used to check if the given value is present in the object or not. Following are the membership operators used in python:

Coding example:

`x=[1,2,3,4,5]print(1 in x)print(2 not in x)`

Output

```
True
False
```

Bitwise operators in python are used to compare numbers. & operator in python is the most popular operator. Following are the bitwise operators used in python:

In this article, we have learned about operators and types of operators in python. Operators are used to performing operations and used in the expressions. There are the following types of operators in python: arithmetic operators, assignment operators, relational or conditional operators, logical operators, membership operators, and bitwise operators. We hope you enjoyed the article. If you have any related queries, feel free to ask in the comment section below.

Liked the Blog, then Share it with your friends and colleagues to make this AI community stronger.

To learn more about nuances of Artificial Intelligence, Python Programming, Deep Learning, Data Science and Machine Learning, visit our blog page.

Keep Learning. Keep Growing.