Python Program for Set max() Method

In the previous article, we have discussed Python Program for Set copy() Method
Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given Set = {20, 40, 50, 10, 20, 60, 50}

Output:

The given set is :
{40, 10, 50, 20, 60}
The above Given set's maximum value =  60

Explanation:

Here 60 is the maximum value in the given set . so, the maximum value 60 is printed by using the max() method.

Example2:

Input:

Given Set = {1, 3, 5, 20, 60, 90}

Output:

The given set is :
{1, 3, 5, 20, 90, 60}
The above Given set's maximum value =  90

Explanation:

Here 90 is the maximum value in the given set . so, the maximum value 90 is printed by using the max() method.

Program for Set max() Method in Python

set max() Method:

One of the set methods in Python is the set max function, which is used to find the maximum value within a given set.

Syntax:

max(set_name)

The set max function returns the maximum number of items from a given set’s total number of items.

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Apply max() method to the given set to get the maximum value in the given set.
  • Store it in another variable.
  • Print the maximum value in the above-given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {20, 40, 50, 10, 20, 60, 50}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply max() method to the given set to get the maximum value in the given set.
# Store it in another variable.
maxim_val = max(gven_set)
# Print the maximum value in the above-given set.
print("The above Given set's maximum value = ", maxim_val)

Output:

The given set is :
{40, 10, 50, 20, 60}
The above Given set's maximum value =  60

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the set as user input using the set(), map(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Apply max() method to the given set to get the maximum value in the given set.
  • Store it in another variable.
  • Print the maximum value in the above-given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(),map(), input(), and split() functions.
# Store it in a variable.
gven_set = set(map
(int,input("Enter some random values separated by spaces = ").split()))
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply max() method to the given set to get the maximum value in the given set.
# Store it in another variable.
maxim_val = max(gven_set)
# Print the maximum value in the above-given set.
print("The above Given set's maximum value = ", maxim_val)

Output:

Enter some random values separated by spaces = 10 25 68 90 120 5 30 80
The given set is :
{68, 5, 10, 80, 120, 25, 90, 30}
The above Given set's maximum value = 120