#PYTHON(DAY8)
Tuple and It’s methods
Tuple()
- Tuples are sequential, indexable, heterogeneous containers which are immutable in nature
- ‘( )’ are used to represent a tuple, tuple can never be empty, it takes atleast 2 values
- a tuple with one element can be created using ‘,’ and passing no second element
- there are two methods we can perform on tuple
- we can use tuple as unpack
- tuples are faster when compared to list
Example:
mytuple = ("apple", 1,2)
print(mytuple)
And the output is:
('apple',1,2)
So, to know the type of class we use type() function
x = (1,2,3,1,'sree',4)
print(x)
print(type(x))
And the expected output is:
(1,2,3,1,'sree',4)
<class 'tuple'>
Tuple Length
To determine how many items a tuple has, use the len()
function
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
And the expected output is:
3
creating an empty tuple
x = ()
print(x, len(x), type(x), sep = '\n')
And the expected output is:
()
0
<class 'tuple'>
creating a tuple with 2 elements
x = (1,)
print(x, len(x), type(x), sep = '\n')
And the expected output is:
(1,)
1
<class 'tuple'>
NOTE: tuple does not contain a single element
x = (1)
print(x, len(x), type(x), sep = '\n')
we get an error for this as: object of type ‘int’ has no len()
type-casting
converting list to tuple
a = [1,2,3]
x = tuple(a)
print(x, len(x), type(x), sep = '\n')
And the expected output is:
(1, 2, 3)
3
<class 'tuple'>
converting string to tuple
a = 'generic string'
x = tuple(a)
print(x, len(x), type(x), sep = '\n')
And the expected output is:
('g', 'e', 'n', 'e', 'r', 'i', 'c', ' ', 's', 't', 'r', 'i', 'n', 'g')
14
<class 'tuple'>
Access Tuple Items
You can access tuple items by referring to the index number, inside square brackets:
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
And the expected output is:
banana
The first item has index as 0
Negative Indexing
Negative indexing means start from the end.
-1
refers to the last item, -2
refers to the second last item etc.
thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])
And the output is:
cherry
Methods in tuple():
- index()
- count()
Range of Indexes
You can specify a range of indexes by specifying where to start and where to end the range.
When specifying a range, the return value will be a new tuple with the specified items.
x = ('apple','banana','grapes','mango','orange','kiwi')
print([2:5])
And the expected output is:
('grapes','mango','orange')
Note: The search will start at index 2 (included) and end at index 5 (not included).
By leaving out the start value, the range will start at the first item:
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[:4])
And the expected output is:
('apple', 'banana', 'cherry', 'orange')
Range of Negative Indexes
Specify negative indexes if you want to start the search from the end of the tuple:
This example returns the items from index -4 (included) to index -1 (excluded)
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[-4:-1])
And the expected output is:
('orange', 'kiwi', 'melon')
Check if Item Exists
To determine if a specified item is present in a tuple use the in
keyword:
Check if “apple” is present in the tuple:
thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple:
print("Yes, 'apple' is in the fruits tuple")
And the expected output is:
Yes, 'apple' is in the fruits tuple
count()
Returns the number of times the value appears in the tuple
thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thistuple.count(5)
print(x)
And the expected output is:
2
Unpacking a tuple
we can also extract the values back into variables. This is called “unpacking”
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)
And the expected output is:
apple
banana
cherry
No comments:
Post a Comment