Overview
Project details
A project to explore and manipulate tuples in Python.
In this project, students will create a program that allows users to input a list of items, which will be stored as a tuple. The program will then provide various functionalities such as displaying the tuple, accessing specific elements, slicing the tuple, and finding the length of the tuple. Additionally, students will implement error handling to manage invalid inputs and demonstrate the immutability of tuples by attempting to modify them and catching exceptions. This project will help students understand the properties and uses of tuples in Python programming.
Tracks: Python
40
number of points you will earn
2
launches to complete the project
4
tasks to complete the project
This project is part of the following tracks:
Learning outcomes
-
Understand the concept of tuples and their properties in Python.
-
Learn how to create and manipulate tuples.
-
Gain experience in handling user input and implementing error handling.
-
Develop skills in accessing and slicing tuples.
-
Recognize the immutability of tuples and its implications in programming.
Project Launches & Tasks
launch_1
-
task_1 (10 points)
-
Create a new Python file named
tuple_explorer.py
. -
Define a function
get_items()
that prompts the user to input a list of items separated by commas. -
Convert the input string into a list and then into a tuple.
-
Return the tuple from the function.
-
Do not forget to implement unit tests, use pytest for tests
-
-
task_2 (10 points)
-
In the same
tuple_explorer.py
file, define a functiondisplay_tuple(t)
that takes a tuple as an argument. -
Print the tuple to the console.
-
Call this function after creating the tuple in
get_items()
to display the user's input. -
Do not forget to implement unit tests, use pytest for tests
-
launch_2
-
task_3 (10 points)
-
In the
tuple_explorer.py
file, define a functionaccess_element(t, index)
that takes a tuple and an index as arguments. -
Use a try-except block to access the element at the given index.
-
If the index is valid, return the element; if not, return an error message indicating the index is out of range.
-
Do not forget to implement unit tests, use pytest for tests
-
-
task_4 (10 points)
-
In the
tuple_explorer.py
file, define a functionslice_tuple(t, start, end)
that takes a tuple and two indices as arguments. -
Return the sliced portion of the tuple from the start index to the end index.
-
Ensure to handle cases where the indices are out of range by returning an appropriate message.
-
Do not forget to implement unit tests, use pytest for tests
-