Python
Python
Output:
Enter the number of elements in list:4
Enter element1:10
Enter element2:20
Enter element3:30
Enter element4:10
Non-duplicate items:
[10, 20, 30]
Q2=Write a Python function that accepts a string and calculate the number of
upper case letters and lower case letters.
Output:-
Enter the string you want: Hello I Am TYBBA(CA) Student
Original String : Hello I Am
TYBBA(CA) Student
No. of Upper case characters : 11
No. of Lower case Characters : 11
Q3= Write Python GUI program to create a digital clock with Tkinter to
display the time
root = Tk()
root.title('Clock')
def time():
string = strftime('%H:%M:%S %p')
lbl.config(text = string)
lbl.after(1000, time)
lbl.pack(anchor = 'center')
time()
mainloop()
Q4= Write a Python program to check if a given key already exists in a
dictionary. If key exists replace with another key/value pair.
dict={'monday':1, 'tuesday':2}
print("dictionary is :",dict)
check_key=input("enter the key to check")
check_value=input("enter the value")
if check_key in dict:
print(check_key,"is present")
dict.pop(check_key)
dict[check_key]=check_value
print(dict)
else:
print("check_key is not found")
Output:
Enter key to check:A
Enter Value: 4
Key is present and value of the
key is:
1
Updated dictionary: {'B': 2, 'C':
3, 'A': '4'}
Q5= Write a python script to define a class student having members roll no,
name, age, gender. Create a subclass called Test with member marks of 3
subjects. Create three objects of the Test class and display all the details of
the student with total marks.
class student:
def accept(self):
self.rollno=int(input("enter the student rollno:"))
self.name=(input("enter the student name:"))
self.age=int(input("enter the student age:"))
self.gender=(input("enter the student gender:"))
def display(self):
print("student rollno:",self.rollno)
print("student name:",self.name)
print("student age:",self.age)
print("student gender:",self.gender)
class test(student):
def getdata(self):
self.m1=int(input("enter marks of m1 subject:"))
self.m2=int(input("enter marks of m2 subject:"))
self.m3=int(input("enter marks of m3 subject:"))
def putdata(self):
print("m1 marks:",self.m1)
print("m2 marks:",self.m2)
print("m3 marks:",self.m3)
print("total marks:",self.m1+self.m2+self.m3)
n=int(input("enter how many student:"))
s=[]
for i in range(0,n):
x=input("enetr the object name:")
s.append(x)
print(s)
for j in range(0,n):
s[j]=test()
s[j].accept()
s[j].display()
print("display details of student:")
s[j].getdata()
s[j].putdata()
Output=
Enter How may students2
Enter Object Name:A
['A']
Enter Object Name:B
['A', 'B']
Enter Student Roll No:101
Enter Student Name:Priti
Enter Student Age:10
Enter Student Gender:F
Enter Marks of Marathi Subject10
Enter Marks of Hindi Subject20
Enter Marks of Eglish Subject30
Display Details of Student 1
Student Roll No: 101
Student Name: Priti
Student Age: 10
Student Gender: F
Marathi Marks: 10
Hindi Marks: 20
English Marks: 30
Total Marks: 60
Enter Student Roll No:201
Enter Student Name:Suhas
Enter Student Age:20
Enter Student Gender:M
Enter Marks of Marathi Subject30
Enter Marks of Hindi Subject40
Enter Marks of Eglish Subject50
class Employee:
def __init__(self, id=0, name="", department="", salary=0.0):
self.id = id
self.name = name
self.department = department
self.salary = salary
def accept(self):
self.id = int(input("Enter Employee ID: "))
self.name = input("Enter Employee Name: ")
self.department = input("Enter Department: ")
self.salary = float(input("Enter Salary: "))
def display(self):
print(f"ID: {self.id}")
print(f"Name: {self.name}")
print(f"Department: {self.department}")
print(f"Salary: ${self.salary:.2f}")
class Manager(Employee):
def __init__(self, id=0, name="", department="", salary=0.0, bonus=0.0):
super().__init__(id, name, department, salary)
self.bonus = bonus
def accept(self):
super().accept()
self.bonus = float(input("Enter Bonus: "))
def display(self):
super().display()
print(f"Bonus: ${self.bonus:.2f}")
def total_salary(self):
def main():
n = int(input("Enter number of managers: "))
managers = []
for i in range(n):
print(f"\nEntering details for manager {i + 1}:")
manager = Manager()
manager.accept()
managers.append(manager)
if managers:
max_manager = max(managers, key=lambda m: m.total_salary())
if __name__ == "__main__":
main()
Output=
Enter number of managers: 1
Entering details for manager 1:
Enter Employee ID: 101
Enter Employee Name: Saniya
Enter Department: Testing
Enter Salary: 25000
Enter Bonus: 5000
class string1:
def get_string(self):
self.s=input("enter the string")
def print_string(self):
str=self.s
print("string in uppercase:",str.upper())
s1=string1()
s1.get_string()
s1.print_string()
Output:
Hello i am TYBBA(CA) Student
HELLO I AM TYBBA(CA) STUDEN
Q9= Write a python script to find the repeated items of a tuple
tup=(2,4,5,4,5,6)
print(tup)
count=tup.count(6)
print(count)
Output=
(2, 4, 5, 4, 5, 6)
1
Q10= Write a Python class which has two methods get_String and
print_String. get_String accept a string from the user and print_String print
the string in upper case. Further modify the program to reverse a string
word by word and print it in lower case.
class myfunction:
def get(self):
self.mystr=input("enter the string:")
def display(self):
s=self.mystr
print("upper case:",s.upper())
cnt=len(s)
i=cnt-1
revstr=""
while(i>=0):
revstr=revstr + s[i]
i=i-1
print("reverse and lower case:",revstr.lower())
m=myfunction()
m.get()
m.display()
Output:
Enter any String: saniya
String in Upper Case: SANIYA
String in Reverse & Lower case: ayinas
Q11= Write a Python script using class to reverse a string word by word
class string:
def get_string(self):
self.s=input("enter the string:")
def print_string(self):
str=self.s
print("print string:",str)
cnt=len(str)
i=cnt-1
revstr=""
while(i>=0):
revstr=revstr + str[i]
i=i-1
print("reverse string is:",revstr)
c=string()
c.get_string()
c.print_string()
Output:
Enter any String: Hello
String in Reverse: olleH
Q12= Write Python GUI program to display an alert message when a button
is pressed.
import tkinter as tk
from tkinter.messagebox import *
top = tk.Tk()
top.geometry("300x200")
def onClick():
tk.messagebox.showinfo("Alter Message","I am studying in TY BBA(CA)
class")
button = tk.Button(top,text="Click me",command=onClick)
button.pack()
top.mainloop()
Output:
Q13=Write a Python program to compute element-wise sum of given tuples.
Original lists: (1, 2, 3, 4) (3, 5, 2, 1) (2, 2, 3, 1) Element-wise sum of the said
tuples: (6, 9, 8, 6)
Output=
Original lists:
(1, 2, 3, 4)
(3, 5, 2, 1)
(2, 2, 3, 1)
Element-wise sum of the said tuples:
(6, 9, 8, 6)
Q14= Write Python GUI program to add menu bar with name of colors as
options to change the background color as per selection from menu option.
file.add_command(label="New")
file.add_command(label="Exit", command=app.quit)
edit.add_command(label="Cut")
edit.add_command(label="Copy")
edit.add_command(label="Paste")
menubar.add_cascade(label="File", menu=file)
menubar.add_cascade(label="Edit", menu=edit)
app.config(menu=menubar)
app.mainloop()
Q15= Write a Python GUI program to create a label and change the label
font style (font name, bold, size) using tkinter module.
import tkinter as tk
root = tk.Tk()
root.title("Welcome to student")
my_label = tk.Label(root,text="hello welcome all of you", font= ("times new
roman",10))
my_label.grid(column=0,row=0)
root.mainloop()
Q16=: Write a python program to count repeated characters in a string.
Sample string: 'thequickbrownfoxjumpsoverthelazydog' Expected output: o-
4, e-3, u-2, h-2, r-2, t-2
str="thequickbraconfoxjumpsovertlongday"
dict={}
for ch in str:
if ch in dict:
dict[ch]=dict[ch]+1
else:
dict[ch] = 1
for key in dict:
print(key,"_",dict[key])
Output:
Enter the string you want: Hello I Am TYBBA(CA) Student
4
A3
e2
l
2
B
2
t2
Q17=Write a program to implement
the concept of queue using list
q=[2,4,6]
def insert():
if len(q)==size:
print("queue is full:")
else:
element=input("enter the
number:")
q.append(element)
print(element,"is added")
def delete():
if len(q)==0:
print("queue is empty")
else:
e=q.pop(0)
print("element removed:")
def display():
print(q)
size=int(input("enter the size of
queue:"))
print("select the option:1.insert 2.delete
3.display")
choice=int=(input("enter the choice"))
if choice==1:
insert()
elif choice==2:
delete()
elif choice==3:
display()
else:
print("invalid")
Output:
Enter the size of Queue:2
Select the Operation: 1.Insert 2.Delete
3.Display 4.Quit 1
Enter the
element:10
10 is added
to the
Queue!
Select the Operation: 1.Insert 2.Delete
3.Display 4.Quit 1
Enter the
element:20
20 is added
to the
Queue!
Select the Operation: 1.Insert 2.Delete
3.Display 4.Quit
1
Queue is Full!!!!
Select the Operation: 1.Insert 2.Delete
3.Display 4.Quit 3
['10', '20']
Select the Operation: 1.Insert 2.Delete
3.Display 4.Quit
2
element removed!!: 10
Select the Operation: 1.Insert 2.Delete
3.Display 4.Quit 4
Q18= Write a Python GUI program to accept dimensions of a cylinder and
display the surface area and volume of cylinder.
import tkinter as tk
window=tk.Tk()
window.geometry("300x300")
def volume():
pi=22/7
height=txtheight.get(1.0,"end-1c")
radius=txtradius.get(1.0,"end-1c")
volume=pi*float(radius)*float(radius)*float(height)
lbl.config(text="volume is :" + str(volume))
def surfacearea():
pi=22/7
height=txtheight.get(1.0,"end-1c")
radius=txtradius.get(1.0,"end-1c")
sur_area=((2*pi*float(radius)*float(radius))+((2*pi*float(radius)*float(height))))
lbl.config(text="surface area is :"+str(sur_area))
txtheight=tk.Text(window,height=2,width=20)
txtradius=tk.Text(window,height=2,width=20)
txtheight.pack()
txtradius.pack()
b1=tk.Button(window,text="volume",command=volume)
b2=tk.Button(window,text="surface area",command=surfacearea)
b1.pack()
b2.pack()
lbl=tk.Label(window,text="")
lbl.pack()
window.mainloop()
Output=
Q19= Write a Python class named
Student with two attributes
student_name, marks. Modify the
attribute values of the said class and
print the original and modified values
of the said attributes
class student:
def accept(self):
self.name=input("enter the name")
self.mark=input("enter the mark")
def modify(self):
self.oldmark=self.mark
self.mark=int(input("enter the new
mark"))
print("oldmark:",self.oldmark)
print("student name is :,self.name)
print("student mark is :",self.mark)
s1=student()
s1.accept()
s1.modify()
Output:
Enter Student Name:Geeta
Enter Student Total Marks:67
Enter Student New Total Marks:78
Student Name: Geeta
Old Total Mark: 67
New Total Mark: 78
Q20= Write a python program to accept string and remove the characters
which have odd index values of given string using user defined function
def myfunction():
str=input("enter the string")
cnt=len(str)
newstring=""
for i in range(0,cnt):
if i%2==0:
newstr=newstr=str[i]
print("new string with removed index is :",newstring)
myfunction()
Output:
Enter the string you want:
Hello
Hlo
Q21=Write a python script to create a class Rectangle with data member’s
length, width and methods area, perimeter which can compute the area and
perimeter of rectangle
class rect :
def __init__(self,length,width):
self.length=length
self.width=width
def rectarea(self):
self.area=self.length*self.width
print("area of rectangle is :",self.area)
def rectperi(self):
self.peri=2*(self.length+self.width)
print("perimeter of rectangle is :",self.peri)
length=int(input("enter length:"))
width=int(input("enter width:"))
b=rect(length,width)
b.rectarea()
b.rectperi()
Output:
Enter Length:2
Enter Width:3
Area of Rectangle: 6
Perimeter of Rectangle: 10
Q22= Write Python GUI program that takes input string and change letter
to upper case when a button is pressed.
import tkinter as tk
window= tk.Tk()
window.title("case changer")
window.geometry("200x100")
def upper_case():
result=entry.get()
lbl.config(text=result.upper())
entry=tk.Entry(window)
entry.pack()
b1=tk.Button(window,text="upper
case",command="upper_case")
b1.pack()
lbl=tk.Label(window,text="")
lbl.pack()
window.mainloop()
Output =
Q23= Create a list a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] and write a python
program that prints out all the elements of the list that are less than 5
List=[1,1,2,3,5,8,13,21,34,55,89]
cnt = len(List)
for i in range(0,cnt):
if(List[i]<10):
print(List[i])
Output:
[1, 1, 2, 3]
Q24= Write a python script to define the class person having members name,
address. Create a subclass called Employee with members staffed salary.
Create 'n' objects of the Employee class and display all the details of the
employee.
class person:
def accept(self):
self.Name=input("Enter the Name
of person")
self.Address=input("Enter the
Address of person")
def display(self):
print("person Name:",self.Name)
print("person
Address:",self.Address)
class Employee(person):
def get(self):
self.sal=int(input("Enter Salary of
Employee"))
def put(self):
print("Salary of
Employee",self.sal)
lst[j].accept()
lst[j].get()
Output:
Enter How may Employee: 1
Enter Object Name:A
['A']
window=Tk()
window.title("multiplication Table")
def multiply():
n=int(entry.get())
str1="table of"+str(n) + "\n------------
----\n"
for x in range(1,11):
str1 = str1 + " " + str (n) + "x" +
str(x) + "=" + str(n*x) + "\n"
output_lbl.config(text=str1,justify=LEF
T)
lbl=Label(text ="enter a
number",font=('Arial',12))
output_lbl=Label(font=('Arial',12))
entry=Entry(font=('Arial',12),width=5)
btn=Button(text="Click to
multiply",font=('Arial',12),command=m
ultiply)
lbl.grid(row=0,column=0,padx=10,pady
=10)
entry.grid(row=0,column=1,padx=10,pa
dy=10,ipady=10)
btn.grid(row=0,column=2,padx=10,pad
y=10)
output_lbl.grid(row=1,column=0,colum
nspan=3,padx=10,pady=10)
window.mainloop()
Output=
Q26= Define a class named Shape and its subclass(Square/ Circle). The
subclass has an init function which takes an argument (Lenght/redious).
Both classes should have methods to calculate area and volume of a given
shape.
class Shape:
pass
class Square(Shape):
def __init__(self,l2):
self.l=12
def SArea(self):
a=self.l * self.l
print("Area of Square:",a)
def Svolume(self):
v=3 * self.l
print("volumn of Square:",p)
class Circle(Shape):
def __init__(self,r2):
self.r=r2
def CArea(self):
a=3.14 * self.r * self.r
print("Area of Circle:",a)
def SPerimeter(self):
p=4 * self.l
print("Perimeter of Square:",p)
Output:
Enter Length of Square: 2
Area of Square: 4
Perimeter of Square: 8
Enter Radius of Circle: 3
Area of Circle: 28.259999999999998
Circumference of Circle: 18.84
Q27= Write a python program to create a class Circle and Compute the
Area and the circumferences of the circle.(use parameterized constructor)
class Circle():
def __init__(self,Radius):
self.Radius=Radius
def area(self):
a=pi*self.Radius*self.Radius
return round(a,2)
def circumference(self):
c=2*self.Radius*pi
return round(c,2)
Output=
Enter radius of circle:5
Area of circle is :78.54
Circumference of circle is :31.42
Q28=Write a Python script to generate and print a dictionary which contains
a number (between 1 and n) in the form(x,x*x). Sample Dictionary (n=5)
Expected Output: {1:1, 2:4, 3:9, 4:16, 5:25}
Output=
Input a number : 5
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Q29= Write a Python Program to Check if given number is prime or not.
Also find factorial of the given no using user defined function.
def prime(num):
flag=0
for i in range(2,num):
if num%i==0:
flag=1
break
if flag == 0:
print("Number is prime:")
else:
print("Number is not prime:")
def fact(num):
f=1
for i in range(1,num+1):
f=f*I
print("Factorial of given no is :",f)
Output:
Enter any number to Check:3
Number is Prime
Factorial of Given number is: 6
Q30= Write Python GUI program which accepts a number n to displays each
digit of number in words.
def printvalue(digit):
if(digit == '0'):
print("zero",end="")
if digit =='1':
print("one",end="")
if digit =='2':
print("two",end="")
if digit =='3':
print("three",end="")
if digit =='4':
print("four",end="")
if digit =='5':
print("five",end="")
if digit =='6':
print("six",end="")
if digit =='7':
print("seven",end="")
if digit =='8':
print("eigth",end="")
if digit =='9':
print("nigth",end="")
def printword(N):
i=0
length=len(N)
while i<length:
printvalue(N[i])
i=i+1
N="0123456789"
printword(N)
Output=
Zero one two three four five six seven eigth night
Q31= Write a Python script to Create a Class which Performs Basic
Calculator Operations
class calculation:
def add(self):
self.a = int(input("enter the first no:"))
self.b = int(input("enter the second no:"))
self.c=self.a+self.b
print("addition of no:",self.c)
def sub(self):
self.a = int(input("enter the first no:"))
self.b = int(input("enter the second no:"))
self.c=self.a-self.b
print("substraction of no:",self.c)
def multi(self):
self.a = int(input("enter the first no:"))
self.b = int(input("enter the second no:"))
self.c=self.a*self.b
print("multiplication of no:",self.c)
def div(self):
self.a = int(input("enter the first no:"))
self.b = int(input("enter the second no:"))
self.c=self.a/self.b
print("division of no:",self.c
obj = calculation()
while True:
print("\n1. addition")
print("\n2. substraction")
print("\n3. multiplication")
print("\n4. division")
print("\n5. exit")
ch = int(input("enter your choice:"))
if ch ==1:
obj.add()
elif ch ==2:
obj.sub()
elif ch ==3:
obj.multi()
elif ch==4:
obj.div()
elif ch==5:
print("\n program stopped")
break
else:
print("you have entered wrong option:”)
Output=
1. addition
2. substraction
3. multiplication
4. division
5. exit
l1=[1,2,3,5]
l2=["saniya","shaikh"]
t1=tuple(l1)
t2=tuple(l2)
t3=t1 + t2
print(t3)
Output:
Enter number of elements of first list : 3
10
20
30
[10, 20, 30]
Enter number of elements of second list
:3
Q33=: Python Program to Create a Class in which One Method Accepts a
String from the User and Another method Prints it. Define a class named
Country which has a method called print Nationality. Define subclass named
state from Country which has a mehtod called printState. Write a method to
print state, country and nationality.
class country:
def accept(self):
self.cname = input("enter the country name:")
self.nationality = input("enter the nationality:")
def print(self):
print(self.cname)
def print_nationality(self):
print(self.nationality)
class state(country):
def printstate(self):
self.state = input("enter state:")
print(self.state)
obj=state()
obj.accept()
obj.printstate()
obj.print()
obj.print_nationality()
Output:
Enter Country Name: India
Enter State Name: Maharashtra
Country Name is: India
State Name is: Maharashtra
Q34= Write a python script to generate Fibonacci terms using generator
function.
def generator(r):
a=0;b=1
for i in range (1,r):
print(b)
a,b=b,a+b
Output=
Enter a number :5
0
1
1
2
3
Q35= Write Python GUI program to accept a number n and check whether it
is Prime, Perfect or Armstrong number or not. Specify three radio buttons.
def armstrong():
number=int(numberFeald.get())
count = 0
temp = number
while temp > 0:
digit = temp % 10
count += digit ** 3
temp //= 10
if number == count:
armstrong1.select()
print(number, 'is an Armstrong number')
else:
armstrong1.deselect()
print(number, 'is not an Armstrong number')
def prime():
number=int(numberFeald.get())
if number > 1:
for i in range(2,number):
if (number % i) == 0:
prime1.deselect()
print(number,"is not a prime number")
print(i,"times",number//i,"is",number)
break
else:
prime1.select()
print(number,"is a prime number")
else:
prime1.deselect()
print(number,"is not a prime number")
root=Tk()
root.title('Prime, Perfect or Armstrong number')
root.geometry('300x200')
numberFeald=Entry(root)
numberFeald.pack()
Button1=Button(root,text='Button',command=lambda:[armstrong(),prime(),perfect
()])
Button1.pack()
prime2=IntVar()
perfect2=IntVar()
armstrong2=IntVar()
armstrong1=Radiobutton(root,text='armstrong',variable=armstrong2,value=1)
prime1=Radiobutton(root,text='prime',variable=prime2,value=1)
perfect1=Radiobutton(root,text='perfect',variable=perfect2,value=1)
armstrong1.pack()
prime1.pack()
perfect1.pack()
root.mainloop()