Python Tutorial | Python tuples 5

Python Tuple Methods

Methods that add items or remove items are not available with tuple. Only the following two methods are available.

count(x): Return the number of items that is equal to x

index(x): Return the index of the first item that is equal to x

all(): Return True if all elements of the tuple are true (or if the tuple is empty).

len() : Return the length (the number of items) in the tuple.

max() : Return the largest item in the tuple.

min(): Return the smallest item in the tuple

sorted(): Take elements in the tuple and return a new sorted list (does not sort the tuple itself).

sum(): Returns the sum of all elements in the tuple.

count(x):

Return the number of items that is equal to x

example:
n1=(1,2,3,2,1,4,5,1)
print(n1)
print(n1.count(1))
#to count total no of “1” present in tuple
#output: 3

n1=(1,2,3,2,1,4,5,1)
print(n1)
print(n1.count(1))
#Output

(1, 2, 3, 2, 1, 4, 5, 1)
3

Example:

n1=(1,2,3,2,1,4,5,1)
print(n1)
print(n1.count(2))
#to count total no of “2” present in tuple
#output: 2

n1=(1,2,3,2,1,4,5,1)
print(n1)
print(n1.count(2))
#Output

(1, 2, 3, 2, 1, 4, 5, 1)
2

Example:

n2=(‘h’,’e’,’l’,’l’,’o’)
print(n2)
print(n2.count(‘l’))

#to count total no of “l” present in tuple
#output: 2

n2=('h','e','l','l','o')
print(n2)
print(n2.count('l'))
#Output

('h', 'e', 'l', 'l', 'o')
2

index(x):

Return the index of the first item that is equal to x

n2=(‘h’,’e’,’l’,’l’,’o’)
print(n2)
print(n2.index(‘l’))
#to find position of “l” is present
#output: 2

print(n2.index(‘e’))
#to count total no of “e” is present
#output: 1

print(n2.index(‘x’))
#error : as element is not present

n2=('h','e','l','l','o')
print(n2)
print(n2.index('l'))
print(n2.index('e'))
print(n2.index('x'))
#Output
('h', 'e', 'l', 'l', 'o')
2
1
Traceback (most recent call last):
  File "E:\batch79\pp1.py", line 6, in <module>
    print(n2.index('x'))
ValueError: tuple.index(x): x not in tuple

all():

Return True if all elements of the tuple are true (or if the tuple is empty).

n1=(1,20,30,22,31,54,5,1)
print(n1)
print(all(n1))

n1=(1,20,30,22,31,54,5,1)
print(n1)
print(all(n1))
n1=()
print(n1)
print(all(n1))
n1='False',
print(n1)
print(all(n1))
n1=(' ',' ')
print(n1)
print(all(n1))
n1=(0,0,0)
print(n1)
print(all(n1))
n1=(0,0,1)
print(n1)
print(all(n1))
n1=(0,1,1)
print(n1)
print(all(n1))
n1=(1,1,1)
print(n1)
print(all(n1))
#Output
(1, 20, 30, 22, 31, 54, 5, 1)
True
()
True
('False',)
True
(' ', ' ')
True
(0, 0, 0)
False
(0, 0, 1)
False
(0, 1, 1)
False
(1, 1, 1)
True

max():

Return the largest item in the tuple.

n1=(1,20,30,22,31,54,5,1)
print(n1)
print(max(n1))

#1
n1=(1,20,30,22,31,54,5,1)
print(n1)
print(max(n1))
#2
n1=(12,34,54,56,76,78)
print(n1)
print(max(n1))
#Output
(1, 20, 30, 22, 31, 54, 5, 1)
54
(12, 34, 54, 56, 76, 78)
78

min():

Return the smallest item in the tuple

n1=(1,20,30,22,31,54,5,1)
print(n1)
print(min(n1))

#1
n1=(1,20,30,22,31,54,5,1)
print(n1)
print(min(n1))
#2
n1=(12,34,54,56,76,78)
print(n1)
print(min(n1))
#Output:
(1, 20, 30, 22, 31, 54, 5, 1)
1
(12, 34, 54, 56, 76, 78)
12

len():

Return the length (the number of items) in the tuple.

n1=(1,20,30,22,31,54,5,1)
print(n1)
print(len(n1))

#1
n1=(10,20,30,40,50)
print(n1)
print(len(n1))
#2
n1=(1,20,30)
print(n1)
print(len(n1))
#3
n1=(1,20,30,22,31,54,5,1)
print(n1)
print(len(n1))
#Output
(1, 20, 30, 22, 31, 54, 5, 1)
8
PS E:\batch79> python pp1.py
(10, 20, 30, 40, 50)
5
(1, 20, 30)
3
(1, 20, 30, 22, 31, 54, 5, 1)
8

sum():

Returns the sum of all elements in the tuple.

n1=(1,20,30,22,31,54,5,1)
print(n1)
print(sum(n1))

#1
n1=(1,20,30,22,31,54,5,1)
print(n1)
print(sum(n1))
#2
n1=(12,34,54,56,76,78)
print(n1)
print(sum(n1))
#Output
(1, 20, 30, 22, 31, 54, 5, 1)
164
(12, 34, 54, 56, 76, 78)
310

sorted():

Takes elements in the tuple and return a new sorted list (does not sort the tuple itself).

n1=(1,20,30,22,31,54,5,1)
print(n1)
print(sorted(n1))

#1
n1=(1,20,30,22,31,54,5,1)
print(n1)
print(sorted(n1))
print(n1)
#2
n1=(45,3,76,8,34,6)
print(n1)
n2=sorted(n1)
print(n2)
#Output
(1, 20, 30, 22, 31, 54, 5, 1)
[1, 1, 5, 20, 22, 30, 31, 54]
(1, 20, 30, 22, 31, 54, 5, 1)
(45, 3, 76, 8, 34, 6)        
[3, 6, 8, 34, 45, 76]

Other Tuple Operations

Tuple Membership Test

We can test if an item exists in a tuple or not, using the keyword in.

# In operation

n = (‘a’,’p’,’p’,’l’,’e’,)
print(‘a’ in n)
# Output: True
print(‘b’ in n)
# Output: False

 

# In operation
n = ('a','p','p','l','e',)
print('a' in n)
print('b' in n)
#Output
True
False

n=tuple((“amit”,”sumit”,”kapil”))
print(n)
print(“amit” in n)
print(“amita” in n)
print(“sumit” in n)
print(“sumita” in n)
print(“Sumit” in n)
print(“Amit” in n)

Output:

(‘amit’, ‘sumit’, ‘kapil’)
True
False
True
False
False
False

n=tuple(("amit","sumit","kapil"))
print(n)
print("amit" in n)
print("amita" in n)
print("sumit" in n)
print("sumita" in n)
print("Sumit" in n)
print("Amit" in n)
#Output
('amit', 'sumit', 'kapil')
True 
False
True 
False
False
False

# Not in operation

n = (‘a’,’p’,’p’,’l’,’e’,)
print(‘a’ not in n)
# Output: False
print(‘b’ not in n)
# Output: True

 

# Not in operation

n = ('a','p','p','l','e',)
print('a' not in n)
print('b' not in n)
#Output
False
True

n=tuple((“amit”,”sumit”,”kapil”))
print(n)
print(“amit” not in n)
print(“amita” not in n)
print(“sumit” not in n)
print(“sumita” not in n)
print(“Sumit” not in n)
print(“Amit” not in n)

Output:

(‘amit’, ‘sumit’, ‘kapil’)
False
True
False
True
True
True

n=tuple(("amit","sumit","kapil"))
print(n)
print("amit" not in n)
print("amita" not in n)
print("sumit" not in n)
print("sumita" not in n)
print("Sumit" not in n)
print("Amit" not in n)
#Output
('amit', 'sumit', 'kapil')
False
True
False
True
True
True

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling