Python Set – max() Function

It is a function in python that returns the element with the maximum value in the given set. If it’s a string then it returns the value with the highest ASCII value. It doesn’t modifies the given set.

Syntax:
max(set)

Also Read: Python Set – enumerate() Method

Example 1:

# Python code to understand max() Function in set
# www.codewindow.in

s1 = {"zebra", "window"}
s = {"code", "website", "best"}
print(max(s1))
print(max(s))

Output:

zebra
website

Explanation:
Here, it returned “zebra” as “z” having the highest ascii value among all other values. The next one “website” as “w” having the highest ascii value among all other values.

Example 2:

# Python code to understand max() Function in set
# www.codewindow.in

s1 = {2, 5, 8, 7, 9}
s = {10, 5, 2}
print(max(s1))
print(max(s))

Output:

9
10

Explanation:
Here, 9 is the maximum value among all the other elements in set s1 and 10 is the maximum value among all the other elements in set s.


Follow Us

You Missed

Also Checkout