0% found this document useful (0 votes)
10 views

Unit II Question Bank

Uploaded by

Lakshmi M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Unit II Question Bank

Uploaded by

Lakshmi M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Department of Artificial Intelligence and Data Science

Course Code and Name : U23AD491 / Python for AI


Year/Semester : I / II Semester

Unit-II: Python Data Structures (Sets and Dictionaries)


PART A
BT Cognizance
S.NO QUESTIONS Level level
1. Compare Symmetric difference and Symmetric difference update. K2 Understand

Ans:
In Python, you can obtain the symmetric difference of two sets using
the caret (^) operator or the symmetric_difference() method.
For example:
A = {1, 2, 3}
B = {3, 4, 5}
sym_diff = A.symmetric_difference(B)
print(sym_diff) # Output: {1, 2, 4, 5}

The symmetric_difference_update() method in Python is used to


update a set with the symmetric difference of itself and another set. It
modifies the set in place.
For example:
A = {1, 2, 3}
B = {3, 4, 5}
A.symmetric_difference_update(B)
print(A) # Output: {1, 2, 4, 5}
2. What does the intersection() method in Python sets do? How do you K2 Understand

SECE/COE/QB/Rev. 01/04.03.2022
remove all elements from a set?
The intersection() method in Python sets returns a new set containing
only the elements that are common to both sets.
To remove all elements from a set, you can use the clear() method.
This method removes all elements from the set, leaving it empty.
3. Develop a Python program to remove an item from a set if it is present K3 Apply
in the set.
discard() method of the set is used to remove an item if it's present.
e.g Example:
my_set = {1, 2, 3, 4, 5}
item_to_remove = 6
my_set.discard(item_to_remove)
print("Set after removal:", my_set)
Since the item 6 is not present in the set my_set, using discard() won't
raise any errors. It simply does nothing.
4. Write Python code to count the frequency of each character in a given K3 Apply
string and store the counts in a dictionary.
Ans:
input_str = "hello world"
char_freq = {}
for char in input_str:
if char in char_freq:
char_freq[char] += 1
else:
char_freq[char] = 1
for char, freq in char_freq.items():
print(f"Character '{char}' occurs {freq} times.")
5. Develop a Python program to multiply all the items in a dictionary. K3 Apply
my_dict = {'a': 2, 'b': 3, 'c': 4}
result = 1
for value in my_dict.values():
result *= value
print(result)
6. Explain the difference between the get() method and accessing a K2 Understand

SECE/COE/QB/Rev. 01/04.03.2022
dictionary key directly in Python.

A: In Python, accessing a dictionary key directly (my_dict[key]) raises


a KeyError if the key is not found in the dictionary, whereas the get()
method (my_dict.get(key)) returns None if the key is not found.
Additionally, the get() method allows specifying a default value to
return if the key is not found, providing more flexibility in handling
missing keys.
7. How can you efficiently check if a key exists in a dictionary in Python K2 Understand
without raising an error?

A: In Python, you can efficiently check if a key exists in a dictionary


using the in operator. For example, if key in my_dict: will return True
if the key exists in the dictionary my_dict and False otherwise.
Alternatively, you can use the get() method which returns None if the
key is not found, allowing you to handle missing keys without raising
an error.
8. How can you remove a key-value pair from a dictionary in Python, and K2 Understand
what happens if you try to remove a key that doesn't exist?

A: You can remove a key-value pair from a dictionary in Python using


the pop() method, specifying the key to remove. If you try to remove a
key that doesn't exist in the dictionary, the pop() method will raise a
KeyError unless you provide a default value as the second argument,
in which case it returns the default value instead of raising an error.
9. Write a Python function that takes two dictionaries as input and returns K3 Apply
a new dictionary containing only the key-value pairs that are common
to both dictionaries.
def common_key_value_pairs(dict1, dict2):
common_pairs = {}
for key in dict1:
if key in dict2 and dict1[key] == dict2[key]:
common_pairs[key] = dict1[key]
return common_pairs

# Example dictionaries
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 2, 'c': 4, 'd': 5}

# Call the function


result = common_key_value_pairs(dict1, dict2)
print("Common key-value pairs:", result)
10. Write a Python function to reverse a dictionary, where the keys become K3 Apply
the values and the values become the keys.
def reverse_dict(dictionary):
return {value: key for key, value in dictionary.items()}

SECE/COE/QB/Rev. 01/04.03.2022
# Example dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}

# Call the function


reversed_dict = reverse_dict(my_dict)
print("Reversed dictionary:", reversed_dict)
PART B
1. Consider a scenario where you are tasked with creating a program to K4 Analyze
manage a library's collection of books. Design a Python program that
utilizes Dictionaries to implement the following functionalities:
• Add a new book to the library's collection.
• Remove a book from the collection based on its title.
• Search for a book in the collection based on its title.
• Display the entire collection of books, including their titles and
corresponding authors.
Your program should provide a menu-driven interface that allows the
user to perform these actions. Ensure that appropriate error handling is
implemented for invalid inputs.
Sample Output:
Welcome to the Library Management System!
1. Add a new book
2. Remove a book
3. Search for a book
4. Display all books
5. Exit
Enter your choice: 1
Enter the title of the book: Harry Potter and the Philosopher's Stone
Enter the author of the book: J.K. Rowling
Book added successfully!

Enter your choice: 1


Enter the title of the book: To Kill a Mockingbird

SECE/COE/QB/Rev. 01/04.03.2022
Enter the author of the book: Harper Lee
Book added successfully!

Enter your choice: 4


Library Collection:
1. Title: Harry Potter and the Philosopher's Stone - Author: J.K.
Rowling
2. Title: To Kill a Mockingbird - Author: Harper Lee

Enter your choice: 2


Enter the title of the book to remove: To Kill a Mockingbird
Book removed successfully!

Enter your choice: 3


Enter the title of the book to search: Harry Potter and the Philosopher's
Stone
Book found in the collection!
Title: Harry Potter and the Philosopher's Stone - Author: J.K. Rowling

Enter your choice: 5


Thank you for using the Library Management System!
2. Identify the difference between remove(),discard(),pop() with suitable K2 Understand

example
3. Construct a dictionary to perform the following operations: K3 Apply
a. Accept and print at least five student’s names and marks
b. Display the student mark by taking the student name
as input. If the student’s name is not available print
it as “Student Not Found”.
c. Find the number of students who have scored more
Than 60 marks.
d. Print the student name who has secured the lowest

SECE/COE/QB/Rev. 01/04.03.2022
Mark.
4. A: {1, 2, 3, 4, 5} K3 Apply
B: {3, 6, 2, 7, 5}

Write a Python program to accomplish the following tasks:


i) Identify numbers common to both A and B.
ii) Determine the numbers exclusively belonging to either A or B, but
not to both.
iii) Determine if A is a subset of B. Conversely, ascertain if B is a
superset of A, and identify any numbers present only in A.
iv) Check if there are any numbers common between A and B.
v) List the numbers present in A but not in B.
vi) Implement a program to remove the element 2 from A.
5. You are tasked with developing a Python program to manage a student K4 Analyze
database for a university. Design a program that utilizes Sets to
implement the following functionalities:
• Add a new student to the database, including their name,
student ID, and major.
• Remove a student from the database based on their student ID.
• Search for a student in the database based on their student ID.
• Display the entire database, showing the name, student ID, and
major of each student.
• Your program should provide a menu-driven interface for the
user to perform these actions. Ensure that appropriate error handling is
implemented for invalid inputs.
Sample Output:
Welcome to the Student Database Management System!
1. Add a new student
2. Remove a student
3. Search for a student
4. Display database
5. Exit

Enter your choice: 1


Enter the name of the student: Alice
Enter the student ID: 12345

SECE/COE/QB/Rev. 01/04.03.2022
Enter the major of the student: Computer Science
Student added successfully!

Enter your choice: 1


Enter the name of the student: Bob
Enter the student ID: 67890
Enter the major of the student: Mathematics
Student added successfully!

Enter your choice: 4


Student Database:
Name: Alice, Student ID: 12345, Major: Computer Science
Name: Bob, Student ID: 67890, Major: Mathematics

Enter your choice: 2


Enter the student ID to remove: 12345
Student removed successfully!

Enter your choice: 3


Enter the student ID to search: 67890
Student found in the database!
Name: Bob, Student ID: 67890, Major: Mathematics

Enter your choice: 5


a) Thank you for using the Student Database Management
System!

Faculty Incharge HoD

SECE/COE/QB/Rev. 01/04.03.2022

You might also like