#programming #programming/python #semester-1
Tuples, similar to lists, are used to store multiple items in a single variable. They are ordered, unchangeable, and allow duplicate values.
A tuple can be declared using regular brackets: ()
a_tuple = ("apple", "bannana", "cherry")To declare a tuple with one item, use a comma after the item:
a_tuple = ("apple",) # Notice the CommaTuples can have multiple data types in them:
a_tuple = ("apples", 1, True, 3.0)Tuple items can be accessed with the following syntax: tuple[i]
a_tuple = ["apple", "bannana", "cherry"]
x = a_tuple[0] # "apple"
y = a_tuple[1] # "bannana"
z = a_tuple[2] # "cherry"Note how the index starts at 0, not 1
We can also use a negative index to access items from the end of the tuple, with -1 being the last item, -2 being the second last, etc.
a_tuple = ["apples", 1, True]
print(a_tuple[-1]) # True
print(a_tuple[-2]) # 1We can specify a range of indexes:
a_tuple = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(a_tuple[2:5])
# from index 2 (inclusive) to index 5 (exclusive) - > [3, 4, 5]
print(a_tuple[:5])
# from the start to index 5 (exclusive) -> [1, 2, 3, 4, 5]
print(a_tuple[5:])
# from index 5 (inclusive) to the end of the list -> [6, 7, 8, 9, 10]
print(a_tuple[-4:-1])
# from 4th last item (inclusive) to the last item (exclusive) -> [7, 8, 9]Tuples are immutable, meaning they cannot be changed.
If you want to change the items in a tuple, you can change it into a list, then back into a tuple
a_tuple = ("apple", "bannana", "cherry")
b_list = list(a_tuple)
b_list[1] = "pear"
a_tuple = tuple(b_list)
print(a_tuple) # ["apple", "pear", "cherry"]You can add tuples to tuples
a_tuple = ("apple", "banana", "cherry")
b_tuple = ("pear")
a_tuple += b_tuple
print(a_tuple) # ("apple", "banana, "cherry", "pear")You cannot remove items from a tuple, unless it is converted to a list first
When you create a tuple, you normally assign values to it. This is called packing a tuple
We can then unpack the items back into variables:
a_tuple = ("apple", "banana", "cherry")
(a, b, c) = a_tuple
print(a) # "apple"
print(b) # "banana"
print(c) # "cherry"We can also use an asterisk * in order to unpack the remainder of a tuple into a variable as a list
a_tuple = ("apple", "banana", "cherry", "pear", "blueberry")
(a, b, c*) = a_tuple
print(a) # "apple"
print(b) # "banana"
print(c) # ["cherry", "pear", "blueberry"]You can loop through the items in a tuple by using a for loop:
a_tuple = ("apple", "banana", "cherry")
for x in a_tuple:
print(x)
# "apple"
# "banana"
# "cherry"We can loop through the index of the list too:
a_tuple= ("apple", "banana", "cherry")
for i in range(len(a_tuple)):
print(a_tuple[i])You can use the in keyword to check if an item is in a list:
a_tuple= ("apple", "orange", "cherry")
if "apple" in a_tuple:
print("blah blah blah")We can join two tuples:
a_tuple = ("apple", "orange", "cherry")
b_tuple = ("pear", "blueberry", "strawberry")
c_tuple = a_tuple + b_tuple
print(c_tuple) # ("apple", "orange", "cherry", "pear", "blueberry", "strawberry")We can also multiply the contents of a tuple:
a_tuple = ("apple", "orange", "cherry")
b_tuple = a_tuple \* 2
print(b_tuple) # ("apple", "orange", "cherry", "apple", "orange", "cherry")| Method | Description |
|---|---|
| count() | Returns the number of times a specified value occurs in a tuple |
| index() | Searches the tuple for a specified value and returns the position of where it was found |