python programs 2-8(c)
python programs 2-8(c)
PROGRAM
OUTPUT
PROGRAM:
OUTPUT:
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:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
*
**
***
****
*****
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("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")
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("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:
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)
symmetric_difference=com1.symmetric_difference(com2)
print("after symmetric_difference:",symmetric_difference)
OUTPUT
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'}
PROGRAM:
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)
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:
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]
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: