Mr. Arvind Singh: Submitted by

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

NAAC ACCREDITED ‘B+’ GRADE (FIRST CYCLE)

DEPARTMENT OF INFORMATION TECHNOLOGY

Submitted by:

Name: ____________________________
Class : ____________________________
Roll No.: ____________________________
Subject: ____________________________

Submitted To :

Mr. Arvind Singh

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

INDEX
PRAC.NO NAME SIGNATURE
1(a). PROGRAM TO TAKE INPUT FROM USER THEIR NAME
AND AGE AND PRINT WHEN THEY WILL BECOM 100
YEAR OLD

1(b). PROGRAM TO FIND WHETER THE NUMBER IS EVEN


OR ODD
TAKE INPUT FROM USER

1(c). WRITE A PROGRAM TO GENERATE FIBONACI


SERIES

1(d). WRITE A FUNCTION THAT REVERSE THE USER


DEFINE NUMBER

1(e). WRITE A PROGRAM TO CHECK WHETER THE NO.IS


AMSTRONG OR NOT OR ALSO DO FOR PALINDROME

1(f). WRITE A RECURSIVE FUNCTION TO PRINT


FACTORIAL
2(a). WRITE A PROGRAM TO TAKE CHARACTER FROM
USER IF ITS VOWEL RETURN TRUE ELSE FALSE

2(b). DEFINE A FUNCTION THAT COMPUTE THE LENGTH


OF A GIVEN STRENGTH

2(c). PRINT HISTOGRAM[4,9,7]


****
*********
*******

3(a). WRITE A PANAGRAM SENTENCE WHICH CONTAIN


ALL ALPHABET eg:- THE QUICK BROWN FOX JUMPS
OVER THE LAZY DOG.

3(b). a=[1,1,2,3,5,8,13,21,34,55,89]
FROM ABOVE GIVEN LIST PRINT THE LIST WHOSE
VALUE IS LESS THEN 5.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

4(a). WRITE A PROGRAM THAT TAKE TWO LIST AND


RETURN TRUE IF THEY HAVE ATLEAST ONE
COMMON ELEMENT.

4(b). WRITE A PYTHON PROGRAM TO PRINT A SPECIFIED


LIST AFTER REMOVING THE 0th ,2nd ,4th AND 5th
ELEMENT

4(c). WRITE A PROGRAM TO MAKE CLONE OR COPY A


LIST

5(a). WRITE A PYTHON SCRIPT TO SHORT(ASCENDING


OR DESCENDING) A VALUE IN A GIVEN DICTIONARY

5(b). WRITE A PYTHON SCRIPT TO CONCATENATE TWO


DICTIONARY AND FORM NEW ONE

5(c). WRITE A PYTHON PROGRAM TO SUM ALL THE


ITEMS IN DICTIONARY.

6(a). WRITE A PYTHON PROGRAM TO READ AN ENTIRE


FILE

6(b). WRITE A PYTHON PROGRAM TO APPEND A TEXT IN


AFILE AN DISPLAY THE TEXT

6(c). WRITE A PYTHON PROGRAM TO READ LAST n LINES


OF FILES

7(a). DESIGN A CLASS STORE THE INFORMATION OF


STUDENT AND DISPLAY THE SAME

7(b). IMPLEMENT THE CONCEPT OF INHERITANCE

7(c). CREATE ADD,SUB,MULTIPLYFUNCCTION IN CLASS

8(a). CREATE A FILE GEOMETRY.PY AND SAVE IT AND


CREATE DEMO.PY AND IMPORT THE GEOMETRY IN

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

DEMO

8(b). WRITE A PROGRAM TO IMPLEMENT EXCEPTION


HANDLING

9(a). TRY TO CONFIGURE VARIOUS WIDGET LIKE:


bg=”red” family=”times” SIZE=16

9(b). TRY TO CONFIGURE VARIOUS WIDGET AND DO


VARIOUS OPERATION LIKE
MESSAGEBOX,RADIO,CHECKBOX,ENTRY,BUTTON

10(a). DESIGN A SIMPLE DATABASE THAT STORE THE


DATA AND RETRIVE IT

10(b). DESIGN A DATABASE THAT SEARCH SPECIFIED


RECORD IN DATABASE

10(c). DESIGN A DATABASE THAT ALLOW THE USER TO


ADD,DELETE,MODIFY DATA

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

PRATICAL.1

(A) create a program that ask the user to enter their name and their
age. print out the message address to them that tells then the year
that they turn 100 year old.

PROGRAM:

name=input("What is your name:")

age=int(input("How old are you:"))

year=str((2019-age)+100)

print(name+ " You will be 100 years old in the year" +year)

OUTPUT:

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(B) enter the number of users and depending on whether the no. is
even or odd. print out an appropriate message to the users.

PROGRAM:

a=int(input("Enter any number:"))

if(a%2==0):

print(a," is even number")

else:

print(a,"is odd number")

OUTPUT:

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(C)Write a program to Generate FIBONACCI series.

PROGRAM.

n=int(input("Enter any number:"))

a=0

b=1

for i in range(0,n):

if(i<=1):

c=i

else:

c=a+b

a=b

b=c

print(c)

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(D)Reverse the number by using Function.

PROGRAM.

def revnum(n):

rev=0

while(n>0):

rem=n%10

rev=(rev*10)+rem

n=n//10

return rev

n=int(input("Please enter any number:"))

rev=revnum(n)

print("Reverse of entered number is=%d" %rev)

output.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

PRATICAL.2

(A)write a function that take a character(i.e a string of length l)and


return. True if it is a vowel ,false otherwise.

PROGRAM.

def vchk(ch):

if(ch=='a' or ch=='A' or ch=='e' or ch=='E' or ch=='i'

or ch=='I' or ch=='o' or ch=='O' or ch=='U' or ch=='u'):

print(ch,"is a vowel.")

else:

print(ch,"is not a vowel.")

ch=input("Enter any character (A-Z/a-z) only:")

vchk(ch)

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(B)Define a function that computes the length of a given list or string.

PROGRAM.

def calen(n):

count=0

for i in n:

count=count+1

return count

print(calen([1,3,5,7,9,10,2]))

print(calen("Pawan"))

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(C)define a procedure HISTOGRAM() that takes the list of integers and


print a histogram to the screen.for example,histogram([4,9,7])should
print the following.

****

*********

*******

PROGRAM.

def histogram(inputList):

for i in range(len(inputList)):

print(inputList[i]*'*')

List=[3,5,7]

histogram(List)

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

PRATICAL.3

(A)A pangram is a sentence that contain all the letters of the English
alphabet atleast once for example: the quick brown fox jumps over the
lazy dog.Your task here is to write a function to check a sentence to see
if it is a pangram or not.

PROGRAM.

import string,sys

def ispangram(str1,alphabet=string.ascii_lowercase):

alphaset=set(alphabet)

return alphaset <=set(str1.lower())

print(ispangram('The quick brown fox jumps over the lazy dog'))

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(B)Take a list, say for example this one:

a=[1,2,3,5,8,13,21,34,55,89]&WAP that print out all the elements of


the list that are less than 5.

PROGRAM.

a=[1,1,2,3,5,8,13,21,34,55,89]

for i in a:

if i<5:

print(i)

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

PRATICAL.4

(A) write a program that takes two list and returns True if they have
atleast one common member.

PROGRAM.

def find_common(str1,str2):

res=False

for x in str1:

for y in str2:

if x==y:

res=True

return res

print(find_common([4,6,8,10,12],[5,7,8,10,11]))

print(find_common([3,5,7,9,11],[2,4,6,8]))

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(B) write a program to find a specified list after removing the


0th,2nd,4th&5th element.

PROGRAM.

name=['Hardik','Pawan','kartik','Shivam','Ajay','Hritik','Harsh']

name=[x for(i,x) in enumerate(name) if i not in(0,2,4,5)]

print(name)

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(C) write a program to copy or clone a list.

PROGRAM.

L1=[8,11,13,22,27]

L2=list(L1)

print('L1:',L1)

print('L2:',L2)

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

PRATICAL.5

(A) write a python script to sort(ascending descending)a dictionary


values.

PROGRAM.

import operator

d={1:22,3:13,4:8,2:11,0:27}

print(d)

t=sorted(d.items(),key=operator.itemgetter(0))

print('In Ascending order by values:',t)

t=sorted(d.items(),key=operator.itemgetter(0),reverse=True)

print('In Descending order by values:',t)

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(B) write a python script to concatenate following dictionaries to create


a new one.

PROGRAM.

dic1={1:10,2:20}

dic2={3:30,4:40}

dic3={5:50,6:60}

dic1.update(dic2)

dic1.update(dic3)

print(dic1)

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(C) write a python program to sum all the items in following dictonary.

PROGRAM.

d={'t1':10,'t2':5,'t3':25}

print(d)

print("sum:",sum(d.values()))

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

PRATICAL.6

(A) write a python program to read a entire list.

PROGRAM.

f=open('pra6(a).txt','r')

t=f.read()

print(t)

f.close()

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(B) write a python program to append text to a file & display the text.

PROGRAM.

f=open('pra6(a).txt','a+')

f.write('Easy to learn\n')

f=open('pra6(a).txt','r')

t=f.read()

print(t)

OUTPUT:

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(C) write a python program to read last n line of a file.

PROGRAM.

f=open('pra6(a).txt','r')

t=f.readlines()

print(t[-1])

f.close()

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

PRATICAL.7

(A) design a class that store the information of student & display the
same.

PROGRAM.

class student:

def info(self,studentname,stuadd,age,phone):

print("\nName:",studentname,"\nStudent
Address:",stuadd,"\nAge:",age,"\nPhone Number:",phone)

obj=student()

obj.info('Pawan','Vikhroli','18','8291712110')

obj1=student()

obj1.info('Ajay','Powai','19','1234543456')

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(B) Implement the concept of inheritance using python.

PROGRAM.

class st:

def s1(self):

print("Base class")

class st1(st):

def s2(self):

print("Derived class")

t=st1()

t.s1()

t.s2()

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(C) create a class called number, which has a single class attribute called
multiplier, and a constructor which take the parameter x and y (these
should be all the numbers)

(i) write a method called add which returns the sum of the attribute
s&y.

(ii) write a static method called multiply which takes a single number
parameter b and c and returns the product of a and multiplier.

(iii) write a static method called subtract which takes two numbers
parameters, b & c ,and returns b-c.

(iv) write a method called value which values which returns a tuple
containing the values of x and y make this method into a property and
write a setter & a delete for manipulating the values of x and y.

PROGRAM.

class Numbers:

MULTIPLIER=3

def init__(self,x,y):

self.x=x

self.y=y

def add(self):

return self.x+self.y

@classmethod

def multiply(cls,a)

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

return cls.MULTIPLIER*a

@staticmethod

def subtract(b,c):

return b-c

@property

def value(self):

return(self.x,self.y)

@value.setter

def value(self,xy_tuple):

self.x,self.y=xy_tuple

@value.deleter

def value(self):

del self.x

del self.y

T=Numbers(2,4)

print(T.add())

print(T.multiply(2))

print(Numbers.subtract(4,3))

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

PRATICAL.8

(A)Open a new file in idle(“new window” in “file” menu)and save it as


Geometry.py in the directory where you keep the files you create for
this course. Then copy the functions you wrote for calculating volumes
and areas in the “control Flow and Functions” exercise into this file and
save it. Now open a new file and save it in the same directory you
should now be able to import your own module like this:

Import geometry, try and add print(geometry)to the file and run it. now
write a function pointShapeVolume(x,y,squareBase) that calculates the
volume of square pyramid if squareBase is true and of a right circular
cone if squareBase is False. x is the length of an edge on a square if
squareBase is True and the radius of a circle when squareBase is False.
y is the heightof the object. First use SquareBase to distinguish the
case. Use the circleArea and squareArea from the geometry module to
calculate the base areas.

PROGRAM.

import math

def sphereArea(r):

return 4*math.pi*r*2

def sphereVolume(r):

return 4*math.pi*r**3/3

def sphereMetrics(r):

return sphereArea(r),sphereVolume(r)

def circleArea(r):

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

returnmath.pi*r**2

def squareArea(x):

return x**2

#demo.py

import geometry

def pointyShapeVolume(x,h,square):

if square:

base=geometry.squareArea(x)

else:

base=geometry.circleArea(x)

return h*base/3.0

print(dir(geometry))

print(pointyShapeVolume(4,2.6,True))

print(pointyShapeVolume(4,2.6,False))

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(B)WAP to implement exception handling.

PROGRAM.

try:

num=int(input("Enter the number:"))

re=100/num

except(ValueError,ZeroDivisionError):

print("Somthing is wrong")

else:

print("Result is:",re)

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

PRATICAL.9

(A)Try to configure the widget with various options like: bg=”red”,


family=”times”. Size=18

PROGRAM.

import tkinter as tk

win=tk.Tk()

win.title("Practical 9 A")

def redclick():

label.config(text="Helvetica Font")

label.config(bg="red")

label.config(font=("Helventica",16))

def orangeclick():

label.config(text="Cambria Font")

label.config(bg="orange")

label.config(font=("Cambria",18))

def yellowclick():

label.config(text="Arial Font")

label.config(bg="yellow")

label.config(font=("Arial",14))
label=tk.Label(win,text="Practical 9 A",bg='white')

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

label.pack()

B1=tk.Button(win,text="Red Click",relief='raised',command=redclick)

B1.pack(side="left")

B2=tk.Button(win,text="Orange
Click",relief='raised',command=orangeclick)

B2.pack(side="left")

B3=tk.Button(win,text="Yellow
Click",relief='raised',command=yellowclick)

B3.pack(side="left")

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(B)Try to change the widget type and configuration option to


experiment with other widget types like Message, Button, Entry,
Checkbutton, Radiobutton, Scale etc.

PROGRAM.

from tkinter import*

def swap():

if v.get():

e.pack_forget()

mb.pack(anchor="w",side="right")

l2.config(text="Use Menu Below.")

l2.config(bg="yellow")

l2.config(font=("Helventica",16,"italic"))

else:

mb.pack_forget()

e.pack(anchor="w",side="left")

l2.config(bg="orange")

l2.config(font=("Cambria",16,"bold"))

e.focus()

t=Tk()

v=IntVar(t)

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

c=Checkbutton(t,command=swap,text="Select to use
menu.",variable=v)

c.pack(anchor="w")

f1=Frame(t)

l1=Label(f1,text="Select the menu itm of your choice:")

l1.pack(side="left")

l2=Label(f1,text="Use Entry Box


Below.",bg="orange",font=("Cambria",16,"bold"))

l2.pack(side="top")

f=Frame(f1)

f.pack(side="left")

e=Entry(f,width=35)

mb=Menubutton(f,width=25,text="Veg",indicatoron=1,relief="sunken",
anchor="w")

m=Menu(mb,tearoff=0);mb.configure(menu=m)

for s in "Veg nonVeg Chinese Franch".split():

m.add_command(label=s,command=lambda
s=s:mb.configure(text=s))

f.pack()

f1.pack()

swap()

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

b=Button(t,text="Place
order",relief="raised",fg="red",command=t.destroy);

b.pack(side="top")

f.mainloop()

OUTPUT.

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

PRATICAL.10
(A)Design the simple database application that stores the records and retrieve the
same.

PROGRAM:
from tkinter import*

import mysql.connector as mysql

from tkinter import messagebox

def put(*args):

conn=mysql.connect(user='root',password='',host='localhost',database='syit')

cur=conn.cursor()

cur.execute("insert into
student(name,age)values('"+nameEntry.get()+"','"+ageEntry.get()+"')")

messagebox.showinfo("Info Msg","Record inserted")

conn.commit()

conn.close()

def get():

conn=mysql.connect(user='root',host='localhost',password='',database='syit')

cur=conn.cursor()

cur.execute("select * from student where id=(select max(id) from student)")

result=cur.fetchall()

row=cur.fetchone()

messagebox.showinfo("Info Msg",result)

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

conn.close()

root=Tk()

root.title("Insert Data")

nameEntry=Entry(root,width=7)

ageEntry=Entry(root,width=7)

nameEntry.grid(row=1,column=1)

ageEntry.grid(row=2,column=1)

Label(root,text="Name").grid(row=1,column=0)

Label(root,text="Age").grid(row=2,column=0)

Button(root,text="Insert",command=put).grid(row=3,column=0)

Button(root,text="Display",command=get).grid(row=3,column=1)

root.mainloop() OUTPUT:

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(B)Design a database application to search the spesified record.

PROGRAM:
from tkinter import*

import mysql.connector as mysql

def get():
conn=mysql.connect(user="root",password="",host="localhost",database="syit")

cur=conn.cursor()

str="select * from student where id="+idEntry.get()+""

cur.execute(str)

for(id,name,age) in cur:

print("id:{},Name:{},Age:{}".format(id,name,age))

conn.commit()

conn.close()

root=Tk()

root.title("Search Data")

Label(root,text="ID").grid(row=0,column=0)

idEntry=Entry(root,width=10)

idEntry.grid(row=0,column=1)

Button(root,text="Search",command=get).grid(row=0,column=2)

root.mainloop()

OUTPUT:

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

(C)Design a database application to that allows the user to add,delete and


modify the records.

PROGRAM:
from tkinter import*

import mysql.connector as mysql

from tkinter import messagebox

def upd():

conn=mysql.connect(user="root",host="localhost",password="",database='syit')

cur=conn.cursor()

str="update student set age='"+ageEntry.get()+"' where id="+idEntry.get()+""

cur.execute(str)

messagebox.showinfo("Info Msg","Record updated")

conn.commit()

conn.close()

def dele():

conn=mysql.connect(user="root",host="localhost",password="",database='syit')

cur=conn.cursor()

str="delete from student where id="+idEntry.get()+""

cur.execute(str)

messagebox.showinfo("info Msg","Record deleted")

conn.commit()

conn.close()

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

def put(*args):
conn=mysql.connect(user="root",host="localhost",password="",database='syit')

cur=conn.cursor()

cur.execute("insert into
student(name,age)values('"+nameEntry.get()+"',"+ageEntry.get()+")")

messagebox.showinfo("info Msg","Record inserted")

conn.commit()

conn.close()

def get():
conn=mysql.connect(user="root",host="localhost",password="",database='syit')

cur=conn.cursor()

cur.execute("select * from student")

result=cur.fetchall()

for row in result:

print(row[0],row[1],row[2])

conn.close()

root=Tk()

root.title("Insert data")

nameEntry=Entry(root,width=7)

ageEntry=Entry(root,width=7)

nameEntry.grid(row=1,column=1)

ageEntry.grid(row=2,column=1)

Label(root,text="Name").grid(row=1,column=0)

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

Label(root,text="Age").grid(row=2,column=0)

Button(root,text="Insert",command=put).grid(row=3,column=0)

Button(root,text="Display",command=get).grid(row=3,column=1)

Label(root,text='ID').grid(row=4,column=0)

idEntry=Entry(root,width=7)

idEntry.grid(row=4,column=1)

Button(root,text="Delete",command=dele).grid(row=5,column=0)

Button(root,text="Update",command=upd).grid(row=5,column=1)

root.mainloop()

OUTPUT:

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

ROLL.NO:08
CHANDRABHAN SHARMA COLLEGE SUB:PYTHON PROGRAMMING

ROLL.NO:08

You might also like