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

Python Assignment 6

Uploaded by

sirohiriya5191
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)
17 views3 pages

Python Assignment 6

Uploaded by

sirohiriya5191
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

Python Worksheet: Dictionaries and Sets

Instructions
This worksheet contains 15 questions to help you practice the concepts of dictionaries
and sets in Python. Each question is designed to reinforce your understanding of their
properties, such as accessing dictionary values, handling set operations, and recognizing
their characteristics (e.g., sets are unordered and cannot have duplicates). Write your
answers in the space provided or on a separate sheet.

Questions
Question 1: What is a dictionary in Python? Explain its key features and provide an example
of a dictionary with at least three key-value pairs.

Question 2: What is a set in Python? Describe its main characteristics, including why it cannot
contain duplicate elements.

Question 3: Write a Python code snippet to create a dictionary that stores the names and ages
of three people. Print the dictionary.

1 # Write your code here

Question 4: Given the dictionary student = {"name": "Alice", "grade": 85, "subject":
"Math"}, write the Python code to access and print the value associated with the
key "grade".

1 # Write your code here

Question 5: What is the type of the following object: {"a": 1, "b": 2}? Write a Python
code snippet to verify and print its type.

1 # Write your code here

Question 6: What is the type of the following object: {1, 2, 3}? Write a Python code snippet
to verify and print its type.

1
1 # Write your code here

Question 7: Explain why sets in Python cannot have duplicate elements. Create a set with
duplicate values (e.g., {1, 2, 2, 3}) and print it to demonstrate this property.

1 # Write your code here

Question 8: Why is indexing not possible in Python sets? Provide a short explanation and
write a code snippet that attempts to access an element by index to demonstrate
the error.

1 # Write your code here

Question 9: What happens when you try to access a dictionary with a key that does not exist?
Write a code snippet using student = {"name": "Bob", "age": 20} to access
a non-existent key "grade" and handle the error using a try-except block.

1 # Write your code here

Question 10: Write a Python code snippet to create a set mys et = {1, 2, 3}andusetheadd()methodtoaddtheelement

1 # Write your code here

Question 11: Write a Python code snippet to add a new key-value pair "city": "New York"
to the dictionary person = {"name": "Emma", "age": 25}. Print the updated
dictionary.

1 # Write your code here

Question 12: Write a Python code snippet to add the element 5 to the set mys et = {1, 2, 3}usingtheadd()method.P

1 # Write your code here

Question 13: Write a Python code snippet to check if the key "name" exists in the dictionary
info = {"name": "John", "score": 95}. Print an appropriate message based
on the result.

2
1 # Write your code here

Question 14: Write a Python code snippet to remove the element 2 from the set numbers = {1,
2, 3, 4} using the remove() method. Print the updated set.

1 # Write your code here

Question 15: Write a Python code snippet to check if the element 5 exists in the set numbers
= {1, 2, 3, 4}. Use the in operator and print an appropriate message based on
the result.

1 # Write your code here

End of Worksheet. Happy Coding!

You might also like