Python
Python
2. Write a program based on GUI, window with a label and two buttons
import tkinter as tk
def button1_click():
label.config(text="Button 1 clicked")
def button2_click():
label.config(text="Button 2 clicked")
# Create a label
label = tk.Label(root, text="Welcome to the GUI")
class calculator_implementation():
def __init__(self,in_1,in_2):
self.a=in_1
self.b=in_2
def add_vals(self):
return self.a+self.b
def multiply_vals(self):
return self.a*self.b
def divide_vals(self):
return self.a/self.b
def subtract_vals(self):
return self.a-self.b
input_1 = int(input("Enter the first number: "))
input_2 = int(input("Enter the second number: "))
print("The entered first and second numbers are : ")
print(input_1, input_2)
my_instance = calculator_implementation(input_1,input_2)
choice=1
while choice!=0:
print("0. Exit")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
choice=int(input("Enter your choice... "))
if choice==1:
print("The computed addition result is : ",my_instance.add_vals())
elif choice==2:
print("The computed subtraction result is : ",my_instance.subtract_vals())
elif choice==3:
print("The computed product result is : ",my_instance.multiply_vals())
elif choice==4:
print("The computed division result is : ",round(my_instance.divide_vals(),2))
elif choice==0:
print("Exit")
else:
print("Sorry, invalid choice!")
print()
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = 7
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
12. Python Program to Create a Class and Compute the Area and the Perimeter of the a.
Circle(with interfaces)
import math
class circle_compute():
def __init__(self,my_radius):
self.radius=my_radius
def area_calculate(self):
return math.pi*(self.radius**2)
def perimeter_calculate(self):
return 2*math.pi*self.radius
my_result = int(input("Enter the radius of circle..."))
my_instance = circle_compute(my_result)
print("The radius entered is :")
print(my_result)
print("The computed area of circle is ")
print(round(my_instance.area_calculate(),2))
print("The computed perimeter of circle is :")
print(round(my_instance.perimeter_calculate(),2))
17. . Python Program to Copy the Contents of One File into Another.
18. Python Program to Swap the First and Last Value of a List
19. Python Program to read a .CSV file and remove duplicate values
23. Python Program to Take in the Marks of 5 Subjects and Display the Grade.
24. Python Program to Count Number of Lowercase Characters in a String a. Python Program to
plot a histogram for any chosen data.
26. Python Program to plot a 4 subplots for any chosen data. (scatterplot)
27. Python program to create a class with two methods to demonstrate method overloading
28. Python program to read a file whether it exists if yes add some text to the file or create a file
and add some text.
import os
file_path = "example.txt"
text_to_add = "This is some sample text to add to the file."
if os.path.isfile(file_path):
with open(file_path, "a") as f:
f.write("\n" + text_to_add)
else:
with open(file_path, "w") as f:
f.write(text_to_add)
In this program, the variable file_path contains the name and path of the file you want
to create or modify, and text_to_add contains the text you want to add to the file.
First, the os.path.isfile function checks if the file exists. If it does, the program opens
the file in "append" mode using the open function with the "a" argument. This allows
the program to add text to the end of the file without overwriting any existing text.
The with statement ensures that the file is properly closed after writing.
If the file does not exist, the program opens a new file with the same name using the
open function with the "w" argument. This creates a new empty file. The program then
writes the text_to_add to the file using the write function.
Note that in the with statement, the open function returns a file object that is
automatically closed when the with block is exited. This is a safer and more efficient
way to handle file I/O in Python, as it ensures that the file is always closed even if an
error
30. Python program to create two threads where one thread will print odd numbers and other will
print even numbers,
Code:
import threading
def print_odd():
for i in range(1, 10, 2):
print(i)
def print_even():
for i in range(2, 10, 2):
print(i)
t1 = threading.Thread(target=print_odd)
t2 = threading.Thread(target=print_even)
t1.start()
t2.start()
t1.join()
t2.join()
print("Printing Complete!")
32. Python Program that Displays which Letters are Present in Both the Strings
String1=int(input(“string1”))
String2=int(input(“string2”))
Set1=set(string1)
Set2=set(string2)
Common=set1.intersection(set2)
If common==0:
Print(“No common”)
Else:
For I in common:
Print(char)
33. Python Program to Create a Class in which One Method Accepts a String from the User and
Another Prints it
# creating a class
class stringClass():
# use the constructor to initialize the class's values(here it is a string).
def __init__(self):
self.string = ""
# Creating two methods inside the class.
# getString: which scans the value of the given string
def getString(self):
# using input() function to scan the value of the string.
self.string = input("Enter some random string = ")
# putString: Which prints the value of the given string.
def putString(self):
# printing the string
print("The string =", self.string)
# Create an object to represent the class.
stringobj = stringClass()
# Call both methods with the object created above.
# calling getString method which scans the value of string
stringobj.getString()
# calling putString method which prints the value of the string
stringobj.putString()
35. Create an employee database and insert records using sqlite database connectivity
36. Demonstrate database connectivity and display records from a table of ur choice
37. Demonstrate to delete records from a student table using sqlite database connectivity