Python Practical File (1)
Python Practical File (1)
(DSEU)
Department of BSc (Data Analytics)
Bhai Parmanand DSEU Shakarpur Campus -II
65 Fibonnaci Series
Output:-
Objective: write a program to input the coefficients a,
b and c for a quadratic equation and calculate the
determinant and roots of the same
Prodecure:
Code:-
#quadratic equation
a=int(input('enter the value of a'))
b=int(input('enter the value of b'))
c=int(input('enter the value of c'))
d=b**2-4*a*c
print(d)
if d>0:
r=(-b+d**1/2)/2*a
f=(-b-d**1/2)/2*a
print('roots are real and uneqaul with roots',r,f)
elif d==0:
h=-b/2*a
print("roots are real and equal with having roots",h,h)
elif d<0:
print("roots are not real”)
Output:
Objective: write a program to input the temperature
in Fahrenheit and convert it into Celcius
Prodecure:
Code:-
#temperature conversion
tem=int(input("enter the value of temperature in
Fahrenhei: "))
t=(tem-32)*5/9
print("temperature in Celsius: ",t)
print("-----------------")
Output:-
Objective: calculate simple interest by using input
from user of principle, rate if interest and time period
Prodecure:
Code:-
#simple interest
p=int(input("enter the value of p: "))
r=int(input("enter the value of r: "))
t=int(input("enter the value of t(in months): "))
si=(p*r*t)/100
print("simple interest: ",si)
Output
Write a program to swap two number with and
without taking a third variable
a,b = 1,2
print(a,b)
b,a = 2,1
print(b,a)
DAY 3
1:- write a program to find the greatest of 3 numbers
inputted by the user
a=int(input("enter the number: "))
b=int(input('enter the second number: '))
c=int(input('enter the third: '))
a = int(input("enter "))
i = int(input("enter "))
while i>=a:
a=a+1
if a%5 == 0:
continue
elif a%2 == 0:
print(a)
factorial of a number
# Driver program
date1 = date(2020, 12, 13)
date2 = date(2021, 2, 25)
print(numOfDays(date1, date2), "days")
Calculate total salary of the person and tax is 15% of
the basic which is to be deducted.
L=[2,4,5,"Hi",74,"Hello","Good Night"]
for u in L:
print(u)
Write a program to print all the elements in a list
which are divisible by 5.
Input:
L1=[5,4,9,10,45,89,45445,789,895,963,67,70]
print("Elements Divisible by 5:")
for i in L1:
if i%5==0:
print(i)
Write a program to input a list and print it using a user defined function.
Input:
def prn(n):
print(n)
#main
l=list(input("Enter Elements of the list:"))
prn(l)
Write a program to concatenate two lists and to
repeat a list.
print("1.Concatenation\n2.Repeatition\n3.Slicing\n4.Member
ship")
L3=[1,5,6,88,445,454,"Hello"]
L4=[51,848,51,151,515,151,55,51,212121]
print("Your lists:\n1.",L3,"\n2.",L4)
#main
ans="y"
while ans=="y":
q=int(input("Enter Your Choice:"))
if q==1:
print("Concatenation of both list is:",L3+L4)
elif q==2:
po=int(input("Choose list:"))
og=int(input("How many times you want to repeat:"))
if po==1:
print(L3*og)
elif po==2:
print(L4*og)
else:
print("wrong input.")
elif q==3:
lc=int(input("Choose list:"))
fi=int(input("enter starting point:"))
la=int(input("enter ending point:"))
if lc==1:
print(L3[fi:la])
elif lc==2:
print(L4[fi:la])
else:
print("wrong input.")
elif q==4:
lc1=int(input("Choose list:"))
print("Search type\n1.string\n2.numeric")
sea_type=int(input("what you want to search:"))
if sea_type==1 and lc1==1:
st=input("Enter String:")
if st in L3:
print(st,"is present in the list.")
else:
print(st,"is not present in the list.")
elif sea_type==1 and lc1==2:
st1=input("Enter String:")
if st1 in L3:
print(st1,"is present in the list.")
else:
print(st1,"is not present in the list.")
elif sea_type==2 and lc1==1:
int1=int(input("Enter number:"))
if int1 in L3:
print(int1,"is present in the list.")
else:
print(int1,"is not present in the list.")
elif sea_type==2 and lc1==2:
int2=int(input("Enter number:"))
if int2 in L3:
print(int2,"is present in the list.")
else:
print(int2,"is not present in the list.")
else:
print("wrong input.")
else:
print("wrong input.")
ans=input("Do you want to continue:\ny/n:")
if ans=="n" or ans=="N":
print("tata bye bye :)")
Write a program to print index of the input element in
a list.
L5=[45,545,6,848,12,55,484]
Choose=int(input("Search number:"))
if Choose in L5:
for z in range(len(L5)):
if L5[z]==Choose:
print("Entered number is present and index
is:",z)
else:
print("Entered number is not in the list")
Tabulate some of the important list functions.
Method Description
append() Adds an element at the end of
the list
clear() Removes all the elements from
the list
copy() Returns a copy of the list
count() Returns the number of elements
with the specified value
extend() Add the elements of a list to the
end of the current list
index() Returns the index of the first
element with the specified value
insert() Adds an element at the specified
position
pop() Removes the element at the
specified position
remove() Removes the first item with the
specified value
reverse() Reverses the order of the list
sort() Sorts the list
Write a program to add all the elements in a list.
L2=[4,8,6,245,9,44,25,54,32,56,78,45,4514,956]
def add_list():
sum=0
for k in L2:
sum+=k
print("sum of all element is:",sum)
print("Given list:",L2)
add_list()
Write a program to return prime number in a tuple.
t1=(2,4,6,5,48,89,81,77,11,52)
print("All prime number present in tuple are:")
for ele in t1:
condi=True
for u in range (2,ele):
if ele%u==0:
condi=False
break
if condi:
print(ele)
Write a program to return even indexed number in a
tuple.
Input:
t2=(2,4,6,5,48,89,81,77,11,52,5454,74,545)
print("All even indexed elements in the tuple are:")
for index in range(0,len(t2)):
if index%2==0:
print(t2[index])
Write a program to sort a tuple in increasing or
decreasing order.
Input:
t3=(-1,2,5,9,55,15,-10,0,9,18)
print("Given Tuple:",t3)
print("Sorted Tuple in Ascending Order:",sorted(t3))
print("Sorted Tuple in Descending
Order:",sorted(t3,reverse=True))
Write a program to remove duplicates from a tuple.
Input:
t4=(12,8,45,-15,12,84,-15,8,56)
print("Original Tuple:",t4)
print("Tuple after removing all
duplicates:",tuple(set(t4)))
Write a program to add and remove items from set.
Input:
s1={7,8,0,2,"hello"}
print(s1)
s1.discard(7)
s1.add("bye")
print(s1)
Write a program to remove an item from set if it is
present in set.
Input:
s1={7,8,0,2,"hello"}
a=60
if a in s1:
s1.discard(a)
print(s1)
else:
s1.add(a)
print(s1)
Write a program to create intersection, union,
difference of two sets.
Input:
s1={7,8,0,2,"hello"}
s2={84,18,73,7,0}
s3=s1.union(s2)
s4=s1.intersection(s2)
s5=s1.difference(s2)
print("union",s3)
print("intersection",s4)
print("difference",s5)
Write a program to create intersection, union,
difference of two frozen sets.
Input:
set1=frozenset(s3)
set2=frozenset(s4)
uni=set1.union(set2)
diff=set1.difference(set2)
inter=set1.intersection(set2)
print("union",uni)
print("intersection",diff)
print("difference",inter)
Write a program to study built in methods related to
set.
Input:
print(dir(set))
Write a program to find length of a set using loops.
Input:
set3={1,2,35,56,6,90}
set4={99,218,56,3,2,6}
set5=set3.copy()
for ele5 in set3:
if ele5 in set4:
set5.discard(ele5)
print('Q7 new set:',set5)
Write a program to find the element in a given set
that are not in another set using loops, conditional
statement and functions.
Input:
set3={1,2,35,56,6,90}
set4={99,218,56,3,2,6}
set6=set()
for ele3 in set3:
if ele3 not in set4:
set6.add(ele3)
print(‘Old set’,set3)
print(“New Set”,set6)
Write a python program to get the path and name of
the file that is currently executing
import os
i = int(input("enter "))
for t in range(i):
if t%2 == 0:
print( t , "is even")
else :
print(t , "is odd")
su = 0
te = t
while te>0:
digit = te%10
su += digit**3
te //= 10
if t == su:
print(t,"is armstrong")
Fibonnaci Series
n=int(input('Enter the value of n:'))
a=0
b=1
sum=0
count=1
print('Fibonnaci Series:',end='')
while(count<=n):
print(sum,end='')
count+=1
a=b
b=sum
sum=a+b
Output:
Write a python program to determine if a python
shell is executing in 32 bit or 64 bit mode.
import platform
print(platform.architecture()[0])
WAP to count no. of lines in file WAP to count no. of
lines in file
i = input("enter the file name ")
lines = open(i,"r")
cou = 0
for line in lines:
cou = cou + 1
print("total no.of lines is", cou)
WAP to read specific lines in a file.
i = input("enter the file name ")
lines = open(i,"r")
cou = str()
for line in lines:
if line.startswith("From"):
cou = line