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

Python Program

Uploaded by

aartipatil24264
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)
14 views

Python Program

Uploaded by

aartipatil24264
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/ 19

Practical:-5

Write a program related to functions & modules.


def add_n(x,y):
sum=x+y
return sum
def sub_n(t,u):
sum=t-u
return sum
def mul_n(m,o):
sum=m*o
return sum
def div_n(p,q):
sum=p/q
return sum
import add as a
num1=int(input("enter 1st no:"))
num2=int(input("enter 2nd no:"))
sum=a.add_n(num1,num2)
print("addition is:",sum)
import add as a
num1=int(input("enter 1st no:"))
num2=int(input("enter 2nd no:"))
sum=a.sub_n(num1,num2)
print("substraction is:",sum)
import add as a
num1=int(input("enter 1st no:"))
num2=int(input("enter 2nd no:"))
sum=a.mul_n(num1,num2)
print("multiplection is:",sum)
import add as a
num1=int(input("enter 1st no:"))
num2=int(input("enter 2nd no:"))
sum=a.div_n(num1,num2)
print("division is:",sum)

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

upper=int(input("please,Enter the upperst range value:"))

print("The prime numbers in the range are:")

for number in range(lower,upper+1):

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)

print("elememts at indewx 2:",numbers[2])

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'}

# Accessing elements of a dictionary


print("Name: ", my_dict['name'])
print("Age: ", my_dict['age'])
print("City: ", my_dict['city'])

# Adding elements to a dictionary


my_dict['email'] = '[email protected]'
print("Email: ", my_dict['email'])

# Removing elements from a dictionary


my_dict.pop('age')
print("Dictionary after removing age:", my_dict)
Output:-
Practical no:-4
Write a program to demonstrate the use of slicing in string.
s="code num"
print("s1",s[0:3:1])
print("s1",s[2:4])
print("s1",s[0:5:2])
print("s1",s[:4])
print("s1",s[::])
print("s1",s[0::3])
print("s1",s[-1:-4:-1])
print("s1",s[-1::-1])

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)

# Accessing elements of a tuple


print("The first element of the tuple is:", my_tuple[0])
print("The last element of the tuple is:", my_tuple[-1])

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

# Counting elements in a tuple


print("The number of times 3 appears in the tuple is:", my_tuple.count(3))

# Finding the index of an element in a tuple


print("The index of 4 in the tuple is:", my_tuple.index(4))

Output:-
Practical no:-10
Write a program to demonstrate regular expression in python.
Find all:
import re

txt="Wel-come to India"

#Check if "Portugal"is in the string:

X=re.findall("India",txt)

print(X)

if(X):

print("your text is Match")

else:

print("No Match")

Search:
import re

txt="The rain in india"

X=re.search("india",txt)

print(X)

Split:
import re

#Split the string at the first while-space character:

txt="Wel-Come to India"

X=re.split("\s",txt,1)

print(X)

Sub:
import re

#Replace all white-space characters with the sigit"9":

txt="Hello Friend,Wel-Come to India"

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

You might also like