7 Python Operators with Examples

We are going to cover Python Operators with Examples , python Arithmetic Operators, python Logical operators, python Comparison operators, python Assignment operators, python Bitwise operators, python Identity operators, python Membership operators

What is Operator in Python ?

Operators are special symbols used to perform operations. The value that the operator operates on is called the operand.

For Example, 2+5=7 here, 2 and 5 are called operands and + is an operator that performs addition.

Python Operators with Examples

#1: Python Arithmetic Operators:

Arithmetic Operators are used to perform mathematical calculations on numeric data. And perform operations like addition, subtraction, multiplication and division.

OperatorDescriptionSyntax
+Addition : add two operandsa+b
Subtraction : subtracts two operandsa-b
*Multiplication: multiplies two operandsa*b
/Division: divides the first operand by the seconda/b
//Floor division: divides the first operand by the seconda//b
%Modulus : divides first operand by second operand and returns remaindera%b
**Exponent(power):  returns first raised to power seconda**b

Examples of Arithmetic Operators

a = 7
b = 2
#addition
add = a + b
#subtraction
sub = a – b
#multiplication
mul = a * b
#division
div = a / b
#Floor division
div_floor = a // b
#Modulus
mod = a % b
# Exponent(power)
power = a ** b
#print
print(add)
print(sub)
print(mul)
print(div)
print(div_floor)
print(mod)
print(power)
7 Python Operators with Examples 1

#2: Python Logical Operators:

Logical operators allow a program to make a decision based on multiple conditions. Each operand is considered a condition that can be evaluated to a true or false value. Then the value of the conditions is used to determine the overall value of the operand1 operator operand2. Logical operators perform Logical AND, Logical OR and Logical NOT operations.

OperatorDescriptionSyntax
andLogical AND: If both the operands are true then result become true. If anyone operand is false then result become falsea and b
orLogical OR: If one operand true or another is false then result become truea or b
notLogical NOT: If both operands are true then the result becomes true. otherwise anyone operand is false then the result becomes false.not a

Examples of python Logical Operators

a = True
b =  False
# and
print(a and b)
# or
print(a or b)
# not
print(not a) 
output:
False
True
False
7 Python Operators with Examples 2

#3: Python Comparison Operators:

In python Comparison Operators are also called as relational operators in python.  Comparison operators compare the value.

It returns True or False according to the condition.

In python there are different types of comparison operators like less than, greater than, equal to and not equal to.

OperatorDescriptionSyntax
>Greater than:Result becomes True when left operand is greater than the right. a > b
<Less than: If the left operand is less than right then the result becomes True.a < b
==Equal to: If both operands are equal then the result becomes True.a == b
>=Greater than or equal to: If left operands is greater than or equal to the right then the result becomes True.a >= b
<=Less than or equal to: If left operands is less than or equal to the right then the result becomes True.a <= b
!=Not equal to: If both operands are not equal then result become Truex !=y

Examples of python comparison operators

a = 12
b = 10
  
# it returns True because 12 is greater than 10  
print(a > b) 
  
# it returns False because 12 is not less than 10  
print(a < b) 
  
# it returns False because 12 is not equal to 10  
print(a == b) 
  
# it returns True because 12 is not equal to 10  
print(a != b) 
  
# it returns True because 12 is greater than or equal to 10  
print(a >= b) 
  
# it returns False because 12 is neither greater than or equal to 10  
print(a <= b) 
7 Python Operators with Examples 3

#4: Python Assignment Operators:

In python, assignment operators  are used to assign value to variables.

OperatorDescriptionSyntax
=Assign value on the left side of the expression. a = b
+=Add AND: Add right operand with left operand and assign Result to left side operanda + = ba = a + b
-=Subtract AND :Subtract right operand from left operand and assign result to the left operanda – = ba = a – b
*=Multiply AND: Multiply right operand with left operand and then assign result to the left operanda * = ba = a * b
/=Divide AND: Divide left operand with right operand and then assign result  to the left operanda / = ba = a / b
//=Divide floor AND: Divide left operand with right operand and then assign result to the left operanda // = ba = a //b
%=Modulus AND: Using left and right operands and assign result to left operanda % = ba = a % b
**=Exponent AND: Calculate exponent value using operands and assign result to left operanda ** = ba = a ** b
>>=Performs Bitwise right shift on operands and assign result to left operanda >> = ba = a >> b
<<=Performs Bitwise left shift on operands and assign result to left operanda << = ba = a <<b
&=Performs Bitwise AND on operands and assign value to left operanda & = ba = a & b

Examples of python assignment operators

a = 7
print(a)
#Add AND
a + = 7
print(a)
#Sub AND
a - = 7
print(a)
#Mul AND
a * = 7
print(a)
#Div AND
a / = 7
print(a)
#Div Floor AND
a // = 7
print(a)
#Mod AND
a % = 7
print(a)
#Exponent AND
a ** = 7
print(a)
#Bit Right 
a >> = 7
print(a)
#Bit Left
a << =7
print(a)
#Bit AND
a & = 7
print(a)
7 Python Operators with Examples 4

#5: Python Bitwise Operators:

In Bitwise operator operations are done in bit-level. In this operators are used to compare numbers

operatorDescriptionSyntax
&Bitwise AND: Bitwise AND result is 1 if the bits of two operands is 1. If either bit of operand is 0 then result become 0a & y
|Bitwise OR: If anyone bit operand is one the resilt become 1a | b
^Bitwise XOR: If bit operands are opposite then result become 1.a ^ b
~Bitwise NOT: It changes 1 to 0 and 0 to 1~a
>>Bitwise right shift: Towards righta >>
<<Bitwise left shift: Towards lefta <<

Examples of python Bitwise Operators

a = 12
b = 25
print(a & b)
print(a|b)
print(a ^ b)
print(~ a)
print(a>>4)
print(a<<2)
7 Python Operators with Examples 5

#6: Python Identity Operators:

Identity Operator is used to compare the object memory location. It checks if two values are located in the same part of memory.

If two values are the same that does not mean they are identical.

OperatorDescriptionSyntax
isIf both operands are the same object then result become Truea is b
Is notIf both operands are not the same object then result become Truea is not b

Examples of Python Identity Operators

x = 'FOSSTechNix'
y = 'FOSSTechNix'
x1 = ["a","b","c"]
y1 = ["a","b","c"]
# True because y is same as x
print(x is y)
# False because y and x same
print(x is not y)
# True because x and y is equal
print(x == y)
# False because x and y is equal
print(x != y)
7 Python Operators with Examples 6

#7: Python Membership Operators:

Membership Operators are used to check value is in sequence

OperatorDescriptionSyntax
inIf the value is in sequence the result become truea in y
not inIf the value is not in sequence the result become truea not in b

Examples of Python Membership Operators

x = ["a","b","c"]
y = "FOSSTechNix"
# in 
print("FOSS" in y)
print("FOSS" in x)
# not in
print("d" not in x)
print("a" not in x)
7 Python Operators with Examples 7

We have covered Python operators with Examples.

Conclusion:

We have covered Python operators with Examples , python Arithmetic Operators, python Logical operators, python Comparison operators, python Assignment operators, python Bitwise operators, python Identity operators, python Membership operators

Related Articles:

Python Introduction for Programmers [Part 1]

Python Conditional Statements with Examples

Reference:

Python Official Page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern at startup Product Company. Likes to share knowledge.

7 thoughts on “7 Python Operators with Examples”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link