Python Tuples
- Previous Page Python Lists
- Next Page Python Sets
Tuple (Tuple)
Tuples are ordered and immutable collections. In Python, tuples are written with parentheses.
Instance
Create a tuple:
thistuple = ("apple", "banana", "cherry") print(thistuple)
Access tuple items
You can access tuple items by referencing the index number within the square brackets.
Instance
Print the second item in the tuple:
thistuple = ("apple", "banana", "cherry") print(thistuple[1])
Negative index
Negative indices represent starting from the end, -1 represents the last item, -2 represents the second-to-last item, and so on.
Instance
Print the last item of the tuple:
thistuple = ("apple", "banana", "cherry") print(thistuple[-1])
Index range
You can specify the index range by specifying the start and end points of the range.
After specifying the range, the returned value will be a new tuple with the specified item.
Instance
Return the third, fourth, and fifth items:
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango") print(thistuple[2:5])
Note:The search will start from index 2 (inclusive) and end at index 5 (exclusive).
Remember, the index of the first item is 0.
Negative index range
If you want to start searching from the end of the tuple, specify a negative index:
Instance
This example will return items from index -4 (inclusive) to index -1 (exclusive):
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango") print(thistuple[-4:-1])
Change tuple value
After creating a tuple, you will not be able to change its value. Tuples are immutable, or also known as constant.
But there is a solution. You can convert the tuple to a list, change the list, and then convert the list back to a tuple.
Instance
You can change a tuple by converting it to a list:
x = ("apple", "banana", "cherry") y = list(x) y[1] = "kiwi" x = tuple(y) print(x)
Traverse tuple
You can use for
Loop through tuple items.
Instance
Traverse items and print values:
thistuple = ("apple", "banana", "cherry") for x in thistuple: print(x)
You will learn Python For Loop In this chapter, you will learn about for
More about loops.
Check if item exists
To determine if a specified item exists in the tuple, use in
Keyword:
Instance
Check if "apple" exists in the tuple:
thistuple = ("apple", "banana", "cherry") if "apple" in thistuple: print("Yes, 'apple' is in the fruits tuple")
Tuple length
To determine how many items a tuple has, use len()
Method:
Instance
Print the number of items in the tuple:
thistuple = ("apple", "banana", "cherry") print(len(thistuple))
Add item
Once a tuple is created, you cannot add items to it. Tuples are immutable.
Instance
You cannot add items to a tuple:
thistuple = ("apple", "banana", "cherry") thistuple[3] = "orange" # Will cause an error print(thistuple)
Create a tuple with one item
To create a tuple that contains only one item, you must add a comma after the item, otherwise Python cannot recognize the variable as a tuple.
Instance
Single-item tuple, don't forget the comma:
thistuple = ("apple",) print(type(thistuple)) # Not a tuple thistuple = ("apple") print(type(thistuple))
Delete item
Note:You cannot delete items from a tuple.
Tuples are immutable, so you cannot remove items from them, but you can completely delete the tuple:
Instance
The del keyword can completely delete a tuple:
thistuple = ("apple", "banana", "cherry") del thistuple print(thistuple) # This will cause an error because the tuple does not exist.
Merge two tuples
To connect two or more tuples, you can use the + operator:
Instance
Merge this tuple:
tuple1 = ("a", "b", "c") tuple2 = (1, 2, 3) tuple3 = tuple1 + tuple2 print(tuple3)
tuple() Constructor
You can also use tuple()
Constructor to create a tuple.
Instance
Use tuple()
Methods to create a tuple:
thistuple = tuple(("apple", "banana", "cherry")) # Note the parentheses print(thistuple)
Tuple Methods
Python provides two built-in methods that can be used on tuples.
Method | Description |
---|---|
count() | Return the number of times a specified value appears in the tuple. |
index() | Search for a specified value in the tuple and return the position where it is found. |
- Previous Page Python Lists
- Next Page Python Sets