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

JOURNAL PYTHON FINAL

The document contains various Python programming exercises including file handling, exception handling, graphical user interface (GUI) creation using Tkinter, and database operations. It covers tasks such as writing to and reading from files, creating shapes and figures, building a simple calculator, and checking eligibility for a hiring agency. Additionally, it includes code snippets for calculating area, perimeter, simple interest, and calories in food items.

Uploaded by

xxxjack2018
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)
8 views

JOURNAL PYTHON FINAL

The document contains various Python programming exercises including file handling, exception handling, graphical user interface (GUI) creation using Tkinter, and database operations. It covers tasks such as writing to and reading from files, creating shapes and figures, building a simple calculator, and checking eligibility for a hiring agency. Additionally, it includes code snippets for calculating area, perimeter, simple interest, and calories in food items.

Uploaded by

xxxjack2018
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/ 14

KAUSHIK MOURYA

ROLL NO.:- CS20042


SUB:- PYTHON JOURNAL
 Write a program in Python to create a file "data.txt" and write 5 lines to it.

file=open("D:\\kaushik\\drawing\\DATA.txt",'w')
file.write("I love Python""\nI am a student""\nI also play
games""\ngodzilla""\nkong")
file.close()

 Write a program in Python to read the contents of "data.txt" and display them.

file=open("D:\\kaushik\\drawing\\DATA.txt","r")
print(file.read())
file.close()

 Write a program in Python to copy the contents of "data1.txt" to another file


"data2.txt"

def copy(s,d):
f1=open(s,"r")
f2=open(d,"w")
data=f1.read()
f2.write(data)
f1.close
f2.close

source=input("enter source file (with .txt extension):")


destination=input("enter destination file (with .txt extension):")
copy(source,destination)
 Write a program in Python to raise an “Invalid Marks Exception”

class InvalidmarkException(Exception):
pass
try:
mark=input("Enter your mark :")
x="45"
if mark==x:
print("Correct mark")
else:
raise InvalidmarkException
except InvalidmarkException:
print("Incorrect mark")

 Write a program in Python to raise an “Invalid Colour Exception”



class InvalidColourException(Exception):
pass
try:
colour=input("Enter a Colour :")
x="yellow"
if colour==x:
print("Correct Colour")
else:
raise InvalidColourException
except InvalidColourException:
print("Incorrect Colour")
 Write a program in Python to create a figure which consist of rectangle, a
circle, an arc and line it..

from tkinter import *
master=Tk()
w=Canvas(master,width=1000, height=600)
w.create_line(10,10,200,200,width="3")
w.create_rectangle(10,10,300,200)
w.create_oval(10,10,300,200)
w.create_arc(10,10,300,200,start=0,extent=-120,fill="pink")
w.pack()
mainloop()

 Write a Python Program to draw the following figures:

 House

from tkinter import *
master=Tk()
w=Canvas(master,width=1000, height=600)
w.create_rectangle(130,80,900,500,fill="light blue")
w.create_rectangle(600,500,380,280,fill="red")
w.create_rectangle(530,500,450,420,fill="orange")
w.create_rectangle(450,360,400,310,fill="yellow")
w.create_rectangle(535,360,585,310,fill="yellow")
w.create_rectangle(650,280,330,220,fill="brown")

w.pack()
mainloop()
 Sun

from tkinter import *
master=Tk()
w=Canvas(master,width=1000, height=600)
w.create_oval(630,420,350,160,fill="yellow")

w.pack()
mainloop()

 Smiley

from tkinter import *
master=Tk()
w=Canvas(master,width=1000, height=600)
w.create_rectangle(850,450,135,115)
w.create_oval(630,420,350,160,fill="yellow")
w.create_oval(480,300,380,200,fill="black")
w.create_oval(600,300,500,200,fill="black")
w.create_oval(520,300,450,340,fill="black")
w.create_arc(565,410,415,290,start=0,extent=-180,fill="red")

w.pack()
mainloop()
 Indian Flag

from tkinter import *
master=Tk()
w=Canvas(master,width=1000, height=600)
w.create_rectangle(850,450,135,115)
w.create_rectangle(600,200,350,150,fill="orange")
w.create_rectangle(600,250,350,200,fill="white")
w.create_rectangle(600,300,350,250,fill="green")
w.create_oval(510,250,450,200,fill="blue")
w.create_rectangle(350,500,330,150,fill="brown")

w.pack()
mainloop()

 Fish

from tkinter import *
master=Tk()
w=Canvas(master,width=1000, height=600)
w.create_rectangle(130,80,900,500,)
w.create_oval(320,180,750,300,fill="grey")
w.create_polygon(750,240,800,180,800,300,fill="yellow")
w.create_oval(380,210,395,225,fill='black')
w.create_arc(680,300,500,100,start=90,extent=90,fill="green")
w.create_arc(680,180,500,380,start=180,extent=90,fill="red")

w.pack()
mainloop()
 Write a program to draw a Mickey Mouse face.

from tkinter import *
master=Tk()
w=Canvas(master,width=1000, height=600)
w.create_rectangle(850,450,135,115)
w.create_oval(630,420,350,160,fill="black")
w.create_oval(450,110,320,230,fill="black")
w.create_oval(655,110,527,230,fill="black")
w.create_oval(500,360,380,200,fill="navajo white")
w.create_oval(600,360,480,200,fill="navajo white")
w.create_oval(635,415,345,280,fill="navajo white")
w.create_oval(550,430,430,330,fill="navajo white")
w.create_oval(520,300,450,340,fill="black")
w.create_arc(565,410,415,290,start=0,extent=-180,fill="red")
w.pack()
mainloop()

 Write a program in Python to create project to calculate the Area of Rectangle,


Perimeter and Clear button

l=int(input("Length of the rectangle : "))
b=int(input("Breadth of the rectangle : "))
p=(l+l+b+b)
print ("Perimeter of the rectangle is ", p)
a=(l*b)
print ("Area of the rectangle is ", a)
 Write a program in Python to create project to calculate the Simple Interest.

p = float(input('Enter Princal Amount: '))
r = float(input('Enter Rate of Interest: '))
t = float(input('Enter Time period: '))
SI= (p * r * t )/100.
print('The value of simple interest is', SI )

 Write a program in Python to create a Simple Calculator using Tkinter



from tkinter import *
#1
def add():
n1=int(e1.get())
n2=int(e2.get())
res=n1+n2
r.set(res)
def sub():
n1=int(e1.get())
n2=int(e2.get())
res=n1-n2
r.set(res)
def mult():
n1=int(e1.get())
n2=int(e2.get())
res=n1*n2
r.set(res)
def div():
n1=int(e1.get())
n2=int(e2.get())
res=n1/n2
r.set(res)
#2
master = Tk()
master.geometry("300x200")
r=StringVar()
#3
l1=Label(master, text="First Number")
l2=Label(master, text="Second Number")
l3=Label(master, text="Result:")
e1 = Entry(master,bg="lightgreen")
e2 = Entry(master,bg="lightgreen")

result=Label(master,text="",textvariable=r)
#4

b1 = Button(master, text="+",command=add, bg="yellow",fg="black",


bd="1")
b2 = Button(master, text="-",command=sub, bg="yellow",fg="black", bd="1")
b3 = Button(master, text="x",command=mult,bg="yellow",fg="black",
bd="1")
b4 = Button(master, text="%",command=div, bg="yellow",fg="black",
bd="1")

#5
l1.grid(column=0,row=0)
e1.grid(column=1,row=0)
l2.grid(column=0,row=1)
e2.grid(column=1,row=1)
l3.grid(column=0,row=2)
result.grid(column=1,row=2)
#6
b1.grid(column=0,row=3)
b2.grid(column=1,row=3)
b3.grid(column=2,row=3)
b4.grid(column=0,row=6)
#****************************************
master.mainloop()

 Create a project calorie counter. Your project intakes the amount of proteins,
carbohydrates and fats in a given food item and calculate the amount of
calories present in it.
1 gm carbohydrate is 4 cal
1 gm fat is 9 cal
1 gm protein
Your project should have a button next item which would clear the contents of
text boxes.

from tkinter import *
def calc():
p=int(e1.get())
f=int(e2.get())c=int(e3.get())
res=(4*p)+(9*f)+(4*c)
r.set(res)
def clear():
e1.delete(0,END)
e2.delete(0,END)
e3.delete(0,END)
r.set("")
def ext():
exit()
master = Tk()
master.geometry('300x200')
r=StringVar()
l1=Label(master, text="Proteins")l2=Label(master, text="Fats")
l3=Label(master, text="Carbohydrates")
l4=Label(master, text="Calories:")
e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)
result=Label(master,text="",textvariable=r)
b1 = Button(master,
text="Calculate",command=calc)
b2 = Button(master,
text="Clear",command=clear)
b3 = Button(master,
text="Exit",command=ext)
l1.grid(column=0,row=0)
e1.grid(column=2,row=0)
l2.grid(column=0,row=1)
e2.grid(column=2,row=1)l3.grid(column=0,row=2)
e3.grid(column=2,row=2)
l4.grid(column=0,row=4)
result.grid(column=2,row=4)
b1.grid(column=0,row=6)
b2.grid(column=1,row=6)
b3.grid(column=2,row=6)

 Write a program in Python to create project which includes label of name,


mobile no, Email Id, a radio for gender and spinbox for age.


from tkinter import *
master = Tk()
master.geometry('300x200')
xxx=StringVar()
l1=Label(master, text="Name")
l2=Label(master, text="Mobile Number")
l3=Label(master, text="Email id")
l4=Label(master, text="Gender")
l5=Label(master, text="Age")
e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)
xxx=""
r1 = Radiobutton(master,
text="Male",variable=xxx, value="Male")
r2 = Radiobutton(master,
text="Female",variable=xxx,
value="Female")
r3 = Radiobutton(master,
text="others",variable=xxx, value="others")
s1=Spinbox(master,from_=0,to=100)
l1.grid(column=0,row=0)
e1.grid(column=1,row=0)
l2.grid(column=0,row=1)
e2.grid(column=1,row=1)
l3.grid(column=0,row=2)
e3.grid(column=1,row=2)
l4.grid(column=0,row=3)
r1.grid(column=1,row=3)
r2.grid(column=2,row=3)
r3.grid(column=3,row=3)
l5.grid(column=0,row=4)
s1.grid(column=1,row=4)
master.mainloop()

 Create an application for an Hiring Agency.


Candidate has to fill his name, age , mobile number=> Text Fields
Course=> B.Sc.(CS)/ B.Sc.(IT)/ B.E.=>Radio Button
Marks in SSC, HSC, FY, SY=> Text Fields
In order to be eligible, candidate has to have more that 60% marks in SSC &
HSC and average 55% marks and above in FY/SY
In a Label display whether candidate is eligible or not.
if(ssc_marks>=60 and hsc_marks>=60 and (fy_marks+ sy_marks)/2=55)

from tkinter import *
def check():
ssc_mark=int(e4.get())
hsc_mark=int(e5.get())
fy_mark=int(e6.get())
sy_mark=int(e7.get())
if(ssc_mark>=60 and hsc_mark>=60 and (fy_mark+sy_mark)/2>=55):
r.set("Eligible")
else:
r.set("Not Eligible")
master = Tk()
master.geometry('500x400')
r=StringVar()
rad=StringVar()
l1=Label(master, text="Hiring Agency")
l2=Label(master, text="Name")
l3=Label(master, text="Age")
l4=Label(master, text="Mobile Number")
l5=Label(master, text="Course:")
l6=Label(master, text="Marks in")
l7=Label(master, text="SSC")
l8=Label(master, text="HSC")
l9=Label(master, text="FY")
l10=Label(master, text="SY")
elg=Label(master,text="",textvariable=r)
e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)
e4 = Entry(master)
e5 = Entry(master)
e6 = Entry(master)
e7 = Entry(master)
b1 = Button(master,text="Check Eligibility",command=check)
r1 = Radiobutton(master, text="B.Sc.(CS)", variable=rad,
value="B.Sc.(CS)")
r2 = Radiobutton(master, text="B.Sc.(IT)", variable=rad, value="B.Sc.(IT)")
r3 = Radiobutton(master, text="B.E.", variable=rad, value="B.E.")
l1.grid(column=1,row=0)
l2.grid(column=0,row=1)
l3.grid(column=0,row=2)
l4.grid(column=0,row=3)
l5.grid(column=0,row=5)
l6.grid(column=0,row=8)
l7.grid(column=0,row=9)
l8.grid(column=0,row=10)
l9.grid(column=0,row=11)
l10.grid(column=0,row=12)
e1.grid(column=1,row=1)
e2.grid(column=1,row=2)
e3.grid(column=1,row=3)
e4.grid(column=1,row=9)
e5.grid(column=1,row=10)
e6.grid(column=1,row=11)
e7.grid(column=1,row=12)
r1.grid(column=1,row=5)
r2.grid(column=1,row=6)
r3.grid(column=1,row=7)
b1.grid(column=1,row=13)
master.mainloop()

 Write a program in Python to create a table cust(custid*,custname,custmob) in


customer database.

import MYSQLdb
db=MYSQLdb.connect("localhost","KM","km05","customer_info")
cur= db.cursor()
cur.execute("Create Table customer(custid INT (10) Primary Key, custname
Varchar (20), custmob Varchar(10)" )

print("TABLE has been created" )


db. close()

You might also like