[go: up one dir, main page]

0% found this document useful (0 votes)
24 views5 pages

Pfp.imp.Practice question

The document provides important questions and answers related to Python programming, covering topics such as local and global variables, positional arguments, constructors, string operations, and the Matplotlib library for data visualization. It also includes a detailed implementation of a Student Record Management System with functionalities to add, display, search, update, and delete student records. Each section includes examples and code snippets to illustrate the concepts discussed.

Uploaded by

satvirnatt19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

Pfp.imp.Practice question

The document provides important questions and answers related to Python programming, covering topics such as local and global variables, positional arguments, constructors, string operations, and the Matplotlib library for data visualization. It also includes a detailed implementation of a Student Record Management System with functionalities to add, display, search, update, and delete student records. Each section includes examples and code snippets to illustrate the concepts discussed.

Uploaded by

satvirnatt19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

pfp.imp.practice.3736.

md 2025-05-19

Python Programming – Important Questions and Answers

1. Differentiate between Local and Global variables with example.

Aspect Local Variable Global Variable

Accessible only inside the function where Accessible throughout the program,
Scope
it is defined. including inside functions.

Declaration Declared inside a function. Declared outside all 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:

def student_info(name, age):


print("Name:", name)
print("Age:", age)

# Correct order
student_info("Alice", 20)

# Incorrect order
student_info(20, "Alice")

1/5
pfp.imp.practice.3736.md 2025-05-19

Output:

Correct: Name: Alice, Age: 20


Incorrect: Name: 20, Age: Alice

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:

sentence = "Python Is Fun"


print(sentence.lower())
print(sentence.replace("Fun", "Powerful"))

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

import matplotlib.pyplot as plt


x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.title("Line Plot")
plt.show()

2. Bar Plot

categories = ['A', 'B', 'C']


values = [4, 7, 1]
plt.bar(categories, values)
plt.title("Bar Plot")
plt.show()

3. Histogram

data = [22, 87, 5, 43, 56, 73, 55, 54]


plt.hist(data, bins=5)
plt.title("Histogram")
plt.show()

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()

6. Student Record Management System (8 Marks)

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

print("Student not found")

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")

choice = input("Enter your choice (1-6): ")

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

You might also like