Python Practice Examples
Python Practice Examples
print ("The number of lines present in the 16. Write a Python program to count the frequency
file",line_count) of each word present in a given file.
print ("The number of words present in the fh = open("c:\\python\\sample.txt", "r")
file",word_count) lines=fh.readlines()
print ("The number of characters present in the d = dict()
file",char_count) for line in lines:
line = line.strip()
13. Develop a Python program to print each word line = line.lower()
present in a given file in reverse order. words = line.split(" ")
outfile = open("c:\\Python\\outfile.txt" , "w" ) for word in words:
inputfile=open ( "c:\\Python\\Sample.txt" , "r" ) if word in d:
lines = inputfile.readlines() d[word] = d[word] + 1
for line in lines: else:
words=line.split() d[word] = 1
for word in words:
word_1=word[::-1] for key in list(d.keys()):
word_1=word_1+" " print(key, ":", d[key])
outfile.write(word_1)
outfile.write('\n')
outfile.close()
14. Write a Python program to validate a USN of UG 17. Develop a Python program to create and execute
and PG student of VTU. two threads.
import re #1.Creating a Thread without using any Class
def val_USN(s): from threading import *
ug_pat='[1-4]{1}[a-zA-Z]{2}[0-9]{2}[a-zA-Z]{2}[0- def display():
9]{3}' for i in range(1,11):
pg_pat='[1-4]{1}[a-zA-Z]{2}[0-9]{2}[a-zA-Z]{3}[0- print("Child Thread ")
9]{2}'
pat=ug_pat + '|' + pg_pat #combine two patterns t=Thread(target=display)
using |(OR) t.start() #starting of Thread
for i in range(1,11):
if re.search(pat,s): print("Main Thread ")
print('valid')
else:
print('invalid')
val_USN('1DA21IS001')
val_USN('1ABC034KX')
val_USN('1DA15MCA01')
18. Develop a Python GUI program to add two integers
and display their sum.
from tkinter import *
root = Tk()
root.title("Sum Two Numbers With GUI Using Python
Source Code")
width = 400
height = 280
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
root.resizable(0, 0)
#==============================METHODS================
========================
def calculate():
if not num1.get() and not num2.get():
print("Please enter a number")
else:
res=int(num1.get())+int(num2.get())
lbl_text.config(text = "The answer is %d" %
(res))
num1.set=""
num2.set=""
#==============================FRAMES=================
========================
Top = Frame(root, bd=2, relief=RIDGE)
Top.pack(side=TOP, fill=X)
Form = Frame(root, height=200)
Form.pack(side=TOP, pady=20)
#==============================LABELS=================
========================
lbl_title = Label(Top, text = "Sum Two Numbers With
GUI Using Python Source Code", font=('arial', 12))
lbl_title.pack(fill=X)
lbl_num1 = Label(Form, text = "Number 1:",
font=('arial', 14), bd=15)
lbl_num1.grid(row=0, sticky="e")
lbl_num2 = Label(Form, text = "Number 2:",
font=('arial', 14), bd=15)
lbl_num2.grid(row=1, sticky="e")
lbl_text = Label(Form)
lbl_text.grid(row=2, columnspan=2)
#==============================ENTRY
WIDGETS==================================
num1 = Entry(Form, font=(14))
num1.grid(row=0, column=1)
num2 = Entry(Form, font=(14))
num2.grid(row=1, column=1)
#==============================BUTTON
WIDGETS=================================
btn_calculate = Button(Form, text="Calculate",
width=45, command=calculate)
btn_calculate.grid(pady=25, row=3, columnspan=2)