Python
Python
Q) Explain any three widgets in tkinter in brief in python. Ans :- 1) Button The Button
widget is used to display buttons in your application. You can use buttons to perform
actions, such as opening a new file, saving a file, or closing an application. 2) Label The
Label widget is used to display text in your application. You can use labels to display
information, such as the name of your application, the current date and time, or the
instructions for using your application. 3) Entry The Entry widget is used to display a
single-line text field in your application. You can use Entry widgets to allow users to
enter text, such as their name, email address, or password.
Q) what is inheritance write its benefits and syntax? Ans :- 1) Inheritance allows us to
define a class that inherits all the methods and properties from another class. 2) Parent
class is the class being inherited from, also called base class. 3) Child class is the class
that inherits from another class, also called derived class. 4) Inheritance provides code
reusability, readability, and transition of properties which helps in optimized and
efficient code building.. Benefits :- 1. Less code repetition, as the code which is
common can be placed in the parent class, hence making it available to all the child
classes. 2. Structured Code: By dividing the code into classes, we can structure our
software better by dividing functionality into classes. 3. Make the code more scalable.
Syntax :- class BaseClass : Body of base class class Derived Class (BaseClass) : Body of
derived class.
Q)Write a Python program to check whether a number is in a given range.
def is_number_in_range(number, lower_limit, upper_limit):
return lower_limit <= number <= upper_limit
lower_limit = 10
upper_limit = 50
user_input = float(input("Enter a number to check if it's in the range: "))
if is_number_in_range(user_input, lower_limit, upper_limit):
print(f"{user_input} is in the range between {lower_limit} and {upper_limit}.")
else:
print(f"{user_input} is outside the range between {lower_limit} and {upper_limit}.")
Q) Write a Python program to display current date and time.
import datetime
current_datetime = datetime.datetime.now()
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
print("Current Date and Time:", formatted_datetime)
Q) Write a python class to accept a string and number 'n' from user and display 'n'
repetition of strings by overloading *operator
class StringRepeater:
def _init_(self, input_string):
self.input_string = input_string
def repeat(self, n):
return self.input_string * n
user_string = input("Enter a string: ")
user_n = int(input("Enter the number of repetitions (n): "))
string_repeater = StringRepeater(user_string)
result = string_repeater.repeat(user_n)
print(f"{user_string} repeated {user_n} times: {result}")
Q)Write python GUI program to generate a random password with upper and
lower case letters
import random
import string
def generate_password(length):
characters = string.ascii_
letters password = ''.join(random.choice(characters) for _ in range(length))
return password
length = int(input("Enter the desired length of the password: "))
password = generate_password(length)
print("Generated password:", password)
Q)Write a python program to accept string and remove the characters which have
odd index values of given string using user defined function. def
remove_odd_characters(string):
new_string = ""
for index in range(len(string)):
if index % 2 == 0:
new_string += string[index]
return new_string
input_string = input("Enter a string: ")
result = remove_odd_characters(input_string)
print("String with odd-indexed characters removed:", result)
Q)Write a python program to create a class circle and compute the Area and the
circumferences of the circle. (Use parametrized constructor)
import math
class Circle:
def __init__(self, radius):
self.radius = radius
def calculate_area(self):
return math.pi * self.radius ** 2
calculate_circumference(self):
return 2 * math.pi * self.radius
radius = float(input("Enter the radius of the circle: "))
circle = Circle(radius)
print("Area of the circle:", circle.calculate_area())
print("Circumference of the circle:", circle.calculate_circumference())