In today’s class, we’ll learn about different **Python operators** such as Arithmetic, Comparison, Assignment, Logical, Bitwise, Identity and Membership operators.

Also, we’ll know what are these operators for and how to use them in real-time.

The following table will assist you in learning Python operators.

**What are built-in operators in Python?****Arithmetic operators****Comparison operators****Logical operators****Bitwise operators****Assignment operators****Identity operators****Membership operators**

## What are built-in Python operators?

Like many programming languages, Python reserves some special characters for acting as operators. Every operator has a pre-defined action like addition, multiplication to manipulate data and variables. The variables passed as input to an operator are known as operands. We also recommend you to read about **keywords in Python**.

**Example-**

>>> 7%4 3

In this case, ‘%’ is the modulus operator that calculates the remainder of the division. The numbers ‘7’ and ‘4’ passed as input are the operands whereas the numbers ‘3’ is the result of the action performed.

## Arithmetic operators

With arithmetic operators, we can do various arithmetic operations like addition, subtraction, multiplication, division, modulus, exponent, etc. Python provides multiple ways for arithmetic calculations like eval function, declare variable & calculate, or call functions.

The table below outlines the built-in arithmetic operators in Python.

**Example-**

a=7 b=4 print('Sum : ', a+b) print('Subtraction : ', a-b) print('Multiplication : ', a*b) print('Division (float) : ', a/b) print('Division (floor) : ', a//b) print('Modulus : ', a%b) print('Exponent : ', a**b)

**Output-**

Sum : 11 Subtraction : 3 Multiplication : 28 Division (float) : 1.75 Division (floor) : 1 Modulus : 3 Exponent : 2401

## Comparison operators

In Python programming, comparison operators allow us to determine whether two values are equal or if one is greater than the other and then make a decision based on the result.

The table below outlines the built-in comparison operators in Python.

**Example-**

a=7 b=4 print('a > b is',a>b) print('a < b is',a<b) print('a == b is',a==b) print('a != b is',a!=b) print('a >= b is',a>=b) print('a <= b is',a<=b)

**Output-**

a > b is True a < b is False a == b is False a != b is True a >= b is True a <= b is False

## Logical operators

The logical Python operators enable us to make decisions based on multiple conditions. The operands act as conditions that can result in a true or false value. The outcome of such an operation is either true or false (i.e., a Boolean value).

However, not all of these operators return a boolean result. The ‘and’ and ‘or’ operators do return one of their operands instead of a pure boolean value. Whereas the ‘not’ operator always gives a real boolean outcome.

Refer the below table and the example to know how these operators work in Python.

**Example-**

a=7 b=4 # Result: a and b is 4 print('a and b is',a and b) # Result: a or b is 7 print('a or b is',a or b) # Result: not a is False print('not a is',not a)

**Output-**

a and b is 4 a or b is 7 not a is False

## Bitwise operators

Bitwise Python operators process the individual bits of integer values. They treat them as sequences of binary bits.

We can use bitwise operators to check whether a particular bit is set. For example, IoT applications read data from the sensors based on a specific bit is set or not. In such a situation, these operators can help.

**Example-**

Let’s consider the numbers 4 and 6 whose binary representations are ‘00000100’ and ‘00000110’. Now, we’ll perform the AND operation on these numbers.

a=4 b=6 #Bitwise AND: The result of 'a & b' is 4 print('a & b is',a & b)

**Output-**

a & b is 4

The above result is the outcome of following AND (‘&’) operation.

0 0 0 0 0 1 0 0 & 0 0 0 0 0 1 1 0 ------------------ 0 0 0 0 0 1 0 0 (the binary representation of the number 4)

## Assignment operators

In Python, we can use assignment operators to set values into variables.

The instruction a = 4 uses a primitive assignment operator which assigns the value 4 to the left operand.

Below is the list of available compound operators in Python. For example, the statement a += 4 adds to the variable and then assigns the same. It will evaluate to a = a + 4.

## Advanced Python operators

Python also bundles a few operators for special purposes. These are known as advanced Python operators like the identity operator or the membership operator.

### Identity operators

These operators enable us to compare the memory locations of two Python objects/variables. They can let us find if the objects share same memory address. The variables holding equal values are not necessarily identical.

Alternatively, we can use these operators to determine whether a value is of a specific class or type.

Refer the below table to understand more about them.

**Example-**

# Using 'is' identity operator a = 7 if (type(a) is int): print("true") else: print("false") # Using 'is not' identity operator b = 7.5 if (type(b) is not int): print("true") else: print("false")

**Output-**

true true

### Membership operators

Membership operators enable us to test whether a value is a member of another Python objects such as strings, lists, or tuples.

In C, membership test requires iterating through a sequence and checking each value. So, it is clear that Python makes it very easy to establish membership as compared to C.

Also, please note that this operator can also test against a dictionary but only for the key, not the value.

**Example-**

# Using Membership operator str = 'Python operators' dict = {6:'June',12:'Dec'} print('P' in str) print('Python' in str) print('python' not in str) print(6 in dict) print('Dec' in dict)

**Output-**

True True True True False

## Quick wrap up – Python operators

This tutorial covered various Python operators and sought to describe each of them with examples. It should now be easier for you to use operators in Python.

If you find something new to learn today, then do share it with others. And, follow us on our social media (**Facebook**/**Twitter**) accounts to see more of this.

**Best,**

**TechBeamers**