0% found this document useful (0 votes)
9 views3 pages

Solution Ut-Ii

The document contains a series of questions and programming exercises related to Python for a Class XI Computer Science curriculum. It covers topics such as dictionary methods, tuple unpacking, list slicing, and character counting, along with example code snippets. Each question is designed to test the understanding of Python concepts and programming skills.

Uploaded by

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

Solution Ut-Ii

The document contains a series of questions and programming exercises related to Python for a Class XI Computer Science curriculum. It covers topics such as dictionary methods, tuple unpacking, list slicing, and character counting, along with example code snippets. Each question is designed to test the understanding of Python concepts and programming skills.

Uploaded by

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

UT-II SOLUTION

COMPUTER SCIENCE

CLASS XI

Q1: Multiple Choice Questions [1x10=10]


A: b) pop()
B: c) Updates the dictionary with key-value pairs from another dictionary
C: b) {'a': 10, 'b': 2}
D: b) dict_keys(['a', 'b'])
E: b) An immutable, ordered collection of items
F: b) index()
G: a) (5,)
H: d) Both b and c
I: c) A mutable, unordered collection of key-value pairs
J: b) {1: 'ONE'}

Q2: How is the pop() method different from the del statement in dictionaries?
The pop() method removes an item by its key and returns its value, while the del statement
deletes an item by its key without returning the value. If the key does not exist, pop() raises a
KeyError but can take a default argument to return; del raises an error if the key is not found.

Q3: What is tuple unpacking in Python?


Tuple unpacking is a process of assigning the elements of a tuple to multiple variables in a
single statement.
Example:

t = (1, 2, 3)
a, b, c = t
print(a, b, c) # Output: 1 2 3

Q4: Program to find the largest and smallest elements in a tuple.

my_tuple = (10, 4, 25, 8, 15)


print("Largest:", max(my_tuple))
print("Smallest:", min(my_tuple))

Q5: Output of the code.

my_list = [5, 10, 15, 20, 25, 30, 35]


sliced_list = my_list[1:6:3]
print(sliced_list) # Output: [10, 25]
Q6: Python program to find the common elements between two lists.

# Input two lists using eval


list1 = eval(input("Enter the first list (e.g., [1, 2, 3, 4]): "))
list2 = eval(input("Enter the second list (e.g., [3, 4, 5, 6]): "))

# Find common elements using a loop


common_elements = []
for element in list1:
if element in list2 and element not in common_elements:
common_elements.append(element)

# Display the common elements


print("Common elements:", common_elements)

Q7: Python program to create a new list with even numbers from two input lists.

list1 = eval(input("Enter the first list (e.g., [1, 2, 3, 4]): "))


list2 = eval(input("Enter the second list (e.g., [5, 6, 7, 8]): "))

even_numbers = []

for num in list1:


if num % 2 == 0:
even_numbers.append(num)

for num in list2:


if num % 2 == 0:
even_numbers.append(num)

print("Even numbers:", even_numbers)

Q8: Python program to add a new key-value pair to an existing dictionary.

my_dict = {"a": 1, "b": 2}


my_dict["c"] = 3
print("Updated dictionary:", my_dict)

Q9: Program to count occurrences of each character in a string using a dictionary.

string = input("Enter a string: ")

char_count = {}

for char in string:

if char in char_count:

char_count[char] += 1
else:

char_count[char] = 1

print("Character count:", char_count)

Q10: Answer the following questions:

student_data = {"name": "John", "age": 18, "grade": "A"}

# i) Add a new key-value pair


student_data["city"] = "New York"

# ii) Update the grade


student_data["grade"] = "B"

# iii) Delete the age key


del student_data["age"]

# iv) Check if key "name" exists


print("name" in student_data) # Output: True

# v) Clear the dictionary


student_data.clear()
print(student_data) # Output: {}

Q11: Program to display student names starting with 'M' or 'L'.

student_names = eval(input("Enter the student names as a tuple "))

for i in student_names:

if i[0] == 'M' or first_char == 'L':

print("Filtered names:", filtered_names)

You might also like