Operators in Python
V.Anusuya
Assistant Professor(SG)/CSE
Ramco Institute of Technology
02/09/2023 1
Operators
Operators are the constructs which can
manipulate the value of operands.
• In an expression, an operator is used on
operands.
Example: 4 + 5 = 9.
• 4 and 5 are called operands
• + is called an Operator
02/09/2023 2
There are various operators in python:
1. Arithmetic Operator
2. Comparison/Relational Operator
3. Logical Operator
4. Bitwise Operator
5. Assignment Operator
6. Membership Operator
7. Identity Operator
02/09/2023 3
Arithmetic Operators
Arithmetic operators are used to
perform some basic arithmetic operations.
These operators can be applied on
numbers as well as on variables to
perform the corresponding operations.
02/09/2023 4
Contd.,
Operator Meaning Example Result
+ Addition C=12+1 C=13
- Subtraction C=12-1 C=11
* Multiplication C=12*1 C=12
/ Division C=12/1 C=12
// Floor division C=12//10 1
% Modulus C=12%10 C=2
** Exponentiation C=10**2 C=100
02/09/2023 5
Example:
z=x*y
x=10
print z
y=20 Output
z=x+y 200
print(z) z=x**2
Output print z
30 Output
100
02/09/2023 6
Example of Arithmetic Operator
Output
print("Arithmetic Operator") Arithmetic Operator
a=10 Addition: 15
Subtraction: 5
b=5
Multiplication: 50
print("Addition:",a+b) Division: 2.0
print("Subtraction:",a-b) Floor Division: 2
print("Multiplication:",a*b) Modulus: 0
Exponent 100000
print("Division:",a/b) print("Floor
Division:",a//b) print("Modulus:",a
%b) print("Exponent",a**b)
02/09/2023 7
Quiz
What is the output of this expression, 3*1**3?
a) 27
b) 9
c) 3
d) 1
Answer
c
02/09/2023 8
Comparison Operator
• Relational operators are also called as
Comparison operators.
• It is used to compare values.
• It either returns True or False according to
condition.
02/09/2023 9
Contd.,
Operator Meaning Example Result
> Greater than 5>6 False
< Less than 5<6 True
== Equal to 5==6 False
!= Not equal to 5!=6 True
>= Greater than or 5>=6 False
equal to
<= Less than or 5<=6 True
equal to
02/09/2023 10
Example of Relational Operator
Output
print("Relational Operator")
a=10 Relational Operator
b=5
True
print(a>b)
False
print(a<b)
False
print(a==b)
True
print(a!=b)
True
print(a>=b)
False
print(a<=b)
02/09/2023 11
Logical Operator
Boolean(logical) values.
They allow a program to make a decision
based on multiple condition.
02/09/2023 12
Contd.,
Operator Meaning Example Result
and True if both 10<5 and 10<20 False
the operands
are true
or True if either 10<5 or 10<20 True
of the
operands is
true
not True if not (10<20) False
operands is
false
( complements
the operand)
02/09/2023 13
Example of Logical Operator
print("Logical Operator") print(10<5 and
10<20)
print(10<5 or 10<20)
print(not(10<20))
Output
Logical Operator
False
True
False
02/09/2023 14
Bitwise Operators
• These operators perform bit level operations
on operands. Let us take two operands
• x=10 and y=4. In binary format this can be
written as x=1010 and y=0100.
02/09/2023 15
Operator Meaning Example
Operator copies a bit to the
& Binary AND result if it exists in both x& y = 0 (0000 0000)
operands
It copies a bit if it exists in
| Binary OR x | y = 14 (0000 1110)
either operand.
It copies the bit if it is set in x ^ y = 14 (0000
^ Binary XOR
one operand but not both. 1110)
~ Binary Ones It is used to opposite the
~x results 0110
Complement bits of operand.
The left operands value is
<< Binary Left moved left by the number x<< 2 = 40 (0010
Shift of bits specified by the right 1000)
operand.
The left operands value is
>> Binary Right moved right by the number x>> 2 = 2 (0000
Shift of bits specified by the right 0010)
operand.
02/09/2023 16
Assignment Operator
This operator is used to store the right side operand in
the left side operand
02/09/2023 17
Operator Meaning Example
= Assign a value a=5
+= Adds and assign the a+=1 (a=a+1)
result to the variable
-= Subtracts and assign the a-=1 (a=a-1)
result to the variable
*= Multiplies and assign a*=5 (a=a*5)
the result to the variable
/= Division and assign the a/= (a=a/5)
result to the variable
//= Floor division and a//=5(a=a//5)
assign the result to the
variable
%= Find modulus and a%=5 (a=a%5)
assign the result to the
variable
**= Find Exponentiation a**=5 (a=a**5)
and assign the result
to the variable
02/09/2023 18
Membership Operators
Python supports two types of membership
operators–in and not in. These operators, test for
membership in a sequence such as strings, lists, or
tuples.
02/09/2023 19
Contd.,
in Operator: The operator returns True if a
variable is found in the specified sequence and
False otherwise.
not in Operator: The operator returns True if a
variable is not found in the specified sequence
and False otherwise.
02/09/2023 20
Example:
x=10
y=12
list=[21,13,10,17]
10 in list
True
5 in list
False
10 not in list
False
02/09/2023 21
Contd.,
Identity Operators
• is Operator: Returns True if operands or values
on both sides of the operator point to the same
object and False otherwise.
• is not Operator: Returns True if operands or
values on both sides of the operator does not point
to the same object and False otherwise.
02/09/2023 22
Example
x=12
y=12
x is y
True
02/09/2023 23
Example of Identity Operator
a1=5 Output
b1=5 False
a2="Hello" True
b2="Hello" False
a3=[1,2,3]
b3=[1,2,3]
print(a1 is not b1)
print(a2 is b2)
print(a2 is b3)
02/09/2023 24
Precedence of Python Operators
Operator Description
** Exponentiation (raise to the power)
~+- Complement, unary plus(+a) and minus (-a)
* / % // Multiply, divide, modulo and floor division
+- Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'
^| Bitwise XOR and OR
<= < > >= Comparison operators
== != Equality operators
= %= /= //= -= += *= **= Assignment operators
is is not Identity operators
in not in Membership operators
02/09/2023 not or and Logical operators 25
Input Statement
The input() method reads a line from input,
converts into a string and returns it
Syntax
input([Prompt])
Prompt-A string that is written to standard
output.
02/09/2023 26
Example
val=input(“Enter the String”)
print (val)
Output
Enter the String
Welcome
Welcome
02/09/2023 27
Addition of two Numbers
a=int(input(“Enter number1”))
b=int(input(“Enter number 2”)
c=a+b
print(“The Addition is”,c)
Output
Enter number1
10
Enter number2
5
The Addition is 15
02/09/2023 28