Python
Q1. Attempt any six of the following:
a. List out Geometry Management methods.
In Python's Tkinter module, geometry management methods control how widgets are arranged
on the screen. The main geometry management methods are:
pack(): Organizes widgets in blocks before placing them in the parent widget.
grid(): Places widgets in a 2D grid.
place(): Places widgets at an absolute position within the parent widget.
b. How do Break and Pass statements work in Python?
break: Terminates the current loop prematurely and transfers control to the next statement after
the loop.
pass: A null statement, it is used as a placeholder when a statement is required syntactically but
no code needs to be executed.
c. Write the syntax of the Raise statement & explain it.
raise [ExceptionType] ([optional message])
The raise statement is used to trigger an exception. You can either raise a specific built-in
exception or a custom exception. Example:
raise ValueError("This is an error message")
This raises a ValueError with the specified message.
d. Differentiate between Python List and NumPy array.
List: A built-in Python data structure that can hold elements of any type. Lists are flexible but
slower for numerical operations.
NumPy Array: A fixed-size, homogeneous array provided by the NumPy library. It is optimized
for numerical computations and faster for handling large datasets.
e. What is the syntax of the constructor in Python?
In Python, the constructor is defined by the __init__() method:
class MyClass:
def __init__(self, parameters):
[Link] = parameters
This method is automatically called when an object is created.
f. What are the advantages of Pandas?
Fast and efficient for data manipulation.
Provides data alignment and integrated handling of missing data.
Easy handling of data with labels, and it can integrate with various file formats like CSV, Excel,
and SQL.
Powerful group-by functionality for summarizing and aggregating data.
g. What is the use of random() in the random module?
The random() function returns a random floating point number between 0.0 and 1.0. Example:
import random
print([Link]())
h. List out any five built-in options in Python.
Five built-in functions in Python:
1. print(): Outputs data to the screen.
2. len(): Returns the length of an object.
3. type(): Returns the type of an object.
4. max(): Returns the largest item in an iterable.
5. min(): Returns the smallest item in an iterable.
---
Q2. Attempt any two of the following:
a. 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 __init__(self, roll_no, name, age, gender):
self.roll_no = roll_no
[Link] = name
[Link] = age
[Link] = gender
class Test(Student):
def __init__(self, roll_no, name, age, gender, marks):
super().__init__(roll_no, name, age, gender)
[Link] = marks
def total_marks(self):
return sum([Link])
def display(self):
print(f"Roll No: {self.roll_no}, Name: {[Link]}, Age: {[Link]}, Gender: {[Link]}")
print(f"Marks: {[Link]}, Total Marks: {self.total_marks()}")
# Creating objects
student1 = Test(1, "Alice", 20, "Female", [80, 85, 90])
student2 = Test(2, "Bob", 21, "Male", [75, 80, 78])
student3 = Test(3, "Charlie", 22, "Male", [88, 92, 81])
# Display details
[Link]()
[Link]()
[Link]()
b. Write a python GUI to create a digital clock with tkinter to display the time.
import tkinter as tk
from time import strftime
# Create window
root = [Link]()
[Link]("Digital Clock")
# Function to update the time
def time():
string = strftime('%H:%M:%S %p')
[Link](text=string)
[Link](1000, time)
# Styling the label widget
label = [Link](root, font=('calibri', 40, 'bold'), background='purple', foreground='white')
[Link](anchor='center')
time() # Call the time function
[Link]()
c. What are lists and tuples? What is the key difference between the two?
List: A mutable, ordered collection of elements that can contain elements of different data types.
It allows changes like adding, removing, or modifying elements.
Tuple: An immutable, ordered collection of elements. Once a tuple is created, its elements
cannot be changed.
Key Difference: Lists are mutable, while tuples are immutable.
d. Write a python program to accept a string and remove the characters which have odd index
values of a given string using a user-defined function.
def remove_odd_index_chars(input_string):
result = ""
for index in range(len(input_string)):
if index % 2 == 0: # Checking for even index
result += input_string[index]
return result
# Input string from user
user_input = input("Enter a string: ")
print("String after removing characters at odd indices:", remove_odd_index_chars(user_input))