0% found this document useful (0 votes)
20 views30 pages

Mybook Final

The document contains a series of Python programming experiments demonstrating various concepts such as dictionaries, recursion, modules, inheritance, error handling, and loops. Each experiment includes source code and expected outputs, covering topics like score tracking, factorial calculation, email management, multilevel inheritance, and more. The document serves as a comprehensive guide for learning Python programming through practical examples.

Uploaded by

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

Mybook Final

The document contains a series of Python programming experiments demonstrating various concepts such as dictionaries, recursion, modules, inheritance, error handling, and loops. Each experiment includes source code and expected outputs, covering topics like score tracking, factorial calculation, email management, multilevel inheritance, and more. The document serves as a comprehensive guide for learning Python programming through practical examples.

Uploaded by

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

PHILIP WANDAWA

012220111

EXPERIEMENT-1
Write a program to demonstrate working with dictionaries in python
# Write a program to read scores of n players

# Each player is having name and runs scored

Source Code:

p={}

while True:

print("1. Adding")

print("2. Updating")

print("3. Finding")

print("4. Score Board")

print("5. Exit")

opt=int(input("Enter your option"))

if opt==1:

name=input("Enter Name")

score=int(input("Enter Score"))

if name in p:

print(name,"is exists")

else:

p[name]=score

elif opt==2:

name=input("Enter Name")

if name in p:

s=int(input("Enter Updated Score"))

p[name]=s

else:

print(name,"not exists")

elif opt==3:

name=input("Enter name")
PHILIP WANDAWA
012220111

if name in p:

s=p[name]

print("score ",s)

else:

print(name,"is not exists")

elif opt==4:

tot=0

for x in p.items():

name,score=x

print(name,score)

tot=tot+score

print("Total Score :",tot)

elif opt==5:

break

OUTPUT:
PHILIP WANDAWA
012220111

EXPERIMENT-2

Write a python program to find factorial of a number using recursion:

Source Code

def factorial(n):

if n==0:

return 1

else:

return n*factorial(n-1)

def main():

num=int(input("enter any number"))


PHILIP WANDAWA
012220111

res=factorial(num)

print(f'factorial is {res}')

main()

Output:

EXPERIMENT-3
Write a python program to define a module and import a specific function in
that module to another module.
Source code:

module1.py 🡪 module-Philip1

x=100 #global variable

y=200 #global variable

def fun1():

print("inside fun1 of module1")

def fun2():

print("inside fun2 of module1")

module2.py 🡪 module-Philip2
PHILIP WANDAWA
012220111

from module1 import *

def main():

print(x)

print(y)

fun1()

fun2()

main()

Output:
PHILIP WANDAWA
012220111

EXPERIEMNT-4
PHILIP WANDAWA
012220111

Write a python program to define a module and import the content of other
module with alias name used.
module6.py 🡪 module-Wandawa1

Source Code:

x=100 #global

y=200 #global

def fun1():

print("inside fun1 of module6")

def fun2():
print("inside fun2 of module6")

module7.py 🡪 module-Wandawa2

from module6 import x as k

from module6 import fun1 as f1

def fun1():

print("fun1 of current module")

x=99

def main():

print(x)

fun1()

print(k)

f1()

main()

Output:
PHILIP WANDAWA
012220111

EXPERIEMENT-5
PHILIP WANDAWA
012220111

How to access modules saved inside other locations into current module?
Source Code:

Create a module8.py and save this module in e:\\

def fun1():

print("fun1 of module8")

Create a module9.py

import sys

sys.path.append("e:\\")

import module8

def main():

module8.fun1()

main()

Output:
PHILIP WANDAWA
012220111
PHILIP WANDAWA
012220111

EXPERIMENT-6
Write a program to demonstrate working with LIST in python

Source Code:

# write a program to read the scores of n players

# and display total,max,min

n=int(input("enter how many players?")) # 5

scores=[]

for i in range(n):# range(5) --> 0 1 2 3 4

r=int(input("Enter score"))

scores.append(r)

print("Total Score :",sum(scores))

print("Maximum Score :",max(scores))

print("Minimum Score :",min(scores))

Output:
PHILIP WANDAWA
012220111

EXPERIMENT-7
Write a program to demonstrate working with SET in python.

Source code:

# Email Book Application

email_set=set()

while True:

print("***Email Book***")

print("1. Add")

print("2. Search")

print("3. Remove")

print("4. List")

print("5. Exit")

opt=int(input("enter your option"))

if opt==1:

email_id=input("Enter Email-id")

if email_id in email_set:

print("this email id exists")

else:

email_set.add(email_id)

elif opt==2:

email_id=input("Enter Email_id")

if email_id in email_set:

print(email_id,"is found")

else:

print("this email id not exists")

elif opt==3:

email_id=input("Enter Email_id")

if email_id in email_set:

email_set.remove(email_id)
PHILIP WANDAWA
012220111

else:

print("this email id not exists")

elif opt==4:

for email_id in email_set:

print(email_id)

elif opt==5:

break

Output:
PHILIP WANDAWA
012220111

EXPERIMENT-8
Implement multilevel inheritance in python.
Source code:

# Multilevel inheritance

class Animal:

def speak(self):

print("Animal Speaking")

#The child class Dog inherits the base class Animal

class Dog(Animal):

def bark(self):

print("dog barking")

#The child class Dogchild inherits another child class Dog

class DogChild(Dog):

def eat(self):

print("Eating bread...")

d = DogChild()

d.bark()

d.speak()

d.eat()
PHILIP WANDAWA
012220111

Output:

Experiment 9

Implement the working of packages.

Create a folder with name package1 in E:\\

Inside that package create a python program(module)

m1.py

Source code:

def fun1():

print("inside fun1")
PHILIP WANDAWA
012220111

def fun2():

print("inside fun2")

def fun3():

print("inside fun3")

m2.py

def fun4():

print("hello ")

Create a module in current working directory Test183.py

import sys

sys.path.append("d:\\")

import package1.m1

import package1.m2

def main():

package1.m1.fun1()

package1.m1.fun2()

package1.m1.fun3()

package1.m2.fun4()

main()
PHILIP WANDAWA
012220111

Output:
PHILIP WANDAWA
012220111

Experiment 10:
Implement calculator program using functions
Source code:

def calculator(n1,n2,opr):

def add():

return n1+n2

def sub():

return n1-n2

def multiply():

return n1*n2

def div():
PHILIP WANDAWA
012220111

return n1/n2

if opr=='+':

print(f'Result is {add()}')

elif opr=='-':

print(f'Result is {sub()}')

elif opr=='*':

print(f'Result is {multiply()}')

elif opr=='/':

print(f'Result is {div()}')

def main():

num1=int(input("Enter First Number"))

num2=int(input("Enter Second Number"))

opr=input("Enter Operator")

calculator(num1,num2,opr)

main()
PHILIP WANDAWA
012220111

Output:

EXPERIMENT-11
Write a Python program that accepts two integers from the user and divides
the first number by the second. Handle potential errors such as division by
zero and invalid input types (non-integer inputs)by using exception handling.
Display appropriate error messages in each case.

Source code:

# program for dividing two numbers


PHILIP WANDAWA
012220111

try:

n1=int(input("enter first number"))

n2=int(input("enter second number"))

n3=n1/n2

print(f'result is {n3}')

except ZeroDivisionError:

print("cannot divide number with zero")

except ValueError:

print("input must be integer type")

Output:
PHILIP WANDAWA
012220111

EXPERIMENT-12
Write a Python program that prompts the user to enter a number and then
prints its multiplication table up to 10.

Source code:

num=int(input("Enter any number"))

i=1

while i<=20:

p=num*i

print(num,"x",i,"=",p)

i=i+1

Output:
PHILIP WANDAWA
012220111

Experiment -13
Write a Python program that prints the uppercase and lowercase English
letters in separate lines.
Note: Use loops and the chr() function to convert ASCII values to characters.
Source code:

for n in range(65,91):

print(chr(n),end=' ')

print()

for n in range(97,123):

print(chr(n),end=' ')

Output:
PHILIP WANDAWA
012220111

Experiment-14
Write a Python program that prompts the user to enter a percentage and
then determines and displays the corresponding grade based on the
following criteria:
Source code:

Marks greater than 90% receive grade 'A'.

Marks greater than 80% and less than or equal to 90% receive grade 'B'.

Marks greater than or equal to 60% and less than or equal to 80% receive grade 'C'.

Marks below 60% receive grade 'D'.

p=float(input("Enter grade"))

if p>=90 and p<=100:

print("A")

elif p>=80 and p<=90:

print("B")

elif p>=60 and p<=80:

print("C")

else:

print("D")
PHILIP WANDAWA
012220111

Output:

Experiment-15
Write a Python program that takes an input number from the user and
calculates the number of digits (length) in that number. Display the calculated
length as the output.
Source code:

num=int(input("Enter any number"))

c=0

while num!=0:

c=c+1

num=num//10
PHILIP WANDAWA
012220111

print("length of number is ",c)

Output:

Experiment-16

Define two classes A and B to demonstrate single level inheritance in


Python by following the below instructions.
-Implement class A with a method m1() that prints a message, "m1 of A"

-Implement class B that inherits from A. Class B should have a method m2() that prints "m2 of B".

-In the main() function:Create an object objc of class B.

Demonstrate calling methods m1(), m2(), using the object


PHILIP WANDAWA
012220111

class A:

def m1(self):

print("m1 of A")

class B(A):

def m2(self):

print("m2 of B")

def main():

# Create an object of class B

objc = B()

objc.m1() # This will call m1() method of class A

objc.m2() # This will call m2() method of class B

main()

Output:
PHILIP WANDAWA
012220111

Experiment-17
Write a Python program that accepts a number from the user and calculates
the sum of its digits. Display the calculated sum as the output.

num=int(input("enter any number"))

s=0

while num!=0:

r=num%10

s=s+r

num=num//10

print("sum of digits is ",s)

Output:
PHILIP WANDAWA
012220111

Experiment-18
Write a python program to print the following format using for loop.
Source code:

for r in range(5,0,-1):#5 4 3 2 1

for c in range(1,6):

print(r,end='')

print()

Output:

for r in range(1,6): #1 2 3 4 5

for c in range(1,6): # 1 2 3 4 5

print(r,end=' ')

print()
PHILIP WANDAWA
012220111

for r in range(1,6):

for c in range(5,0,-1):

print(r,end=' ')

print()

You might also like