Unit II Question Bank
Unit II Question Bank
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}
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.
# Example dictionaries
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 2, 'c': 4, 'd': 5}
SECE/COE/QB/Rev. 01/04.03.2022
# Example dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
SECE/COE/QB/Rev. 01/04.03.2022
Enter the author of the book: Harper Lee
Book added successfully!
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}
SECE/COE/QB/Rev. 01/04.03.2022
Enter the major of the student: Computer Science
Student added successfully!
SECE/COE/QB/Rev. 01/04.03.2022