Python Program
Python Program
output:-
Practical no:-3
Write a program to print “n” terms of Fibonacci series using iteration.
num=int(input("enter the number:"))
print("below is fibonacci series:")
a,b=1,2
for i in range (0,num):
if i<=1:
print(i)
else:
result=a+b
b=result
print(result)
Output:-
Program:-2
Write a program to find all prime numbers within a given range.
lower=int(input("please,Enter the lowerst range value:"))
if number>1:
for i in range(2,number):
if(number%i)==0:
break
else:
print(number)
output:-
Practical no :-1
Installing python and setting up environment. simple statement like printing the names
(hello world),numbers, mathematical calculations etc.
Mathematical calculations
a=10
b=20
#addition
print('sum:',a+b)
#substraction
print('sub:',a-b)
#multiplication
print('mul:',a*b)
#division
print('div:',a/b)
#modulous
print('mod:',a%b)
# a to the power b
print('power:',a**b)
Output:-
Practical no:-11
Write a program to demonstrate the working of class and object
class Student:
def setdata(self):
self.roll=int(input("Enter roll Number:"))
self.name=input("Enter name:")
self.mathmark=int(input("Enter mathmark:"))
self.cprogrammingmark=int(input("Enter cprogrammingmark:"))
self.englishmark=int(input("Enter englishmark:"))
def display(self):
print("roll:",self.roll)
print("name:",self.name)
print("mathmark:",self.mathmark)
print("cprogrammingmark:",self.cprogrammingmark)
print("englishmark:",self.englishmark)
a=Student()
a.setdata()
a.display()
output:-
Practical no:-7
Write a program to demonstrate the use of list & related functions.
numbers=[1,2,3,4,5]
print("orinal list;",numbers)
numbers.append(6)
print("list after appending 6:",numbers)
numbers.remove(3)
print("list after removeing 3:",numbers)
numbers.sort()
print("list after sorting:",numbers)
numbers.reverse()
print("list after reversing the order of element:",numbers)
output:-
Practical no:-8
Write a program to demonstrate the use of dictionary & related
functions.
# Create a dictionary
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
Output:-
Practical:-6
Write a program that demonstrate concept of functional
programming.
num=int(input("enter the number:"))
print("below is fibonacci series:")
a,b=1,2
for i in range (0,num):
if i<=1:
print(i)
else:
result=a+b
b=result
print(result)
Output:-
Practical no:- 9
Write a program to demonstrate the use of tuple.
# Creating a tuple
my_tuple = (1, 2, 3, 4, 5)
# Slicing a tuple
print("The first three elements of the tuple are:", my_tuple[:3])
# Concatenating tuples
new_tuple = my_tuple + (6, 7, 8)
print("The concatenated tuple is:", new_tuple)
Output:-
Practical no:-10
Write a program to demonstrate regular expression in python.
Find all:
import re
txt="Wel-come to India"
X=re.findall("India",txt)
print(X)
if(X):
else:
print("No Match")
Search:
import re
X=re.search("india",txt)
print(X)
Split:
import re
txt="Wel-Come to India"
X=re.split("\s",txt,1)
print(X)
Sub:
import re
X=re.sub("\s","0",txt)
print(X)
Output:-
Practical no:-12
Write a program to demonstrate the working of Inheritance.
class Department:
def show_dept(self):
print("This is our dept:")
# HoD class inherited from Department
class hod(Department):
hodname=""
def show_hod(self):
print(self.hodname)
#Teacher class inherited from Department
class Teacher(Department):
teachername=""
def show_teacher(self):
print(self.techername)
# Student class inherited from hod and Teacher classes
class Student(hod, Teacher):
def show_staff(self):
print("HoD:", self.hodname)
print("Teacher:", self.teachername)
s1 = Student()# Object of Student class
s1.hodname = "Ramnath"
s1.teachername = "Swami"
s1.show_dept()
s1.show_staff()Output:-
Practical no:-13
Write a program to demonstrate the working of overloading
methods.
class Opt:
def mul(self,a=None,b=None ,c=None):
if a !=None and b!=None and c!=None:
print("Multiplication of three Parameter:",a*b*c)
elif a !=None and b !=None:
print("Multiplication of Two Parameter:",a*b)
X=int(input("Enter 1st Number:"))
Y=int(input("Enter 2nd Number:"))
Z=int(input("Enter 3rd Number:"))
o1=Opt()
o1.mul(X,Y)
o1.mul(X,Y,Z)
output:-
Practical no:-14
Write a program to demonstrate Exception Handling Mechanism.
try:
numberator=int(input("Enter no.:"))
denomiator=int(input("Enter no.:"))
result=numerator/denominator
print(result)
except:
print("Error:Denominator cannot be 0.")
finally:
print("This is finally block.")
Output:-