CONTENT
1. Introduction
2. Creating Tuple
3. Tuple Methods
4. Tuple Functions
Introduction
Tuples are simple yet powerful data structures in Python, and their immutability makes them ideal for specific use cases where data integrity and performance are important.
Creating Tuple
Creating lists in Python is a straightforward process. The most common way is to enclose a comma-separated sequence of items within square brackets [ ]. This allows you to directly initialize a list with specific values, whether they are numbers, strings, or a mix of data types.
For instance, my_list = [1, "hello", 3.14] creates a list containing an integer, a string, and a float.
Alternatively, you can create an empty list using my_list = [ ] and then add elements later using methods like append() or insert().
Another powerful technique is to use the list() constructor, which can convert other iterable data types, such as tuples or strings, into lists.
Tuple Indexing
Tuple Methods
In Python, tuples are immutable sequences, meaning once they are created, their contents cannot be changed. Because of this immutability, tuples have fewer built-in methods compared to mutable data types like lists.
If you need to modify a tuple, you can convert it to a list, make changes, and then convert it back to a tuple.
1. count(item): Returns the number of times a specified value appears in the tuple.
Syntax: tuple.count(value)
value: The value to search for.
Output:
3
2. index(value, start, end): Returns the index of the first occurrence of a specified value in the tuple.
Output:
1
Output:
3
Tuple Functions
1. len(tuple): Returns the number of items in the tuple.
Code </>:
# returns the length of my_list
Output:
2. min(tuple): Returns the smallest item in the tuple.
Code </>:
# returns the smallest element in my_list
Output:
3. max(tuple): Returns the largest item in the tuple.
Code </>:
# returns the largest element in my_list
Output:
4. sum(tuple): Returns the sum of all items in the tuple (only for numeric tuple).
Code </>:
# returns the sum of all elements in my_list
Output:
5. sorted(tuple, key=None, reverse= False): Returns a new sorted tuple from the elements of the tuple. The original tuple remains unchanged.
Code </>:
# returns a new list sorted in descending order
Output:
6. any(tuple): Returns True if at least one element in the tuple is True (or truthy).
Code </>:
# returns True if any element in my_list is True
Output:
7. all(tuple): Returns True if all elements in the list are True (or truthy).
Code </>:
# returns True if all elements in my_list are True
Output:
8. list(iterable): Converts an iterable (e.g., tuple, string, set) into a list.
Code </>:
# converts the string 'hello' into ['h', 'e', 'l', 'l', 'o']
Output:
Examples
Key Differences Between Tuples and Lists
1. Mutability
- Tuples are immutable, meaning you cannot modify their contents after creation.
- Lists are mutable, so you can add, remove, or change elements.
Performance
Tuples are generally faster than lists because of their immutability.
Use Cases
- Use tuples for fixed collections of items (e.g., coordinates, database records).
- Use lists for dynamic collections that may change over time.
- When you want to ensure the data cannot be modified (e.g., constants, keys in dictionaries).
- When performance is critical, and you don't need to modify the data.
- When working with heterogeneous data (e.g., storing different types of data together).
