Operators in Python Programming Language

Operators are fundamental part of a programming language as these are used for doing Mathematics on different entities. Operators are special symbols which can be used for doing different kind of computations like arithmetic or logical computations.
For example – You may want to add two numbers together, how to do this just use plus operation 10 + 23.
Python operators can be divided into 7 categories – Arthimetic, Assignment, Comparison, Logical, Identity, Membership and Bitwise.
These operators are applied to certain values and those values are called Operands.

Arithmetic Operators in Python

Operators which are used for mathematical operations like doing addition, subtraction, multiplication or division.
Below table lists all of Arithmetic Operators in Python with examples for each one.

Operator NameOperator SymbolDescriptionExample
Addition+Adding values10 + 2
x + y
SubstractionSubtracting values132 – 212
x – y
Multiplication*Multiplying values163 * 4
x * y *z
Division/Dividing values19/2
x/y
Modulus%Remainder of left after dividing left operand on rightx % y
10 % 3
Floor Division//Division where resulting number is shifted to it’s closest lower whole number. Like 10//3 will be 3 only not 3.3392//3
x//y
Exponent**Multiplying left operand with itself for as many times as right operand OR Left operand raised to the power of rightx**y
45**4

Assignment Operators in Python

In Python, Assignment Operators are used for assigning values to variables. For example => a = 5 here = equal symbol is an assignment operator.
Below table lists all of Assignment Operators in Python with examples for each one.

Operator NameOperator SymbolDescriptionExample
Equal=Assigning value to a variablex = 5
Increment and Assign+=First increase value and then do assignmentx += 10
Decrement and Assign-=First Decrement value and then do assignmentx -= 40
Multiply and Assign*=First do multiplication and then do assignmentx *= 5
Divide and Assign/=First do division and then do assignmentx /= 38
Modulus and Assign%=First take modulus of operands and then do assignmentx %= 29
Floor Division and Assignment//=First do Floor Division and do assignmentx //= 24
Raise to Power and Assignment**=Firstly raise left operand to right power of right operand and then do assignmentx **= 26
And-percent and Assignment&=First do & operation(check for common elements inside operands) and then do assignmentx &= 29
OR Operator and Assignment|=First do logical OR operation on operands and then do assignmentx |= 10
Exclusive-OR and Assignment^=First do exclusive OR operation on operands and then do assignmentx ^= 28

Comparison Operators in Python

In Python Programming Language, Comparison Operators are used for comparing two values. For example – If we have two variables then to check which is greater that other Comparison Operator can be used.
Below is a table listing Comparison Operators in Python with some examples for each one.

Operator NameOperator SymbolDescriptionExample
Greater than>Checks if left operand is larger than right operand. If it is then returns true false otherwisex > y
10 > 28
Greater than and Equal to>=Checks if left operand is larger or equals to right operand. If it is then returns true false otherwisex >= y
27 >= 17
Less than<Checks if left operand is smaller than right operand. If it is then returns true false otherwisex < y
18 < 29
Less than and Equal to<=Checks if left operand is smaller or equals to right operand. If it is then returns true false otherwisex <= y
38 <= 29
Equal to==Checks if both of operands are equal or not. If they are equal then returns true false otherwisex == y
18 == 29
Not Equal to!=Checks if both operands are not equal. Returns true if they’re not equal, false otherwisex != y
17 != 28

Logical Operators in Python

Python have three logical operators – And, OR, Not. These can be used for Control Flow in program based upon how variables evaluate in program.
Below is a table listing all of Logical Operators in Python with some example for each one.

Operator NameOperator SymbolDescriptionExample
ANDandTrue only if both operands are true, false otherwise.x and y
ORorTrue if either of operand is true, false otherwisex or y
NOTnotReverses Boolean value. If operand true change it to false, if its false change it to truenot x

Identity Operators in Python

Identity Operators are for checking whether two identities(say two variables) are located on same piece of memory.
Below is a diagram showing what does it mean to be on same piece of memory.

Python two variables pointing to same memory location

Below is a table listing all identity operators in Python with some examples for each one.

Operator NameOperator SymbolDescriptionExample
isisReturns true if both of operands point to same memory location, false otherwise.x is y
is notis notReturns true if both of operands does not point to same memory location, true otherwise.x is not y

Membership Operator in Python

In Python Programming Language, Membership Operator is used for checking whether some value is a member of sequence. For example – If we have a list [ 19, 28, 21 ] then to check whether 19 is there in list. We can do 19 in list membership test, where in is a Membership Operator.

If your not aware of What are sequences in Python? Or What are other 5 data types expect Sequences? then check out below article.

Below is a table listing all of Python’s Membership Operators with examples for each one.

Operator NameOperator SymbolDescriptionExample
ininReturns true if value is inside sequence, false otherwise10 in x
not innot inReturns true if value is not inside sequence, false otherwise(If value is inside sequence)28 not in x

Bitwise Operators in Python

These operators treat operands in binary format rather than normal format.
Below is a table listing Python’s Bitwise Operators with some examples for each one.

Operator NameOperator SymbolExample
Bitwise AND&13 & 3
Returns 1
Bitwise OR|15 | 3
Returns 12
Bitwise XOR^34 ^ 28
Returns 62
Bitwise Ones Complement~~23
Returns -24
Bitwise Left Shift<<8 << 3
Returns 64
Bitwise Right Shift>>24 >> 5
Returns 0

Josh

Hi, I'm Josh a Computer Science graduate from California State University, Sacramento since coming out with my Master's from university. I've worked with multiple startups across US and in UK as well primarily as a Python Developer. Here on this website, I'm sharing my knowledge of Python. If you want to ask me anything about Python feel free to reach out, I would be happy to help you out.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts