Sayantan's Blog On Python Programming

PYTHON TUPLE BUILT-IN FUNCTIONS

PYTHON TUPLE BUILT-IN FUNCTIONS

INTRODUCTIONS

Python provides some useful built-in functions which can be used while working with tuples.

len() FUNCTION

len() function returns the no of fields present in a tuple including duplicates.

# Commonly used Python tuple built-in methods example 1

int_tuple = (400, 200, 600, 100, 150, 500, 600) 
print("\nFields of integer tuple ->", int_tuple)
print("\nNo of Fields in integer tuple ->", len(int_tuple))

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'PHP') 
print("\nFields of string tuple ->", str_tuple)
print("\nNo of Fields in string tuple ->", len(str_tuple))

mix_tuple = (200, 600, 100, 'Python', 'Java', 5.8, True)
print("\nFields of mixed tuple ->", mix_tuple)
print("\nNo of Fields in mixed tuple ->", len(mix_tuple))

PYTHON TUPLE BUILT-IN FUNCTIONS : Output

Fields of integer tuple -> (400, 200, 600, 100, 150, 500, 600)

No of Fields in integer tuple -> 7

Fields of string tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'PHP')

No of Fields in string tuple -> 7

Fields of mixed tuple -> (200, 600, 100, 'Python', 'Java', 5.8, True)

No of Fields in mixed tuple -> 7

count() FUNCTION

count() function returns the number of occurrences of an field in a given tuple.

# Commonly used Python tuple built-in methods example 2

int_tuple = (400, 200, 600, 100, 150, 500, 600) 
print("\nFields of integer tuple ->", int_tuple)
print("\nNo of occurrences of 600 in integer tuple ->", int_tuple.count(600))

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'PHP') 
print("\nFields of string tuple ->", str_tuple)
print("\nNo of occurrences of PHP in string tuple ->", str_tuple.count('PHP'))

mix_tuple = (200, 600, 100, 'Python', 'Java', 5.8, True)
print("\nFields of mixed tuple ->", mix_tuple)
print("\nNo of occurrences of True in mixed tuple ->", mix_tuple.count(True))

PYTHON TUPLE BUILT-IN FUNCTIONS : Output

Fields of integer tuple -> (400, 200, 600, 100, 150, 500, 600)

No of occurrences of 600 in integer tuple -> 2

Fields of string tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'PHP')

No of occurrences of PHP in string tuple -> 2

Fields of mixed tuple -> (200, 600, 100, 'Python', 'Java', 5.8, True)

No of occurrences of True in mixed tuple -> 1

sum() FUNCTION

sum() function returns the total of all the fields of a tuple having integer or floating number or both.

# Commonly used Python tuple built-in methods example 3

int_tuple = (400, 200, 600, 100, 150, 500, 600)
print("\nFields of integer tuple ->", int_tuple)
print("\nTotal of all the fields in integer tuple ->", sum(int_tuple))

float_tuple = (4.5, 2.6, 6.9, 10.3, 15.7, 20.5, 16.8) 
print("\nFields of floating tuple ->", float_tuple)
print("\nTotal of all the fields in floating tuple ->", sum(float_tuple))

mixed_tuple = (4.5, 2.6, 600, 10.3, 150, 20.5, 600) 
print("\nFields of mixed tuple ->", mixed_tuple)
print("\nTotal of all the fields in mixed tuple ->", sum(mixed_tuple))

PYTHON TUPLE BUILT-IN FUNCTIONS : Output

Fields of integer tuple -> (400, 200, 600, 100, 150, 500, 600)

Total of all the fields in integer tuple -> 2550

Fields of floating tuple -> (4.5, 2.6, 6.9, 10.3, 15.7, 20.5, 16.8)

Total of all the fields in floating tuple -> 77.3

Fields of mixed tuple -> (4.5, 2.6, 600, 10.3, 150, 20.5, 600)

Total of all the fields in mixed tuple -> 1387.9

max() FUNCTION

max() function returns the maximum of all the fields of a tuple having integer or floating number or both.

# Commonly used Python tuple built-in methods example 4

int_tuple = (400, 200, 600, 100, 150, 500, 600)
print("\nFields of integer tuple ->", int_tuple)
print("\nHighest field in integer tuple ->", max(int_tuple))

float_tuple = (4.5, 2.6, 6.9, 10.3, 15.7, 20.5, 16.8) 
print("\nFields of floating tuple ->", float_tuple)
print("\nHighest field in floating tuple ->", max(float_tuple))

mixed_tuple = (4.5, 2.6, 600, 10.3, 150, 20.5, 600) 
print("\nFields of mixed tuple ->", mixed_tuple)
print("\nHighest field in mixed tuple ->", max(mixed_tuple))

PYTHON TUPLE BUILT-IN FUNCTIONS : Output

Fields of integer tuple -> (400, 200, 600, 100, 150, 500, 600)

Highest field in integer tuple -> 600

Fields of floating tuple -> (4.5, 2.6, 6.9, 10.3, 15.7, 20.5, 16.8)

Highest field in floating tuple -> 20.5

Fields of mixed tuple -> (4.5, 2.6, 600, 10.3, 150, 20.5, 600)

Highest field in mixed tuple -> 600

min() FUNCTION

min() function returns the minimum of all the fields of a tuple having integer or floating number or both.

# Commonly used Python tuple built-in methods example 5

int_tuple = (400, 200, 600, 100, 150, 500, 600)
print("\nFields of integer tuple ->", int_tuple)
print("\nLowest field in integer tuple ->", min(int_tuple))

float_tuple = (4.5, 2.6, 6.9, 10.3, 15.7, 20.5, 16.8) 
print("\nFields of floating tuple ->", float_tuple)
print("\nLowest field in floating tuple ->", min(float_tuple))

mixed_tuple = (4.5, 2.6, 600, 10.3, 150, 20.5, 600) 
print("\nFields of mixed tuple ->", mixed_tuple)
print("\nLowest field in mixed tuple ->", min(mixed_tuple))

PYTHON TUPLE BUILT-IN FUNCTIONS : Output

Fields of integer tuple -> (400, 200, 600, 100, 150, 500, 600)

Lowest field in integer tuple -> 100

Fields of floating tuple -> (4.5, 2.6, 6.9, 10.3, 15.7, 20.5, 16.8)

Lowest field in floating tuple -> 2.6

Fields of mixed tuple -> (4.5, 2.6, 600, 10.3, 150, 20.5, 600)

Lowest field in mixed tuple -> 2.6

all() FUNCTION

all() function returns True if all the fields are True. fields 0 is considered as False. But any negative value is considered as True.

# Commonly used Python tuple built-in methods example 6

int_tuple = (400, 200, 600, 100, 150, 500, -60)
print("\nFields of integer tuple ->", int_tuple)
print("\nAll the fields in integer tuple are True ? ->", all(int_tuple))

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML') 
print("\nFields of string tuple ->", str_tuple)
print("\nAll the fields in string list are True ? ->", all(str_tuple))

mixed_tuple = (45, -2.6, 'Python', 'Java', 'C++', 20.5, 60) 
print("\nFields of mixed tuple ->", mixed_tuple)
print("\nAll the fields in mixed tuple are True ? ->", all(mixed_tuple))

PYTHON TUPLE BUILT-IN FUNCTIONS : Output

Fields of integer tuple -> (400, 200, 600, 100, 150, 500, -60)

All the fields in integer tuple are True ? -> True

Fields of string tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML')

All the fields in string list are True ? -> True

Fields of mixed tuple -> (45, -2.6, 'Python', 'Java', 'C++', 20.5, 60)

All the fields in mixed tuple are True ? -> True

In the above example, we have used both positive and negative values. All the cases python has returned True.

# Commonly used Python tuple built-in methods example 7

int_tuple = (400, 200, 600, 100, 150, 500, -60, 0) 
print("\nFields of integer tuple ->", int_tuple)
print("\nAll the fields in integer tuple are True ? ->", all(int_tuple))

str_tuple =  ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 0) 
print("\nFields of string tuple ->", str_tuple)
print("\nAll the fields in string list are True ? ->", all(str_tuple))

mixed_tuple = (45, -2.6, 'Python', 'Java', 'C++', 20.5, 60, 0)
print("\nFields of mixed tuple ->", mixed_tuple)
print("\nAll the fields in mixed tuple are True ? ->", all(mixed_tuple))

PYTHON TUPLE BUILT-IN FUNCTIONS : Output

Fields of integer tuple -> (400, 200, 600, 100, 150, 500, -60, 0)

All the fields in integer tuple are True ? -> False

Fields of string tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 0)

All the fields in string list are True ? -> False

Fields of mixed tuple -> (45, -2.6, 'Python', 'Java', 'C++', 20.5, 60, 0)

All the fields in mixed tuple are True ? -> False

In the above example, all the tuples are having 0 as an field. That’s why python has returned False in all the cases.

any() FUNCTION

any() function returns True if any of the fields is True.

# Commonly used Python tuple built-in methods example 8

int_tuple = (400, 200, 600, 100, 150, 500, -60, 0) 
print("\nFields of integer tuple ->", int_tuple)
print("\nAll the fields in integer tuple are True ? ->", any(int_tuple))

str_tuple =  ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 0) 
print("\nFields of string tuple ->", str_tuple)
print("\nAll the fields in string list are True ? ->", any(str_tuple))

mixed_tuple = (45, -2.6, 'Python', 'Java', 'C++', 20.5, 60, 0)
print("\nFields of mixed tuple ->", mixed_tuple)
print("\nAll the fields in mixed tuple are True ? ->", any(mixed_tuple))

PYTHON TUPLE BUILT-IN FUNCTIONS : Output

Fields of integer tuple -> (400, 200, 600, 100, 150, 500, -60, 0)

All the fields in integer tuple are True ? -> True

Fields of string tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 0)

All the fields in string list are True ? -> True

Fields of mixed tuple -> (45, -2.6, 'Python', 'Java', 'C++', 20.5, 60, 0)

All the fields in mixed tuple are True ? -> True

any() function will return False only when the tupleis empty or all the list fields are 0.

# Commonly used Python tuple built-in methods example 9

int_tuple = () 
print("\nFields of integer tuple ->", int_tuple)
print("\nAny of the fields in integer tuple is True ? ->", any(int_tuple))

mixed_list = (0, 0, 0, 0) 
print("\nFields of mixed tuple ->", mixed_list)
print("\nAny of the fields in mixed tuple is True ? ->", any(mixed_list))

PYTHON TUPLE BUILT-IN FUNCTIONS : Output

Fields of integer tuple -> ()

Any of the fields in integer tuple is True ? -> False

Fields of mixed tuple -> (0, 0, 0, 0)

Any of the fields in mixed tuple is True ? -> False

tuple() FUNCTION

tuple() function is used to create a tuple with the arguments passed through it. The arguments must be an iterable item. It can be a string or a tuple or any complex data type which can be iterable like list, dictionary, set etc. Iterable means that we can loop through the item. Integer is not iterable, but in python we can loop through a string, as we will see in the below example.

# Commonly used Python tuple built-in methods example 10

b = 'Python'
print("\nValue of b ->", b)
list_b = tuple(b)
print("\nFields  of list_b ->", list_b)

int_list = [400, 200, 600, 100, 150, 500]
int_tuple = tuple(int_list)
print("\nFields  of integer tuple ->", int_tuple)

PYTHON TUPLE BUILT-IN FUNCTIONS : Output

Value of b -> Python

Fields  of list_b -> ('P', 'y', 't', 'h', 'o', 'n')

Fields  of integer tuple -> (400, 200, 600, 100, 150, 500)

# Commonly used Python tuple built-in methods example 11

int_list = [400, 200, 600, 100, 150, 500]
int_tuple = tuple(int_list)
print("\nFields of integer tuple ->", int_tuple)

float_list = [4.2, 2.6, 6.5, 1.8, 15.2, 5.3]
float_tuple = tuple(float_list)
print("\nFields of floating tuple ->", float_tuple)

str_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML']
str_tuple = tuple(str_list)
print("\nFields of string tuple ->", str_tuple)

empty_list = []
empty_tuple = tuple(empty_list)
print("\nFields of empty tuple ->", empty_tuple)

PYTHON TUPLE BUILT-IN FUNCTIONS : Output

Fields of integer tuple -> (400, 200, 600, 100, 150, 500)

Fields of floating tuple -> (4.2, 2.6, 6.5, 1.8, 15.2, 5.3)

Fields of string tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML')

Fields of empty tuple -> ()

If we pass an empty list or string as a parameter to tuple() function then python will create an empty tuple.

range() FUNCTION

range() function creates a sequence of numbers with in a given range and criteria. It takes three arguments as input.

  • Start > It indicates the starting number of the range
  • End > It indicates the end number of the range
  • Step > It indicates the increment or decrement criteria

Using Positive Step Size

# Commonly used Python list built-in methods example 12

odd_range = range(1, 20, 2)
odd_tuple = tuple(odd_range)
print("\nFields of odd integer tuple ->", odd_tuple)

even_range = range(2, 20, 2)
even_tuple = tuple(even_range)
print("\nFields of even integer tuple ->", even_tuple)

PYTHON TUPLE BUILT-IN FUNCTIONS : Output

Fields of odd integer tuple -> (1, 3, 5, 7, 9, 11, 13, 15, 17, 19)

Fields of even integer tuple -> (2, 4, 6, 8, 10, 12, 14, 16, 18)

Using Negative Step Size

# Commonly used Python list built-in methods example 13

odd_range = range(20, 1, -2)
odd_tuple = tuple(odd_range)
print("\nFields of odd integer tuple in descending order ->", odd_tuple)

even_range = range(20, 1, -2)
even_tuple = tuple(even_range)
print("\nFields of even integer tuple in descending order ->", even_tuple)

PYTHON TUPLE BUILT-IN FUNCTIONS : Output

Fields of odd integer tuple in descending order -> (20, 18, 16, 14, 12, 10, 8, 6, 4, 2)

Fields of even integer tuple in descending order -> (20, 18, 16, 14, 12, 10, 8, 6, 4, 2)

PYTHON TUPLE BUILT-IN FUNCTIONS : Output
PYTHON TUPLE BUILT-IN FUNCTIONS : Output

RELATED TOPICS:

1 thought on “PYTHON TUPLE BUILT-IN FUNCTIONS”

  1. Pingback: PYTHON TUPLE DEEP COPY - Sayantan's Blog On Python Programming

Leave a Comment

Your email address will not be published. Required fields are marked *