Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Python Assignment (10)

The document contains a series of Python programming tasks, including file operations, exception handling, drawing shapes using Tkinter, and creating a simple calculator. Each task is accompanied by code snippets that demonstrate how to implement the required functionality. The tasks range from basic file manipulation to graphical user interface applications.

Uploaded by

xxxjack2018
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Assignment (10)

The document contains a series of Python programming tasks, including file operations, exception handling, drawing shapes using Tkinter, and creating a simple calculator. Each task is accompanied by code snippets that demonstrate how to implement the required functionality. The tasks range from basic file manipulation to graphical user interface applications.

Uploaded by

xxxjack2018
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Q1) Write a program in Python to create a file

"data.txt" and write 5 lines to it.

Code;
F = open(“data.txt” , “w”)
a = f. write(“here you can write anythink/n”)
print(a)
f.close()
2)Write a program in Python to read the contents of
"data.txt" and display them.

Code;
F = open (“read.txt” , “rt”)
Content = f.read()
print(content)
f.close()
Q3) Write a program in Python to copy the contents of
"data1.txt" to another file "data2.txt"

Code;
# opens original file
file1 = open("data1.txt" , "r")
# opens new file
file2 = open("data2.txt" , "w")
#for each line in old file
for line in file1:
#write that line to the new file
file2.write(line)
#close file 1
file1.close()
#close file2
file2.clsoe()
4) Write a program in Python to raise an “Invalid Marks
Exception ;

Code:

class InvalidmarkException(Exception):
pass

try:
mark=input("Enter your mark :")
x="60"
if mark==x:
print("Correct mark")
else:
raise InvalidmarkException
except InvalidmarkException:
print("Incorrect mark")
Q5) Write a program in Python to raise an “Invalid Colour
Exception”

Code;
class InvalidColourException(Exception):
pass

try:
colour=input("Enter a Colour :")
x="blue"
if colour==x:
print("Correct Colour")
else:
raise InvalidColourException
except InvalidColourException:
print("Incorrect Colour")

Q6) Write a program in Python to create a figure which


consist of rectangle, a circle, an arc and
line it..

Code;
from tkinter import*
master=Tk()
w=canvas(master,width=500,hight=300)
w.pack()
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="red")
w.create_polygone(10,10,100,50,200,10,30
0,50)
mainloop()
Q7) Write a Python Program to draw the
following figures:
i) House
ii) Sun
iii) Smiley
iv) Indian Flag
v) Fish
(House):

Code;

from tkinter import *


master=Tk()
w=Canvas(master,width=1000, height=600)
w.create_rectangle(130,80,900,500,fill="gre
en")
w.create_rectangle(600,500,380,280,fill="re
d")
w.create_rectangle(530,500,450,420,fill="ye
llow")
w.create_rectangle(450,360,400,310,fill="ye
llow")
w.create_rectangle(535,360,585,310,fill="ye
llow")
w.create_line(380,280,490,80,fill="red",widt
h="3")
w.create_line(600,280,490,80,fill="red",widt
h="3")
w.pack()
mainloop()
(Sun):

Code;

from tkinter import *


master=Tk()
w=Canvas(master,width=1000, height=600)
w.create_rectangle(130,80,900,500,)
w.create_oval(420,390,650,160,fill='yellow')
w.create_line(650,275,720,275,fill='yellow',
width='3')
w.create_line(420,275,350,275,fill='yellow',
width='3')
w.create_line(535,390,535,460,fill='yellow',
width='3')
w.create_line(535,160,535,90,fill='yellow',wi
dth='3')
w.create_line(620,195,670,135,fill='yellow',
width='3')
w.create_line(620,355,670,420,fill='yellow',
width='3')
w.create_line(450,350,400,415,fill='yellow',
width='3')
w.create_line(450,195,400,135,fill='yellow',
width='3')
w.pack()
mainloop()
(Smile):

Code;

from tkinter import *


master=Tk()
w=Canvas(master,width=1000,
height=600)
w.create_rectangle(130,80,900,500,)
w.create_oval(420,390,650,160,fill='y
ellow')
w.create_oval(470,210,500,240,fill='
black')
w.create_oval(570,210,600,240,fill='
black')
w.create_arc(470,280,600,340,start=
0,extent=-180,fill='black')
w.pack()
mainloop()
(indian flag):

Code;

from tkinter import *


master=Tk()
w=Canvas(master,width=600,
height=900)
w.create_rectangle(100,50,500,600)
w.create_rectangle(190,100,200,550
,fill='brown')
w.create_rectangle(200,100,320,130
,fill='red')
w.create_rectangle(200,130,320,160
,fill='white')
w.create_rectangle(200,160,320,190
,fill='green')
w.create_oval(245, 130, 275, 160,)
w.create_line(245,145,275,145,fill='bl
ue',width='2')
w.create_line(260,130,260,160,fill='bl
ue',width='2')
w.create_line(270,133,249,158,fill='bl
ue',width='2')
w.create_line(249,133,270,158,fill='bl
ue',width='2')
W.pack()
mainloop()
(fish):

Code;

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='
black')
w.create_polygon(750,240,800,180,8
00,300)
w.create_oval(380,210,395,225,fill='
brown')
w.create_arc(680,300,500,100,start=
90,extent=90,fill="black")
w.create_arc(680,180,500,380,start=
180,extent=90,fill="black")
w.pack()
mainloop()
Q8) Write a program to draw a Mickey
Mouse face.

Code;

from tkinter import*

master=Tk()
w=Canvas(master,width=500,height=500)
w.pack()
w.create_oval(99,154,348,387,fill="black")
w.create_oval(58,99,153,203,fill="black")
w.create_oval(287,89,396,197,fill="black")
w.create_oval(138,163,226,317,fill="navajo
white")
w.create_oval(208,162,302,320,width=0,fill
="navajo white")
w.create_oval(119,258,332,383,width=0,fill=
"navajo white")
w.create_oval(180,225,218,293,fill="white")
w.create_oval(195,248,215,281,fill="black")
w.create_oval(222,224,259,288,fill="white")
w.create_oval(224,245,243,280,fill="black")
w.create_arc(150,300,299,377,start=0,exte
nt=-180,fill="black")
w.create_oval(202,353,253,376,fill="red")
w.create_oval(192,290,242,322,fill="black")
w.pack()
mainloop()
Q9) Write a program in Python to create
project to calculate the Area of Rectangle,
Perimeter and
Clear button.

Code;

# Python3 code to find area


# and perimeter of rectangle

# Utility function
def areaRectangle(a, b):
return (a * b)

def perimeterRectangle(a, b):


return (2 * (a + b))

# Driver function
a = 5;
b = 6;
print ("Area = ", areaRectangle(a, b))
print ("Perimeter = ", perimeterRectangle(a,
b))
Q10) Write a program in Python to create
project to calculate the Simple Interest.

Code;

P = float(input('Enter Princal Amount:


'))
R = float(input('Enter Rate of Interest: '))
T = float(input('Enter Time period: '))
simple_interest = (P * R * T )/100.
print('The value of simple interest is
',simple_interest)
Q11) Write a program in Python to create a
Simple Calculator using Tkinter

Code;

from tkinter import *

win = Tk() # This is to create a basic


window
win.geometry("312x324") # this is for the
size of the window
win.resizable(0, 0) # this is to prevent from
resizing the window
win.title("Calculator")

###################Starting with
functions ####################
# 'btn_click' function :
# This Function continuously updates the
# input field whenever you enters a number

def btn_click(item):
global expression
expression = expression + str(item)
input_text.set(expression)

# 'bt_clear' function :This is used to clear


# the input field

def bt_clear():
global expression
expression = ""
input_text.set("")

# 'bt_equal':This method calculates the


expression
# present in input field

def bt_equal():
global expression
result = str(eval(expression)) # 'eval':This
function is used to evaluates the string
expression directly
input_text.set(result)
expression = ""

expression = ""

# 'StringVar()' :It is used to get the instance


of input field

input_text = StringVar()

# Let us creating a frame for the input field

input_frame = Frame(win, width=312,


height=50, bd=0,
highlightbackground="black",
highlightcolor="black", highlightthickness=2)
input_frame.pack(side=TOP)

#Let us create a input field inside the


'Frame'

input_field = Entry(input_frame, font=('arial',


18, 'bold'), textvariable=input_text,
width=50, bg="#eee", bd=0, justify=RIGHT)

input_field.grid(row=0, column=0)

input_field.pack(ipady=10) # 'ipady' is
internal padding to increase the height of
input field

#Let us creating another 'Frame' for the


button below the 'input_frame'
btns_frame = Frame(win, width=312,
height=272.5, bg="grey")

btns_frame.pack()

# first row

clear = Button(btns_frame, text = "C", fg =


"black", width = 32, height = 3, bd = 0, bg =
"#eee", cursor = "hand2", command =
lambda: bt_clear()).grid(row = 0, column =
0, columnspan = 3, padx = 1, pady = 1)

divide = Button(btns_frame, text = "/", fg =


"black", width = 10, height = 3, bd = 0, bg =
"#eee", cursor = "hand2", command =
lambda: btn_click("/")).grid(row = 0, column
= 3, padx = 1, pady = 1)

# second row
seven = Button(btns_frame, text = "7", fg =
"black", width = 10, height = 3, bd = 0, bg =
"#fff", cursor = "hand2", command =
lambda: btn_click(7)).grid(row = 1, column
= 0, padx = 1, pady = 1)

eight = Button(btns_frame, text = "8", fg =


"black", width = 10, height = 3, bd = 0, bg =
"#fff", cursor = "hand2", command =
lambda: btn_click(8)).grid(row = 1, column
= 1, padx = 1, pady = 1)

nine = Button(btns_frame, text = "9", fg =


"black", width = 10, height = 3, bd = 0, bg =
"#fff", cursor = "hand2", command =
lambda: btn_click(9)).grid(row = 1, column
= 2, padx = 1, pady = 1)
multiply = Button(btns_frame, text = "*", fg =
"black", width = 10, height = 3, bd = 0, bg =
"#eee", cursor = "hand2", command =
lambda: btn_click("*")).grid(row = 1, column
= 3, padx = 1, pady = 1)

# third row

four = Button(btns_frame, text = "4", fg =


"black", width = 10, height = 3, bd = 0, bg =
"#fff", cursor = "hand2", command =
lambda: btn_click(4)).grid(row = 2, column
= 0, padx = 1, pady = 1)

five = Button(btns_frame, text = "5", fg =


"black", width = 10, height = 3, bd = 0, bg =
"#fff", cursor = "hand2", command =
lambda: btn_click(5)).grid(row = 2, column
= 1, padx = 1, pady = 1)
six = Button(btns_frame, text = "6", fg =
"black", width = 10, height = 3, bd = 0, bg =
"#fff", cursor = "hand2", command =
lambda: btn_click(6)).grid(row = 2, column
= 2, padx = 1, pady = 1)

minus = Button(btns_frame, text = "-", fg =


"black", width = 10, height = 3, bd = 0, bg =
"#eee", cursor = "hand2", command =
lambda: btn_click("-")).grid(row = 2, column
= 3, padx = 1, pady = 1)

# fourth row

one = Button(btns_frame, text = "1", fg =


"black", width = 10, height = 3, bd = 0, bg =
"#fff", cursor = "hand2", command =
lambda: btn_click(1)).grid(row = 3, column
= 0, padx = 1, pady = 1)
two = Button(btns_frame, text = "2", fg =
"black", width = 10, height = 3, bd = 0, bg =
"#fff", cursor = "hand2", command =
lambda: btn_click(2)).grid(row = 3, column
= 1, padx = 1, pady = 1)

three = Button(btns_frame, text = "3", fg =


"black", width = 10, height = 3, bd = 0, bg =
"#fff", cursor = "hand2", command =
lambda: btn_click(3)).grid(row = 3, column
= 2, padx = 1, pady = 1)

plus = Button(btns_frame, text = "+", fg =


"black", width = 10, height = 3, bd = 0, bg =
"#eee", cursor = "hand2", command =
lambda: btn_click("+")).grid(row = 3, column
= 3, padx = 1, pady = 1)

# fourth row
zero = Button(btns_frame, text = "0", fg =
"black", width = 21, height = 3, bd = 0, bg =
"#fff", cursor = "hand2", command =
lambda: btn_click(0)).grid(row = 4, column
= 0, columnspan = 2, padx = 1, pady = 1)

point = Button(btns_frame, text = ".", fg =


"black", width = 10, height = 3, bd = 0, bg =
"#eee", cursor = "hand2", command =
lambda: btn_click(".")).grid(row = 4, column
= 2, padx = 1, pady = 1)

equals = Button(btns_frame, text = "=", fg =


"black", width = 10, height = 3, bd = 0, bg =
"#eee", cursor = "hand2", command =
lambda: bt_equal()).grid(row = 4, column =
3, padx = 1, pady = 1)

win.mainloop()
12)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.

Code;

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_=18,to=75)

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()
Q13) 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.
a) 1 gm carbohydrate is 4 cal
b) 1 gm fat is 9 cal
c) 1 gm protein
Your project should have a button next item
which would clear the contents of text
boxes.

Code;

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)
Q14) Create a project for Reshma Beauty
Parlor. Include checkboxes for the following
services.
a) Hair Styling
b) Manicure & Pedicure

Code;

from tkinter import *

master = Tk()

master.geometry('300x200')

xxx=StringVar()
l1=Label(master, text="Reshma Beauty
Parlour")
l2=Label(master, text="Service")

l3=Label(master, text="Discount")

l4=Label(master, text="Total Cost")


l5=Label(master, text="Discount")
l5=Label(master, text="Net Payable")

e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)
e4 = Entry(master)
e5 = Entry(master)

r1 = Radiobutton(master, text="Total
Cost",variable=xxx, value="Total Cost")
r2 = Radiobutton(master,
text="Discount",variable=xxx,
value="Discount")
r3 = Radiobutton(master, text="Net
Payable",variable=xxx, value="Net
Payable")

l1.grid(column=0,row=0)

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

l3.grid(column=0,row=2)

l4.grid(column=0,row=3)
e4.grid(column=1,row=3)

l5.grid(column=0,row=4)
e5.grid(column=1,row=4)

r1.grid(column=1,row=5)
r2.grid(column=2,row=5)
r3.grid(column=3,row=5)
master.mainloop()
Q15) 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)

Code;
from tkinter import *

def check():
ssc_marks=int(e4.get())
hsc_marks=int(e5.get())
fy_marks=int(e6.get())
sy_marks=int(e7.get())
if(ssc_marks>=60 and hsc_marks>=60
and (fy_marks+sy_marks)/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)
elg.grid(column=1,row=14)

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()
Q16) Write a program in Python to create a
table cust(custid*,custname,custmob) in
customer
database.

Code;

import MYSQLdb
db=MYSQLdb.connect("localhost","r
oot","rdnc","customer")
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()
Q17) Write a program to create a
table student(rollno*, sname , sclass) in
the fycs database of
MySQL.
Write a program to insert one row in
the student table.
Code;

import MYSQLdb
db=MYSQLdb.connect("localh
ost","root","rdnc","fycs")
cur= db.cursor()
cur.execute("Create Table
student (roll no INT (5) Primary
Key, sname Varchar (20), sclass
Varchar(10”)

print(“TABLE has been


created” )
********THANK YOU********

You might also like