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

Sets, Tuples and Ranges Practice Questions (Practice - 3)

Uploaded by

hadiyahaya87
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)
8 views

Sets, Tuples and Ranges Practice Questions (Practice - 3)

Uploaded by

hadiyahaya87
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
You are on page 1/ 4

Practice Questions

---

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

1. Basic Range Creation


Question: Create a range that starts at 0 and ends at 10 (exclusive). Convert it to a list and
print it.

2. Range with Start and Stop


Question: Create a range that starts at 5 and ends at 15 (exclusive). Convert it to a list and
print it.

3. Range with Step


Question: Create a range that starts at 2, ends at 20 (exclusive), with a step of 3. Convert it to
a list and print it.

4. Using Range in Loops


Question: Write a `for` loop that prints the squares of numbers from 1 to 5 using `range()`.

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.

products = ["Laptop", "Smartphone", "Tablet", "Headphones", "Camera", "Smartwatch",


"Printer", "Monitor", "Keyboard", "Mouse"]

---

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.

3. Set Operations: Union


Question: Given two sets `set_a = {1, 2, 3}` and `set_b = {3, 4, 5}`, perform a union operation
and print the result.

4. Set Operations: Intersection


Question: Using the same sets `set_a` and `set_b`, perform an intersection operation and
print the result.

5. Set Operations: Difference


Question: Find the difference between `set_a` and `set_b` (elements in `set_a` but not in
`set_b`) and print the result.

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:

workshop1 = ["Alice", "Bob", "Charlie", "David"]

workshop2 = ["Charlie", "David", "Eve", "Frank"]


Convert these lists to sets and find out:
- Who attended both workshops?
- Who attended only the first workshop?
- Who attended only the second workshop?

---

Bonus Challenge Questions

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.

2. List Comprehension with Range


Question: Use a list comprehension with `range()` to create a list of squares for numbers from
1 to 10. Print the list.

---

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.

2. Subset and Superset


Question: Given two sets `set_a = {1, 2}` and `set_b = {1, 2, 3, 4}`, determine if `set_a` is a
subset of `set_b` and if `set_b` is a superset of `set_a`. Print the results as `True` or `False`.

---
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).

Resources for Further Learning:

- Python Official Documentation on Tuples:


https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences
- Python Official Documentation on Ranges:
https://docs.python.org/3/library/stdtypes.html#ranges
- Python Official Documentation on Sets:
https://docs.python.org/3/tutorial/datastructures.html#sets

You might also like