Py09: Tuple
A sequence of data stored as a single unit. A tuple is generally faster than a list, and immutable.
Introduction
A Tuple is a list and has almost the same function to retrieve its data. Tuple cannot add a new element or remove elements from it. A tuple is defined using parentheses ().
tuple_name = () tuple_name = (item1, item2, ...., itemN)
the parentheses can be omitted, but it's encouraged to use them. Its item can be an integer, float, string, or even a list. a tuple can consist of different data type items.
a_tuple = (1, 3.5, "James")
print(a_tuple) # prints the items in tuple
Output
(1, 3.5, 'James') >
Writing code the following way does not create a tuple.
a_tuple = ("James")
print(type(a_tuple)) # returns the data type
print(a_tuple) # prints the data
Output
<class 'str'> James >
to create a tuple that only has one item, a comma is needed after the first item and optionally the parentheses can be omitted.
a_tuple = ("James",)
print(type(a_tuple)) # returns the data type
print(a_tuple) # prints the items in tuple
Output
<class 'tuple'> ('James',) >
A tuple can be deleted by using the del
operation as shown in the following syntax:
del tuple_name
Accessing Tuple
A tuple element is accessed using the index number specified in the square bracket as shown below:
tupple_name[index]
For example, to get the number 7 in the tuple, use index value 2.
a_tuple = (4, 5, 7, 3)
print(a_tuple[2]) # 7 is 3rd item, so index 2
Output
7 >
using a negative index, -1 for the last element, -2 for the second last and etc.
a_tuple = (4, 5, 7, 3)
print(a_tuple[-1]) # last item is 3
Output
3 >
to access a nested list or tuple, we need two indexes.
a_tuple = (4, 5, 7, [2, 6])
print(a_tuple[3][0]) # 1st item inside 4th item
Output
2 >
The count()
function to count how many times an element appears inside a tuple. For example, the letter 'e' appears 3 times in the following tuple.
a_tuple = ('t','e','a','c','h','e','r','c','u','b','e')
print(a_tuple.count('e')) # 'e' appeard 3 times
Output
3 >
The index() function returns the index number of the element in a tuple. For example, the index of the letter 'r' is 6:
a_tuple = ('t','e','a','c','h','e','r','c','u','b','e')
print(a_tuple.index('r')) # the index of 'r' is 6
Output
6 >
Slicing
To retrieve a subset of the tuple. Syntax is:
tuple_name[start: end]
return element from the start index to the end-1 index. The default value for the start is 0 (zero) and for the end is the length of a tuple.
a_tuple = ('red', 'green', 'blue','grey')
print(a_tuple[1:3]) # get 2nd until 3rd item
print(a_tuple[:]) # get 1st until last item
Output
('green', 'blue') ('red', 'green', 'blue', 'grey') >
Membership
To check if an element exists inside a tuple. This statement will return True if exists, and False if not.
value [in|not in] tuple_name
This example checks if a string exists in a tuple
a_tuple = ("Math", "Science", "Geography", "History")
print("Math" in a_tuple) # True
print("Chemistry" in a_tuple) # False
Output
True False >
This membership check is used together with for loop to iterate through the tuple. The following, example displays all the elements in the tuple:
a_tuple = ("Math", "Science", "Geography", "History")
for element in a_tuple:
print(element)
Output
Math Science Geography History >
A post by Cuber
Comments
Post a Comment