max keyword in python code example
Example 1: python max function
square = {2: 4, -3: 9, -1: 1, -2: 4}
# the largest key
key1 = max(square)
print("The largest key:", key1) # 2
# the key whose value is the largest
key2 = max(square, key = lambda k: square[k])
print("The key with the largest value:", key2) # -3
# getting the largest value
print("The largest value:", square[key2]) # 9
Example 2: python max()
i = max(2, 4, 6, 3)
print(i)
# Result will be 6 because it is the highest number
Example 3: max func in python
max([2, 1, 4, 3])
#Returns 4 as it's the largest integer in the list
Example 4: max in python
print(max(2, 3)) # Returns 3 as 3 is the largest of the two values
print(max(2, 3, 23)) # Returns 23 as 23 is the largest of all the values
list1 = [1, 2, 4, 5, 54]
print(max(list1)) # Returns 54 as 54 is the largest value in the list