0% found this document useful (0 votes)
8 views

python programs 2-8(c)

Uploaded by

smk1125203
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

python programs 2-8(c)

Uploaded by

smk1125203
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

2(a) EXCHANGE THE VALUES OF TWO VARIABLES

PROGRAM

a=int(input("enter the number"))


b=int(input("enter the number"))
print ("before swapping of a,b:"a,b)
c=a
a=b
b=c
print("after swapping of a,b:"a,b)

OUTPUT

enter the number


22
enter the number
25
before swapping of a,b: 22 25
after swapping of a,b: 25 22
2(b) DISTANCE BETWEEN TWO POINTS

PROGRAM:

x1=int(input("enter the number"))


x2=int(input("enter the number"))
y1=int(input("enter the number"))
y2=int(input("enter the number"))
d=((x2-x1)**2+(y2-y1)**2)/2
print(d)

OUTPUT:

enter the number67


enter the number45
enter the number98
enter the number29
2622.5
2(C) CIRCULATE THE VALUES OF n VARIABLES IN LIST

PROGRAM:
list=[ ]
n=int(input("enter the list count"))
for i in range(0,n+1):
le=int(input("enter the list element"))
list.append(le)
print("circulating the elements of list",list)
for i in range(0,n+1):
t=list.pop(0)
list.append(t)
print(list)

OUTPUT:
enter the list count3
enter the list element4
enter the list element5
enter the list element6
enter the list element7
circulating the elements of list [4, 5, 6, 7]
[5, 6, 7, 4]
[6, 7, 4, 5]
[7, 4, 5, 6]
[4, 5, 6, 7]
3(a) FIBONACCI SERIES

PROGRAM:

fib=0
a=0
b=1
n=int(input("enter the number"))
print("fibonacci series")
if(n==0):
print("0")
else:
for i in range(1,n+1):
fib=fib+a
a=b
b=fib
print(fib,end=" ")

OUTPUT:

enter the number 9


fibonacci series
0 1 1 2 3 5 8 13 21
3(b) NUMBER PATTERN

PROGRAM:

n=int(input("enter the number of lines"))


for i in range(1,n+1):
for j in range(1,i+1):
print(j,end=" ")
print(" ")

OUTPUT:

enter the number of lines7


1
12
123
1234
12345
123456
1234567
3(c) PYRAMID PATTERN

PROGRAM:

r=int(input("enter the number of rows"))


for i in range (r+1):
for j in range(r-i):
print("",end=" ")
for j in range(i):
print("*",end=" ")
print( )

OUTPUT:

enter the number of rows5

*
**
***
****
*****
4(a) OPERATION OF LIST

PROGRAM:

con=['concrete','cement','brick','construction aggregate','mortar','metal','steel','concrete
block','wood','clay']
print("the construction element:",con)

print("INDEXING")

print("con.index",con[3])
print("con.index",con[0])
print("con.index",con[7])

print("NEGATIVE INDEXING")

print("con.negative index",con[-5])
print("con.negative index",con[-2])
print("con.negative index",con[-9])

print("SLICING")

print("con.slice",con[3:8])
print("con.slice",con[0:4])
print("con.slice",con[5:7])

print("APPENDING")

print("before appending of con",con)


print("con.append",con.insert(2,'mortal'))
print("after appending of con",con)

print("POPING")

print("con.pop(4)",con.pop(4))
print("con.pop(7)",con.pop(7))

print("SORTING LIST")

con.sort()
print("after sorting of con:",con)

print("LISTCOUNT")

print("number of elements in con", len(con))


OUTPUT:

the construction element: ['concrete', 'cement', 'brick', 'construction aggregate', 'mortar',


'metal', 'steel', 'concrete block', 'wood', 'clay']
INDEXING
con.index construction aggregate
con.index concrete
con.index concrete block
NEGATIVE INDEXING
con.negative index metal
con.negative index wood
con.negative index cement
SLICING
con.slice ['construction aggregate', 'mortar', 'metal', 'steel', 'concrete block']
con.slice ['concrete', 'cement', 'brick', 'construction aggregate']
con.slice ['metal', 'steel']
APPENDING
before appending of con ['concrete', 'cement', 'brick', 'construction aggregate', 'mortar', 'metal',
'steel', 'concrete block', 'wood', 'clay']
con.append None
after appending of con ['concrete', 'cement', 'mortal', 'brick', 'construction aggregate', 'mortar',
'metal', 'steel', 'concrete block', 'wood', 'clay']
POPING
con.pop(4) construction aggregate
con.pop(7) concrete block
SORTING LIST
after sorting of con: ['brick', 'cement', 'clay', 'concrete', 'metal', 'mortal', 'mortar', 'steel',
'wood']
LISTCOUNT
number of elements in con 9
4(b) OPERATION OF TUPLE

PROGRAM:

mtup=("audiobooks","novels","magazines","journals","comics","dictionary","encyclopedia",
"biography","story teller")
print("the library elements:",mtup)

print("INDEXING")
print("mtup.index",mtup[0])
print("mtup.index",mtup[4])
print("mtup.index",mtup[7])

print("NEGATIVE INDEXING")
print("mtup.negative index",mtup[-1])
print("mtup.negative index",mtup[-5])
print("mtup.negative index",mtup[-8])

print("LENGTH OF THE ELEMENTS")


print("number of eleements in mtup",len(mtup))

print("CONCATENATED ELEMENT")
mytuple=mtup+("motivational book","literature","poetry")
print("mytuple.concatenated:",mytuple)

print("REPEATED ELEMENT")
print("mytuple.repeated:",mytuple*2)

print("BOOLEAN")
print("novels" in mtup)
print("case study" in mtup)

print("PACKING")
packing=('A','N','M','J','C','D','E','B','S')
(A,N,M,J,C,D,E,B,S)=mtup
print("packing:",packing)
print("mtup:",mtup)

print("UNPACKING")
print("A-",A)
print("N-",N)
print("M-",M)
print("J-",J)
print("C-",C)
print("D-",D)
print("E-",E)
print("B-",B)
print("S-",S)
OUTPUT:

the library elements: ('audiobooks', 'novels', 'magazines', 'journals', 'comics', 'dictionary',


'encyclopedia', 'biography', 'story teller')
INDEXING
mtup.index audiobooks
mtup.index comics
mtup.index biography
NEGATIVE INDEXING
mtup.negative index story teller
mtup.negative index comics
mtup.negative index novels
LENGTH OF THE ELEMENTS
number of eleements in mtup 9
CONCATENATED ELEMENT
mytuple.concatenated: ('audiobooks', 'novels', 'magazines', 'journals', 'comics', 'dictionary',
'encyclopedia', 'biography', 'story teller', 'motivational book', 'literature', 'poetry')
REPEATED ELEMENT
mytuple.repeated: ('audiobooks', 'novels', 'magazines', 'journals', 'comics', 'dictionary',
'encyclopedia', 'biography', 'story teller', 'motivational book', 'literature', 'poetry', 'audiobooks',
'novels', 'magazines', 'journals', 'comics', 'dictionary', 'encyclopedia', 'biography', 'story teller',
'motivational book', 'literature', 'poetry')
BOOLEAN
True
False
PACKING
packing: ('A', 'N', 'M', 'J', 'C', 'D', 'E', 'B', 'S')
mtup: ('audiobooks', 'novels', 'magazines', 'journals', 'comics', 'dictionary', 'encyclopedia',
'biography', 'story teller')
UNPACKING
A- audiobooks
N- novels
M- magazines
J- journals
C- comics
D- dictionary
E- encyclopedia
B- biography
S- story teller
4(c) LIST TO TUPLE CONVERSION

PROGRAM:

mylist=['brake','radiator','bumper','steering','engine','wheels','piston','hood','headlight','license
plate']
mytup=()
for i in mylist:
mytup+=(i,)
print("original list:",mylist)
print("converted tuple:",mytup)

OUTPUT:

original list: ['brake', 'radiator', 'bumper', 'steering', 'engine', 'wheels', 'piston', 'hood',
'headlight', 'license plate']
converted tuple: ('brake', 'radiator', 'bumper', 'steering', 'engine', 'wheels', 'piston', 'hood',
'headlight', 'license plate')
5(a) OPERATIONS OF SET

PROGRAM:

com1={'engine','gear','fuel','brake'}
com2={'brake','radiator','wheel','clutch'}

print("com1=",com1)
print("com2=",com2)

print("ADD OPERATION")

com1.add('drive shaft')
print("after adding('drive shaft')in com1:",com1)
com2.add('axle')
print("after adding ('axle') in com2:",com2)

print("UNION OPERATION")

union=com1.union(com2)
print("after union of two sets:",union)

print("INTERSECTION OPERATION")

intersection=com1.intersection(com2)
print("after intersection of two sets :",intersection)

print("DIFFERENCE OPERATION")

difference=com1.difference(com2)
print("after difference of two sets:",difference)

print("SYMMETRIC DIFFERENCE OPERATION")

symmetric_difference=com1.symmetric_difference(com2)
print("after symmetric_difference:",symmetric_difference)
OUTPUT

com1= {'gear', 'brake', 'engine', 'fuel'}


com2= {'brake', 'radiator', 'clutch', 'wheel'}

ADD OPERATION
after adding('drive shaft')in com1: {'brake', 'drive shaft', 'gear', 'engine', 'fuel'}
after adding ('axle') in com2: {'brake', 'clutch', 'wheel', 'radiator', 'axle'}

UNION OPERATION
after union of two sets: {'brake', 'clutch', 'drive shaft', 'gear', 'wheel', 'engine', 'radiator', 'fuel',
'axle'}

INTERSECTION OPERATION
after intersection of two sets : {'brake'}

DIFFERENCE OPERATION
after difference of two sets: {'gear', 'engine', 'fuel', 'drive shaft'}

SYMMETRIC DIFFERENCE OPERATION


after symmetric_difference: {'drive shaft', 'clutch', 'wheel', 'gear', 'radiator', 'engine', 'fuel',
'axle'}
5(b) OPERATION OF DICTIONARY

PROGRAM:

civil={"Floors": "Horizontal levels of the building","slabs":'horizontal surfaces',


"foundation": 'Supports entire structure',"retaining wall": 'Supports soil mass',}
print("the original dictionary:",civil)

print("COPIED DICTIONARY")
copy=civil.copy()
print("after copying dictionary:",copy)

print("GET VALUE")
get=civil.get("foundation")
print("after getting the value:",get)

print("POP VALUE")
pop=civil.pop("retaining wall")
print("after popped dictionary:",civil)

print("UPDATED VALUE")
newdata=('overhead covering')
civil["roof"]=newdata
print("after updating data:",civil)

print("the final updated dictionary:",civil)


OUTPUT:

the original dictionary: {'Floors': 'Horizontal levels of the building', 'slabs': 'horizontal
surfaces', 'foundation': 'Supports entire structure', 'retaining wall': 'Supports soil mass'}
COPIED DICTIONARY
after copying dictionary: {'Floors': 'Horizontal levels of the building', 'slabs': 'horizontal
surfaces', 'foundation': 'Supports entire structure', 'retaining wall': 'Supports soil mass'}
GET VALUE
after getting the value: Supports entire structure
POP VALUE
after popped dictionary: {'Floors': 'Horizontal levels of the building', 'slabs': 'horizontal
surfaces', 'foundation': 'Supports entire structure'}
UPDATED VALUE
after updating data: {'Floors': 'Horizontal levels of the building', 'slabs': 'horizontal surfaces',
'foundation': 'Supports entire structure', 'roof': 'overhead covering'}
the final updated dictionary: {'Floors': 'Horizontal levels of the building', 'slabs': 'horizontal
surfaces', 'foundation': 'Supports entire structure', 'roof': 'overhead covering'}
6(a) FACTORIAL USING FUNCTION

PROGRAM:
def factorial(n):
if n==1:
return n
else:
return n*factorial(n-1)
num=int(input("enter the number"))
if num<0:
print("sorry,factorial does not exist for negative number")
elif num==0:
print("the factorial of 0 is 1")
else:
print("the factorial of", num, "is" ,factorial(num))
OUTPUT:
enter the number7
the factorial of 7 is 5040
6(b) FIND LARGEST NUMBER IN THE LIST USING FUNCTION

PROGRAM:
def max_list(list1):
max=list1[0]
for i in list1:
if i>max:
max=i
return max
list1=[25,56,99,103,22]
print("the max element in list",max_list(list1))

OUTPUT:
the max element in list 103
6(c) AREA OF SHAPES USING FUNCTION

PROGRAM:
def square(side):
return side*side
def rectangle(length,width):
return length*width
side=input("enter side:")
side=float(side)
length=input("enter length:")
width=input("enter width:")
length=float(length)
width=float(width)
print("area of square:",square(side))
print("area of rectangle:",rectangle(length,width))

OUTPUT:

enter side:5
enter length:4
enter width:6
area of square: 25.0
area of rectangle: 24.0
7(a) REVERSE A STRING

PROGRAM:

def reverse(str):
revstr="".join(reversed(str))
return revstr
string=input("enter a string: ")
print("original string:",string)
print("reversed string:",reverse(string))

OUTPUT:

enter a string : anushyabharathi


original string: anushyabharathi
reversed string: ihtarahbayhsuna
7(b) PALINDROME

PROGRAM:

def reverse(str):
revstr=""
for j in str:
revstr=j+revstr
return revstr
string=input("enter a string: ")
print("original string:",string)
revstr=reverse(string)
print("reversed string:",reverse(string))
if string==revstr:
print("PALINDROME")
else:
print("NOT A PALINDROME")

OUTPUT:
enter a string: malayalam
original string: malayalam
reversed string: malayalam
PALINDROME
7(c) CHARACTER COUNT

PROGRAM:

import string
str="HI, VERY GOOD MORNING TO ALL 123"
strcount=len([a for a in str if a in string.ascii_uppercase or a in string.ascii_lowercase])
print("count of characters=",strcount)

OUTPUT:

count of characters= 22
8(a) PANDAS LIBRARY MODULE

PROGRAM:

import pandas as pd
data=pd.Series([1,5,9,15,21],index=['a','e','i','o','u'])
data2=pd.Series(['anubharathi',18,9408,'CSE'],['name','age','spr.no','deptarment'])
print(data, "\n\n\n" ,data2)

OUTPUT:
a 1
e 5
i 9
o 15
u 21
dtype: int64

name anubharathi
age 18
spr.no 9968
deptarment CSE
dtype: object
8(b) NUMPY LIBRARY MODULE

PROGRAM:

import numpy as Np
data=Np.array([1,5,9,15,21])
data2=Np.array(['anubharathi','irene','hariharan','bharathi'])
print(data, "\n\n" ,data2)

OUTPUT:

[ 1 5 9 15 21]

['anubharathi' 'irene' 'hariharan' 'bharathi']


8(c) MATPLOTLIB LIBRARY MODULE

PROGRAM:
from matplotlib import pyplot as plt

xpoints = [0,2,4,6,8]
ypoints = [1,3,5,7,9]

plt.plot(xpoints, ypoints)
plt.show()

OUTPUT:

You might also like