Chapter 2 Python Operators

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

PYTHON

An object oriented programming language

Contents

● Python Operators

Aniket Goswami
PGT – Computer Science.
Vivekananda Kendra Vidyalaya, Hurda.
Dist – Bhilwara
Mo. 9829906113

1
Python Operators

Operators are particular symbols that are used to perform operations on


operands. It returns result that can be used in application.

Example

4+5=9
4*5 = 20

Python supports the following operators:

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:

Enter first number:-10


Enter second number:-20
Sum of Two Numbers is:- 30

Example: -

3
Write a program which accept temperature in Fahrenheit and print it in
centigrade.

f=float(input('Enter temperature in Fahrenheit :'))

c=float(f-32)/9

print('Temperature in Celsius is : ', c)

Example: -
Write a program which accept principle, rate and time from user and print
the simple interest.

p=int(input('Enter Principle :'))

r=int(input('Enter Rate :'))

t=int(input('Enter Time :'))

i=(p*r*t)/100

print('Simple interest is : ', i)

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.

total_days=int(input('Enter Days Here:-'))


years=int(total_days/365)
4
num=(total_days%365)
weeks=int(num/7)
days=num%7
print('Years=',years)
print('Weeks=',weeks)
print('Days=',days)

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

feet=int(input('Enter one integer for feet :'))


inches=int(input('Enter one integer for inches :'))
print('The numbers you entered are ', feet ,' for feet and ', inches ,' for inches. ')
totalInches = INCHES_PER_FOOT * feet + inches;
print('The total number of inches = ', totalInches )
centimeter = CENTIMETERS_PER_INCH * totalInches;
print('The number of centimeters = ', centimeter )

Example:
WAP in Python that swap value of two variables.
x = input('Enter value of x: ')
y = input('Enter value of y: ')

# create a temporary variable and swap the values


temp = x
x=y
y = temp

print('The value of x after swapping:',x)


print('The value of y after swapping:',y)
Output:

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

>>>a=5>4 and 3>2


>>>print (a)
True
>>>b=5>4 or 3<2
>>>print (b)
True
>>>c=not(5>4)
>>>print (c)
False

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):

print ("a,b have same identity")

else:

print ("a, b are different")

b=10

if( a is not b):

print ("a,b have different identity")

else:

print ("a,b have same identity")

Output

a,b have same identity


a,b have different identity

11

You might also like