Download our e-book of Introduction To Python

Matplotlib - Subplot2grid() FunctionDiscuss Microsoft Cognitive ToolkitMatplotlib - Working with ImagesMatplotlib - PyLab moduleMatplotlib - Working With TextMatplotlib - Setting Ticks and Tick LabelsCNTK - Creating First Neural NetworkMatplotlib - MultiplotsMatplotlib - Quiver PlotPython - Chunks and Chinks View More

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? Which sorting technique is used by sort() and sorted() functions of python? How to use Enum in python? Can you please help me with this error? I was just selecting some random columns from the diabetes dataset of sklearn. Decision tree is a classification algo...How can it be applied to load diabetes dataset which has DV continuous Objects in Python are mutable or immutable? How can unclassified data in a dataset be effectively managed when utilizing a decision tree-based classification model in Python? How to leave/exit/deactivate a Python virtualenvironment Join Discussion

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.