Sets, Tuples and Ranges Practice Questions (Practice - 3)
Sets, Tuples and Ranges Practice Questions (Practice - 3)
---
Tuples
1. Creating Tuples
Question: Create a tuple named `book` that contains the following information about a book:
title ("Python Programming"), author ("John Doe"), and number of pages (350). Print the tuple.
2. Accessing Elements
Question: Given the tuple `coordinates = (34.0522, -118.2437)`, write code to print only the
latitude.
3. Unpacking Tuples
Question: Unpack the tuple `person = ("Emily", 30, "Engineer")` into three variables: `name`,
`age`, and `profession`. Then, print each variable.
4. Immutable Property
Question: Attempt to change the age in the tuple `person` from 30 to 31. What error do you
expect to receive?
5. Real-World Scenario
Question: You have a list of tuples representing students and their scores: `students =
[("Alice", 85), ("Bob", 90), ("Charlie", 78)]`. Write a loop to print each student's name and score
in the format `"Name: Alice, Score: 85"`.
---
Ranges
5. Real-World Scenario
Question: You have a list of 10 products in a store. Use a range to iterate over the list indices
and print each product with its index.
---
Sets
1. Creating Sets
Question: Create a set named `colors` that contains the following colors: "red", "green", "blue".
Print the set.
2. Ensuring Uniqueness
Question: Create a set from the list `numbers = [1, 2, 2, 3, 4, 4, 5]` to remove duplicates. Print
the set.
6. Modifying Sets
Question: Starting with the set `fruits = {"apple", "banana", "cherry"}`, add "orange" to the set
and remove "banana". Print the updated set.
7. Real-World Scenario
Question: You have two lists of attendees for two different workshops:
---
Tuples
1. Nested Tuples
Question: Create a tuple that contains three tuples, each representing a point's (x, y)
coordinates: (1, 2), (3, 4), and (5, 6). Print the tuple.
2. Tuple Methods
Question: Given the tuple `data = ("apple", "banana", "cherry", "banana")`, find how many
times "banana" appears in the tuple.
---
Ranges
1. Reverse Range
Question: Create a range that starts at 10 and ends at 0 (exclusive) with a step of -2. Convert
it to a list and print it.
---
Sets
1. Symmetric Difference
Question: Given two sets `set_x = {1, 2, 3, 4}` and `set_y = {3, 4, 5, 6}`, find the symmetric
difference and print the result.
---
I encourage you to modify the examples to see different outcomes. For instance, use tuples
within sets or use ranges to generate elements for sets or in implementing small projects like
managing a contact list (tuples for contacts, sets for unique tags), generating reports (using
ranges for iterating), or cleaning data (using sets to remove duplicates).