Chapter 2 Python Operators
Chapter 2 Python Operators
Chapter 2 Python Operators
Contents
● Python Operators
Aniket Goswami
PGT – Computer Science.
Vivekananda Kendra Vidyalaya, Hurda.
Dist – Bhilwara
Mo. 9829906113
1
Python Operators
Example
4+5=9
4*5 = 20
1. Arithmetic Operators.
2. Relational Operators.
3. Assignment Operators.
4. Logical Operators.
5. Membership Operators.
6. Identity Operators.
7. Bitwise Operators.
The following table contains the arithmetic operators that are used to
perform arithmetic operations.
2
Example
>>> 10+20
30
>>> 20-10
10
>>> 10*2
20
>>> 10/2
5
>>> 10%3
1
>>> 2**3
8
>>> 10//3
3
Example: -
Write a program which accept two numbers and print their sum.
a=int(input('Enter first number:-'))
b=int(input('Enter second number:-'))
c=a+b
print('Sum of Two Numbers is:- ', c)
Output:
Example: -
3
Write a program which accept temperature in Fahrenheit and print it in
centigrade.
c=float(f-32)/9
Example: -
Write a program which accept principle, rate and time from user and print
the simple interest.
i=(p*r*t)/100
Example: -
Write a program to calculate area of circle.
r=float(input('Enter radius of circle :'))
area=float(3.14*r*r)
print('Area of circle is :', area ,'Square unit ')
Example:
Write a program which accepts days as integer and displays total number of
years, months and days in it.
Example: -
Write a program which accepts amount as integer and displays total number
of Notes of Rs. 500, 100, 50, 20, 10, 5 and 1.
amt=int(input('Enter Amount:-'))
R500=int(amt/500)
amt=int(amt%500)
R100=int(amt/100)
amt=int(amt%100)
R50=int(amt/50)
amt=int(amt%50)
R20=int(amt/20)
amt=int(amt%20)
R10=int(amt/10)
amt=int(amt%10)
R5=int(amt/5)
amt=int(amt%5)
R1=int(amt)
print('Rs.500 : ',R500,'\nRs.100 : ',R100,'\nRs. 50 : ',R50,'\nRs. 20 : ',R20,'\
nRs. 10 : ',R10,'\nRs. 5 : ',R5,'\nRe. 1 : ',R1)
Example: -
Write a program that takes length as input in feet and inches. The program
should then convert the length in centimeters and display it on screen.
Assume that the given length in feet and inches is an integer.
5
CENTIMETERS_PER_INCH = 2.54
INCHES_PER_FOOT = 12
Example:
WAP in Python that swap value of two variables.
x = input('Enter value of x: ')
y = input('Enter value of y: ')
Enter value of x: 10
Enter value of y: 20
The value of x after swapping: 20
The value of y after swapping: 10
Relational Operators
The following table contains the relational operators that are used to check
relations.
6
Python have the following types of relational operators that are used to
check relations.
Example:
>>> 10<20
True
>>> 10>20
False
>>> 10<=10
True
>>> 20>=15
True
>>> 5==6
False
>>> 5!=6
True
>>> 10<>2
True
7
Assignment Operators
Example
>>> c=10
>>>Print(c)
10
>>> c+=5
>>>Print(c)
15
>>> c-=5
>>>Print(c)
10
>>> c*=2
>>>Print(c)
20
>>> c/=2
>>>Print(c)
10
>>> c%=3
>>>Print(c)
1
>>> c=5
8
>>> c**=2
>>>Print(c)
25
>>> c//=2
>>>Print(c)
12
Logical Operators
The following table contains the Logical operators that are used to perform
Logical operations.
Example
9
Membership Operators (Note: Discussed after if else)
The following table contains the membership operators.
Example
a=10
b=20
list=[10,20,30,40,50];
if (a in list):
print ("a is in given list")
else:
print ("a is not in given list")
if(b not in list):
print ("b is not given in list")
else:
print ("b is given in list")
Output:
a is in given list
b is given in list
10
Identity Operators
The following table contains the identity operators.
a=20
b=20
if( a is b):
else:
b=10
else:
Output
11