PYTHON PRACTICAL
Practical No :- 01
A. Develop Program to understand the decision control structures of python.
1)
amount=0
units=int(input("Enter your units: "))
if units<=100:
print("No charges ")
if units>100 and units<=200:
amount=(units-100)*5
if units>200:
amount=500(units-100)*10
print("Electricity Bill =",amount)
2)
num=int(input("Enter any number :"))
last_digit=num%10
if(last_digit%3==0):
print("{} is divisible by 3".format(last_digit))
else:
print("{} is not divisible by 3".format(last_digit))
3)
per=int(input("Enter your percentage"))
if per>90:
print("The having A grade in examination")
elif per>80 and per<=90:
print("The having B grade in examination")
elif per>=60 and per<=80:
print("The having c grade in examination")
elif per<60:
print("The having D grade in examination")
Output:
Practical No :-02
Develop the program to understand the looping statement.
1)
str=input("Enter any word")
vowels=0
for i in str:
if( i=='a' or i=='e' or i=='i' or i=='o' or i=='u'or i=='A' or i=='E' or i=='I' or
i=='O' or i=='U'):
vowels=vowels+1
print("number of vowels are :")
print(vowels)
2)
no=int(input("Enter number to find factorial"))
fact=1
for i in range(1,no+1):
fact=fact*i
print("Factorial of that no is :",fact)
3)
num=int(input("Enter any number to find sum of digits : "))
sum=0
while(num>0):
reminder=num%10
num=num//10
sum=num+reminder
print("Sum of the digit is=%d",sum)
Output:
Practical No :-03
Develop programs to learn different types of structures(list, dictionary, tuples in
python.
1)Write a program to demonstrate list sequence.
# Update the elements of the list
a = ['Ansh', 2, 35, 'code', 2.5]
print("Length of the list", len(a))
print("Before list",a)
a[0] = "ash"
print("After update list",a)
# Add new elements in to the list – append() , insert()
a = ['Ansh', 2, 35, 'code', 2.5]
print("list",a)
print("Length of the list", len(a))
[Link]("kash")
print("list",a)
print("Length of the list", len(a))
[Link](1,25)
print("list",a)
print("Length of the list", len(a))
# Removing elements from the list – remove() del
a = ['Ansh', 2, 35, 'code', 2.5]
print("list",a)
print("Length of the list", len(a))
b = [Link](1)
print("element deleted using pop()",b)
print("list",a)
[Link](35)
print("element deleted using remove()",a)
del a[1:3]
print("element deleted using del keyword",a)
# Find out length of the list
a = ['Ansh', 2, 35, 'code', 2.5]
print("list",a)
print("Length of the list", len(a))
# Find out maximum between list
a = [1,5,8,6,2]
print("Largest element of list", max(a))
# Find out minimum between list
a = [1,5,8,6,2]
print("Smallest element of list", min(a))
Output:
B. Write aprogram to demonstrate Tuple sequence.
# Write a program to demonstrate Tuple sequence.
# Creating Tuple
a=()
tup = tuple()
print(type(a))
print(type(tup))
# Read the elements of the tuple from the user
lst=[]
a = int((input("Enter the size of tuple")))
for i in range(0,a):
b = (input("Add tuple element"))
[Link](b)
tup = tuple(lst)
print(tup)
print(type(tup))
# Print the elements using slice operator.
tup=('apple', 'realme', 'redmi', 'moto')
print(tup[:])
print(tup[:3])
print(tup[3:])
print(tup[::-1])
print(tup[-1:])
# Deleting Tuple
tup = ('apple', 'realme', 'redmi', 'moto')
print("Tuple",tup)
del tup
# Find out length of the Tuple
tup = ('apple', 'realme', 'redmi', 'moto')
print(tup)
print(" Length of Tuple",len(tup))
# Find out maximum between list
tup = (25,80,74,62,250)
print(tup)
print(type(tup))
print(" Maximum of Tuple",max(tup))
# Find out minimum between list
tup = (25,80,74,62,250)
print(tup)
print(" Minimum of Tuple",min(tup))
Output:
C. Demonstrate the Dictionary.
# C. Demonstrate the dictionary
# Creating Dictionary
dic={}
my_dict={"Car1": "Audi", "Car2":"BMW",
"Car3":"Mercidies Benz","Car4":"Range Rover"}
print(type(dic))
print(my_dict)
print(type(my_dict))
# Sort dictionary by values(Ascending and descending)
import operator
#
d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
#
s= sorted([Link](), key=[Link](1))
#
print('ascending order : ',s)
#
s1= dict( sorted([Link](), key=[Link](1),reverse=True))
#
print('descending order : ',s1)
# concatenate two dictionaries to create one
car1_model={'Mercedes':1960}
car2_model={'Audi':1970}
car2_model.update(car1_model)
print(car2_model)
# check whether the key exist or not
my_dict={"Car1": "Audi", "Car2":"BMW",
"Car3":"Mercidies Benz","Car4":"Range Rover"}
#
key = input("Enter the key you want to search:\n")
#
if key in my_dict.keys():
print("Present")
else:
print("Not Present")
# iterate the keys of dictionary
my_dict={"Car1": "Audi", "Car2":"BMW",
"Car3":"Mercidies Benz","Car4":"Range Rover"}
for x in my_dict:
print(x)
# iterate the values of dictionary
my_dict={"Car1": "Audi", "Car2":"BMW",
"Car3":"Mercidies Benz","Car4":"Range Rover"}
for x in my_dict.values():
print(x)
# iterate the items of dictionary
my_dict={"Car1": "Audi", "Car2":"BMW",
"Car3":"Mercidies Benz","Car4":"Range Rover"}
for x in my_dict.items():
print(x)
# remove the specific values
my_dict={"Car1": "Audi", "Car2":"BMW",
"Car3":"Mercidies Benz","Car4":"Range Rover"}
print("Original Dict \n",my_dict)
my_dict.pop('Car3')
print("Element removed using pop \n",my_dict)
del my_dict['Car2']
print("Element removed using del keyword \n",my_dict)
Output:
Practical No :- 04
Develop program to learn concept of functions scoping, recursion and
list ,mutability.
Function Scoping :-
1. # It is global function
x = "global"
def fun():
print("It is global scope inside :", x)
fun()
print("It is global scope outside :", x)
# It is local function
def myfunc():
x = ("It is local variable")
print(x)
myfunc()
Output:
2. # It is recursion Function
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
return (x * factorial(x-1))
num = 3
print("The factorial of", num, "is", factorial(num))
Output:
Practical No :- 05
D. Develop program to understand object oriented programming using python.
class Raisoni:
def student(self):
print("All Raisoni Students")
class Education(MCA):
def division(self):
print(“Welcome")
e=Education()
[Link]()
[Link]()
Output:
Practical No :- 06
Develop programs for data structure algorithms using python.
1. Searching:-
#Linear Search
class linearsearch:
ele=[]
def get(self):
self.a=int(input("Enter no of element you want to insert"))
for i in range(0,self.a):
b=(input("Add element you want to search"))
[Link](b)
def search(self):
c=int(input("Enter elemnt you want to search"))
for i in range(0,self.a):
if [Link][i]==c:
break;
if i<self.a:
print("Element found at index : ",i+1)
else:
print("Not Found .....")
s=linearsearch()
[Link]()
[Link]()
Output:
2. Sorting :-
#Bubble Sort
class sort:
ele=[]
def get(self):
self.a=int(input("Enter no of element you want to insert"))
for i in range(0,self.a):
b=int((input("Add element you want to sort")))
[Link](b)
print([Link])
def bsort(self):
for i in range(0,self.a):
for j in range(0,self.a-i-1):
if [Link][j]>[Link][j+1]:
temp=[Link][j]
[Link][j]=[Link][j+1]
[Link][j+1]=temp
def show(self):
print("Sorted list")
print([Link])
s=sort()
[Link]()
[Link]()
[Link]()
Output:
Practical:-7
[Link] program to learn GUI programming using Tkinter
from tkinter import *
base = Tk()
[Link]("500x500")
[Link]("registration form")
lb1= Label(base, text="Enter Name", width=10, font=("arial",12))
[Link](x=20, y=120)
en1= Entry(base)
[Link](x=200, y=120)
lb3= Label(base, text="Enter Email", width=10, font=("arial",12))
[Link](x=19, y=160)
en3= Entry(base)
[Link](x=200, y=160)
lb4= Label(base, text="Contact Number", width=13,font=("arial",12))
[Link](x=19, y=200)
en4= Entry(base)
[Link](x=200, y=200)
lb5= Label(base, text="Select Gender", width=15, font=("arial",12))
[Link](x=5, y=240)
vars = IntVar()
Radiobutton(base, text="Male", padx=5,variable=vars, value=1).place(x=180,
y=240)
Radiobutton(base, text="Female", padx =10,variable=vars,
value=2).place(x=240,y=240)
Radiobutton(base, text="others", padx=15, variable=vars,
value=3).place(x=310,y=240)
list_of_cntry = ("United States", "India", "Nepal", "Germany")
cv = StringVar()
drplist= OptionMenu(base, cv, *list_of_cntry)
[Link](width=15)
[Link]("United States")
lb2= Label(base, text="Select Country", width=13,font=("arial",12))
[Link](x=14,y=280)
[Link](x=200, y=275)
lb6= Label(base, text="Enter Password", width=13,font=("arial",12))
[Link](x=19, y=320)
en6= Entry(base, show='*')
[Link](x=200, y=320)
lb7= Label(base, text="Re-Enter Password", width=15,font=("arial",12))
[Link](x=21, y=360)
en7 =Entry(base, show='*')
[Link](x=200, y=360)
Button(base, text="Register", width=10).place(x=200,y=400)
[Link]()
Output:
Practical:-9
Demonstrate the concept of exception handling programs For Try Catch And
Finally :
1) Try and Catch :
n = int(input("enter 1st no: "))
m = int(input("enter 2nd no: "))
try:
x = n/m
except ZeroDivisionError:
print("Sorry ! You are dividing by zero ")
else:
print("Division of two nos. is : ",x)
2) Try and Finally:
n = int(input("enter 1st no: "))
m = int(input("enter 2nd no: "))
try:
x = n/m
print("Division of two nos. is : ",x)
except ZeroDivisionError:
print("Sorry ! You are dividing by zero ")
finally:
print("This statement execute anyways")
Output:
Practical 10
E. Demonstrate implementation of the Anonymous Function Lambda.
A. Simple Lambda
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
Output:
B. Cube Lambda
# Python code to illustrate cube of a number
# showing difference between def() and lambda().
def cube(y):
return y*y*y
lambda_cube = lambda y: y*y*y
# using the normally
# defined function
print(cube(5))
# using the lambda function
print(lambda_cube(5))
OR
def cube(y):
return y*y*y
c = int(input("Enter the no: "))
lambda_cube = lambda y: y*y*y
print("Lanbda Cube is: ",lambda_cube())
Output:
Practical 11
Demonstrate implementation Mapping Functions over Sequences.
def mul(i):
return i * i
num = (3, 5, 7, 11, 13)
update = map(mul, num)#map(square,num)
print(update)
# making the map object readable
mul_output = list(update)
print(mul_output)
Output:
Practical 12
Demonstrate implementation functional programming tools such as filter And
Reduce
a) **********
scores = [66, 90, 68, 59, 76, 60, 88, 74, 81, 65]
def is_A_student(score):
return score > 75
over_75 = list(filter(is_A_student, scores))
print(over_75)
b) *****************
dromes = ("demigod", "rewire", "madam", "freer", "anutforajaroftuna", "kiosk")
palindromes = list(filter(lambda word: word == word[::-1], dromes))
print(palindromes)
c) ***************
# Python 3
from functools import reduce
numbers = [3, 4, 6, 9, 34, 12]
def custom_sum(first, second):
return first + second
result = reduce(custom_sum, numbers)
print(result)
d) ****************
from functools import reduce
numbers = [3, 4, 6, 9, 34, 12]
def custom_sum(first, second):
return first + second
result = reduce(custom_sum, numbers, 10)
print(result)
Output:
Practical 13
Demonstrate the Module Creation, Module usage, Module Namespaces,Reloading
Modules, Module Packages, Data Hiding in Modules.
a) Module Creation:
def add(a, b):
"""This program adds two
numbers and return the result"""
result = a + b
return resultimport math
print("The value of pi is", [Link])
import math as m
print("The value of pi is", [Link])
#from...import statement
from math import pi
print("The value of pi is", pi)
from math import *
print("The value of pi is", pi)
Output: