How to calculate the minimum and maximum value in a tuple?
days = "Monday","Sunday","Tuesday","Wednesday","Thursday"
----------------------------
The maximum and minimum value of tuple is calculated by using the below functions.
- max()
- min()
max() is for maximum value
min() is for minimum value
----------------------------
In the given tuple,
days = "Monday","Sunday","Tuesday","Wednesday","Thursday"
The maximum value shall be printed using the below code:
days = "Monday","Sunday","Tuesday","Wednesday","Thursday"
print(max(days))
The output of the above code shall be Wednesday. This shall be based on the ASCII code of the letters. When all the elements in the tuple are considered, the first letter shall be checked and letter with highest ASCII value shall be the Maximum value in that tuple. In the given elements of the tuple days, 'W' of Wednesday is greater than any starting letter of the elements.
Note: ASCII table is included below in this post for reference
----------------------------
In the given tuple,
days = "Monday","Sunday","Tuesday","Wednesday","Thursday"
The minimum value shall be printed using the below code:
days = "Monday","Sunday","Tuesday","Wednesday","Thursday"
print(min(days))
The output of the above code shall be Monday. This shall be based on the ASCII code of the letters. When all the elements in the tuple are considered, the first letter shall be checked and letter with lowest ASCII value shall be the minimum value in that tuple. In the given elements of the tuple days, 'M' of Monday is lower than any starting letter of the elements.
Note: ASCII table is included below in this post for reference
----------------------------
Integer values in tuple:
Let us assume there are integer elements in a tuple
days = 1,2,3,4,5
The minimum value of this tuple is 1 as 1 is the lowest integer
The maximum value of this tuple is 5 as 5 is the highest integer
Float values in tuple:
Let us assume there are float elements in a tuple
days = 1.5,3.4,2.3,0.1
The minimum value of this tuple is 0.1 as 0.1 is the lowest floating point number
The maximum value of this tuple is 3.4 as 3.4 is the highest floating point number
Integer and Float values in tuple:
Let us assume there are integer and float elements in a tuple
days = 1.0,1
In the above tuple, 1.0 and 1 has the same value but one is integer and one is floating point number.
In scenarios where there is same element in the tuple, the first occurrence is reported as the minimum value or maximum value as per the function used
The minimum value of this tuple is 1.0 as 1.0 has occurred first than 1
The maximum value of this tuple is 1.0 as 1.0 has occurred first than 1
0 Comments