Last Updated: Jun 30, 2023

# Python Operators

APURV RATHORE
0 upvote

## Introduction

Python is a programming language that is quite popular and widely used around the world. It is widely regarded as the best programming language to learn first due to its ease of use. It's a popular programming language for building scalable online applications since it's quick to learn, easy to use, and deploy. Python is used to create YouTube, Instagram, Pinterest, etc.

Beginners will find Python to be an excellent place to start. Python is a perfect choice if you want to focus more on logic rather than syntax.

You must understand the different types of Python operators for learning Python, and this blog focuses on the same.

Also See, Floor Division in Python,  Check out this article - Swapcase in Python

## What are Python Operators?

In Python, operators are special types of symbols that perform some operation on variables. The operands are the variables on which the operator operates.

Different types of operators are present in Python, such as

• Arithmetic Operators
• Comparison Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Identity Operator
• Membership Operator

We will see the above operators one by one.

Also see, Fibonacci Series in Python

## Arithmetic Operators

Arithmetic operators are used to perform different mathematical operations such as addition, multiplication, division, subtraction, etc.

Example:

``````x = 3
y = 9

print(x + y)

# subtraction operator
print(x - y)

# division operator
print(x / y)

# floor division operator
print(x // y)

# multiplication operator
print(x * y)

# modulo operator
print(x % y)

# power operator
print(x ** y)``````

Output:

``````12
-6
0.3333333333333333
0
27
3
19683``````

## Comparison Operators

They are used to compare the value of the operands. They return either True or False based on the condition and the value of the operands.

``````x = 3
y = 9

# less than operator
print(x < y)

# greater than operator
print(x > y)

# equals to operator
print(x == y)

# not equals to operator
print(x != y)

# greater than or equal to operator
print(x >= y)

# less than or equal to operator
print(x <= y)``````

Output:

``````True
False
False
True
False
True``````

## Logical Operators

Logical operators are used two combine two conditional statements.

``````# and operator
print(x and y)

# or operator
print(x or y)

# not operator
print(not x)``````

Output:

``````False
True
False``````

## Bitwise Operators

Bitwise operators operate on bits and perform operations bit by bit (bitwise).

``````x = 3
y = 9

# bitwise and operator
print(x & y)

# bitwise or operator
print(x | y)

# bitwise not operator
print(~ x)

# bitwise xor operator
print(x ^ y)

# bitwise right shift operator
print(x >> y)

# bitwise left shift operator
print(x << y)``````

Output:

``````1
11
-4
10
0
1536``````

You can try it on online python compiler.

## Assignment Operators

Assignment operators are used in Python to assign values to variables.

``````x = 3
y = 9

x += y
print(x)

x = 3
y = 9

x *= y
print(x)

x = 3
y = 9

x |= y
print(x)``````

Output:

``````12
27
11``````

## Identity Operators

Identity operators are a special type of operator present in Python. They are of two kinds: "is" and "it not". They check if two values are located on the same part of memory.

``````x = 3
y = 9

print(x is y)
print(x is x)

print(x is not y)
print(x is not x)``````

Output:

``````False
True
True
False``````

## Membership Operators

Membership operators are a special type of operator present in Python. They are of two kinds: “in” and “not in”. They are used to check if a value is present in a sequence.

``````x = 3
y = [1,2,3,4]

print(x in y)
print(x not in y)``````

Output:

``````True
False
``````

Also see,  Convert String to List Python

## FAQs

1. What are special operators in Python?
Python has two types of special operators:- identity operator and membership operator.

2. When an expression contains two or more operators, then in what order are the operators evaluated?
The order of evaluations of operators is based on precedence and associativity of operators. Operator precedence determines the priorities of the operators. If two or more operators with the same precedence are present, then the order is determined by associativity.

3. How many types of operators are present in Python?

There are seven types of operators present in Python: Arithmetic Operators, Comparison Operators, Logical Operators, Bitwise Operators, Assignment Operators, Identity Operator, and Membership Operator.

## Key Takeaways

Congratulations on making it this far.

In this blog, we understood what different types of Python operators are.