Pfp.imp.Practice question
Pfp.imp.Practice question
md 2025-05-19
Accessible only inside the function where Accessible throughout the program,
Scope
it is defined. including inside functions.
Lifetime Exists only during function execution. Exists as long as the program runs.
Cannot modify global variable unless Can be accessed and modified with global
Modification
declared with global. keyword.
Example:
# Global variable
message = "Hello from global!"
def greet():
# Local variable
message = "Hello from local!"
print("Inside function:", message)
greet()
print("Outside function:", message)
2. "It is important to provide positional arguments in correct order." Justify the statement with
example.
Justification: Positional arguments are passed based on their position in the function call. If given in the
wrong order, they may produce incorrect results.
Example:
# Correct order
student_info("Alice", 20)
# Incorrect order
student_info(20, "Alice")
1/5
pfp.imp.practice.3736.md 2025-05-19
Output:
3. "A constructor can be viewed as a specific method used by a class to perform tasks that need to be
done when an object of a class is created." Justify the statement with example.
Justification: A constructor (__init__) is automatically called when an object is created. It is used to initialize
attributes and set up the initial state of the object.
Example:
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
print("Constructor called!")
def display(self):
print("Name:", self.name)
print("Roll:", self.roll)
s1 = Student("Alice", 101)
s1.display()
Output:
Constructor called!
Name: Alice
Roll: 101
4. Illustrate any 3 operations that can be performed on String with the help of suitable program.
1. Concatenation:
str1 = "Hello"
str2 = "World"
print(str1 + " " + str2)
2. Slicing:
2/5
pfp.imp.practice.3736.md 2025-05-19
text = "Programming"
print(text[0:6]) # Output: Progra
3. String Methods:
5. What is the use of Matplotlib library? Also explain various key plots that are used for data
visualization with the help of code snippet.
Use: Matplotlib is used for creating static, interactive, and animated visualizations in Python. It is commonly
used in data analysis and scientific research.
Key Plots:
1. Line Plot
2. Bar Plot
3. Histogram
4. Scatter Plot
3/5
pfp.imp.practice.3736.md 2025-05-19
x = [1, 2, 3, 4]
y = [10, 15, 13, 17]
plt.scatter(x, y)
plt.title("Scatter Plot")
plt.show()
Class: StudentRecordSystem
class StudentRecordSystem:
def __init__(self):
self.students = {}
def add_student(self):
roll = int(input("Enter Roll Number: "))
if roll in self.students:
print("Student with this roll number already exists.")
return
name = input("Enter Name: ")
course = input("Enter Course: ")
marks = float(input("Enter Marks: "))
self.students[roll] = {'Name': name, 'Course': course, 'Marks': marks}
print("Student record added successfully!")
def display_students(self):
if not self.students:
print("No student records available")
else:
for roll, details in self.students.items():
print(f"Roll: {roll}, Name: {details['Name']}, Course:
{details['Course']}, Marks: {details['Marks']}")
def search_student(self):
roll = int(input("Enter Roll Number to search: "))
if roll in self.students:
details = self.students[roll]
print(f"Found: Roll: {roll}, Name: {details['Name']}, Course:
{details['Course']}, Marks: {details['Marks']}")
else:
print("Student not found")
def update_marks(self):
roll = int(input("Enter Roll Number to update marks: "))
if roll in self.students:
new_marks = float(input("Enter new Marks: "))
self.students[roll]['Marks'] = new_marks
print("Marks updated successfully!")
else:
4/5
pfp.imp.practice.3736.md 2025-05-19
def delete_student(self):
roll = int(input("Enter Roll Number to delete: "))
if roll in self.students:
del self.students[roll]
print("Student record deleted successfully!")
else:
print("Student not found")
# Menu-driven program
system = StudentRecordSystem()
while True:
print("\n--- Student Record Management ---")
print("1. Add Student")
print("2. Display All Students")
print("3. Search Student by Roll Number")
print("4. Update Student Marks")
print("5. Delete Student Record")
print("6. Exit")
if choice == '1':
system.add_student()
elif choice == '2':
system.display_students()
elif choice == '3':
system.search_student()
elif choice == '4':
system.update_marks()
elif choice == '5':
system.delete_student()
elif choice == '6':
print("Exiting program. Goodbye!")
break
else:
print("Invalid choice. Please try again.")
5/5