Slice Operator and Tuple Methods
M3-R5.1 · Chapter 5: Sequence Data Types · 7 min read
Slice Operator:
tuple-name [start: end ] #will give you elements between indices start to end-1.
>>>alpha=('q','w','e','r','t','y')
>>> alpha[1:-3]
('w', 'e')
>>> alpha[3:6]
('r', 't', 'y')
>>> alpha[-1:-5]
( )
>>> alpha[-5:-1]
('w', 'e', 'r', 't')
Comparison Operators:
- Compare two tuples
- It compares the each corresponding element must compare equal and two
- Sequences must be of the same type.
- For non-equal comparison as soon as it gets a result in terms of True/False,
- from corresponding elements’ comparison. If Corresponding elements are
- equal, it goes to the next element and so on, until it finds elements that differ.
Example:
>>> T1 = (9, 16, 7)
>>> T2 = (9, 16, 7)
>>> T3 = ('9','16','7')
|
>>> T1 = = T2 |
>>> T1==T3 |
>>> T4 = (9.0, 16.0, 7.0) >>> T1==T4 |
>>> T1<T2 |
>>> T1<=T2 |
|
True |
False |
True |
False |
True |
Membership: We can apply the ‘in’ and ‘not in’ operator on the items. This tells us whether they belong to the tuple.
>>> "a" in tuple("string")
False
>>> "a" not in tuple("string")
True
Tuple Methods: Consider a tuple: subject=("Hindi","English","Maths","Physics")
|
Function Name |
Description |
Example |
|
len( ) |
Find the length of a tuple. Syntax: len (tuple-name) |
>>>subject=("Hindi","English","Maths","Physics”) >>> len(subject) 4 |
|
max( ) |
Returns the largest value from a tuple. Syntax: max(tuple-name) |
>>> max(subject) 'Physics' |
|
Error: If the tuple contains values of different data types, then it will give an error because mixed data type comparison is not possible. >>> subject = (15, "English", "Maths", "Physics", 48.2) >>> max(subject) TypeError: '>' not supported between instances of 'str' and 'int' |
||
|
index( ) |
Returns the index of the first element with the specified value. Syntax: tuple:name.index(element) |
>>>subject=("Hindi","English","Maths","Physics") >>> subject.index("Maths") 2 |
|
count( ) |
Return the number of times the value appears. Syntax: tuplename.count(element) |
>>> subject.count("English") 1 |
Delete a Tuple: The del statement is used to delete elements and objects but as you know that tuples are
Immutable, which also means that individual element of a tuple cannot be deleted
Example:
>> T=(2,4,6,8,10,12,14)
>>> del T[3]
TypeError: 'tuple' object doesn't support item deletion
But you can delete a complete tuple with del statement as:
Example:
>>> T=(2,4,6,8,10,12,14)
>>> del T
>>> T
NameError: name 'T' is not defined
|
Q: Python program to add a tuple to a list myList = [9, 3, 1, 4] # Creating the List print("Initially List : " + str(myList)) myTuple = (2, 6) # Creating Tuple myList += myTuple # Adding the tuple to list # Printing resultant List print("List after Addition : " + str(myList)) |
Q: Create a tuple with single item 50 tup1= (50, ) print(tup1) Unpack the tuple into 4 variables tuple1 = (10, 20, 30, 40) a, b, c, d = tuple1 print(a) print(b) print(c) print(d) |
|
Q: Access value 20 from the tuple tup1 = ("Orange", [10, 20, 30], (5, 15, 25)) # tup1[0] = 'Orange' # tup1[1] = [10, 20, 30] # list1[1][1] = 20 print(tup[1][1]) |
Q: Swap two tuples in Python tup1 = (11, 22) tup2 = (99, 88) tup1, tup2 = tup2, tup1 print(tup2) print(tup1) |
|
Q: Write a program to copy elements 44 and 55 from the following tuple into a new tuple. tup1 = (11, 22, 33, 44, 55, 66) tup2 = tup1[3:-1] print(tup2) |
Q: Write a program to modify the first item (22) of a list inside a following tuple to 222 tup1 = (11, [22, 33], 44, 55) tup1[1][0] = 222 print(tup1) |
|
Q: Write a program to access nested tuples element. tup1 = ([7,4,3],”JAI”,(10,11,12)) print(tup1[0][0]) print(tup1[0][1]) print(tup1[0][2]) print(tup1[1][0]) print(tup1[1][1]) print(tup1[1][2]) print(tup1[2][0]) print(tup1[2][1]) print(tup1[2][2]) |
Q: Write a program to access nested tuples element. tup1=(1,3,5,7,9,11,13) print(tup1[:]) #copy the tuple print(tup1[1:]) # copy all value at index 1 or more print(tup1[:1]) # copy one value starting at first print(tup1[2:4]) # copy value from index 2 to 4 Output: (1,3,5,7,9,11,13) (3,5,7,9,11,13) (1,) (5,7) |
|
Q: Write a program on tuples string element. tup1 = (“Python”,”Tuples”,”are”,”Immutable”) print(“Total length of Tuple:”,len(tup1)) print(“maximum element of tuple:”,max(tup1)) print(“Maximum element of tuple:”,min(tup1)) print(“No. of occurance of are”,tup1.count(“are”)) |
Q: Convert a tuple in to a string. tup = (1, 2, 3) str1 = str(tup) print(string) # Output: "(1, 2, 3)" Convert a list in to a tuple lst = [1, 2, 3, 4, 5] tup = tuple(lst) print(tup) # Output: (1, 2, 3, 4, 5) |
|
Q: Find the index of an element in a tuple tup = (10, 20, 30, 40, 50) index = tup.index(30) print(index) # Output: 2 |
Q: Find the common elements between two tuples tup1 = (1, 2, 3, 4) tup2 = (3, 4, 5, 6) common =tuple(i for i in tup1 if i in tup2) print(common) |
|
Q:Sort a tuple of integers tup = (5, 3, 8, 1, 2) sorted_tup = sorted(tup) print(sorted_tup) |
Q: Merge two tuples and remove duplicates. tup1 = (1, 2, 3) tup2 = (3, 4, 5) result = set(tup1).union(tup2) print(result) # Output: (1, 2, 3, 4, 5) |
|
Q: Find the first and last elements of a tuple. tup = (10, 20, 30, 40, 50) first, last = tup[0], tup[-1] print("First:", first) print("Last:", last) |
Q:Count the number of even and odd numbers in a tuple tup = (1, 2, 3, 4, 5, 6, 7, 8, 9) even = sum(1 for x in tup if x % 2 == 0) odd = len(tup) - even print("Even:", even) print("Odd:", odd) |
|
Q: Find the frequency of each element in a tuple. tup = (1, 2, 2, 3, 2, 4, 2) freq= {} for i in tup: freq[i] = freq.get(i, 0) + 1 print(freq) |
Q: Find the difference between two tuples. tup1 = (1, 2, 3, 4, 5) tup2 = (3, 4, 5, 6) diff = tuple(x for x in tup1 if x not in tup2) print(diff) |