GE19211 - PROBLEM SOLVING
AND PROGRAMMING IN PYTHON
Operators in Python
WEEK 03
CONTENTS
01 02 03
Arithmetic Operators Relational Operators Assignmnet Operators
04 05 06
Logical Operators Bitwise operators Membership and
Identity Operators
Arithmetic Operators
An arithmetic operators are
mathematical operators that
takes two operands and
performs a simple arithmetic
operation on them.
Arithmetic Operators
#Demo Program to test Arithmetic OperatorsOutput:
a=100
b=10 The Sum = 110
print ("The Sum = ",a+b) The Difference = 90
print ("The Difference = ",a-b) The Product = 1000
print ("The Product = ",a*b)
The Quotient = 10.0
print ("The Quotient = ",a/b)
print ("The Remainder = ",a%30) The Remainder = 10
print ("The Exponent = ",a**2) The Exponent = 10000
print ("The Floor Division =",a//30)
#Program End The Floor Division = 3
Arithmetic Operators
Arithmetic Operators
Input:
n-the given number
Logic:
how to get the last digit?
use modulo division with 10
Write a Program
to find the sum How to get the digit before last
digit?
of last 2 digits in Do true division on n by 10 and
find a result
a given number Apply modulo division with 10 for
the result
Add the 2 digits and find sum
Output:
Print the sum
Relational Operators
Relational operators are
also called as Comparative
operators which checks the
relationship between two
operands. If the relation is
true, it returns True;
otherwise it returns False.
Relational Operators
Relational Operators
Input:
age-the given number
Logic:
Write a Program how to check the
eligibility?
to check the
If the persons age is
person is eligible greater than 18 than
for voting he/she is eligible for voting
otherwise not eligible
Output:
Print True or False
Logical Operators
Logical operators are used
to perform logical
operations on the given
relational expressions or
operands.
Logical Operators
Logical Operators
Logical Operators
Input:
n-the given number
Write a Program Logic:
to check the how to check the n is
given number is multiple of 3 and 5?
multiple of 3 If the number is fully
divisible by 3 and 5
and 5
Output:
Print True or False
Logical Operators
• All positive and negative numbers
are true.
• Zero alone is false.
If all expressions are +ve or –ve numbers
• While applying not operator on
it returns the last expression. If any one numbers , its works as follows
expression is 0 it returns ).
If all expressions are 0 it returns 0.
Otherwise it returns the first +ve
expression.
Assignment Operators
• = is a simple assignment operator to assign values to variable.
• Let a = 5 and b = 10 assigns the value 5 to a and 10 to b
• These two assignment statement can also be given as
a,b=5,10.
• There are various compound assignment operators in Python
like +=, -=, *=, /=, %=, **= and //= .
Assignment Operators
Assignment Operators
Input:
a,b-read 2 values
Write a Program Logic:
to swap two how to swap?
Interchange the two values
values
Output:
Print a , b value
Bitwise Operators
Bitwise operators manipulate
individual bits of data at the
most granular level.
They perform bit by bit logical
operation on numbers.
Bitwise Operators
Bitwise Operators
We can also use a formula to find the answer, We can also use a formula to find the answer,
x<<y=x*2y x>>y=x/2y
Bitwise Operators
Input:
Write a Program n-read a values
to calculate
Logic:
power of 2 how to calculate 2^n?
without using Left shift the bit by n times
exponential(**)
Output:
operator (2^n)
Print the result
Membership and Identity
Operators
Membership and Identity
Operators
Membership Operator Identity Operators
23
Precedence of Operators
• The operators follow the hierarchy which is shown in the
following table.
• The precedence of the common operators in shown in
decreasing order.
• Please note that parenthesis is not an operator but it has the
highest precedence.
Department of Computer Science and
24
Engineering
Precedence of Operators
Department of Computer Science and
25
Engineering
Simple Python codes
Jack and his three friends have decided to go for a trip by sharing the expenses of the fuel equally . Write
a Python program to calculate the amount (in Rs) each of them need to put in for the complete (both to
and fro) journey. The program should also display True, if the amount to be paid by each person is
divisible by 5, otherwise it should display False. (Hint: Use the relational operators in print statement.
Assume that mileage of the vehicle, amount per litre of fuel and distance for one way are given.
Department of Computer Science and
27
Engineering