Python 3 – Number max() Method
The max()
method is a built-in function in Python3 that allows us to easily determine the maximum value among the inputs passed. This method returns the largest item in an iterable, or the largest of two or more arguments passed.
Here is the sample code to use the max() function to find the maximum value between two numbers:
num1 = 10
num2 = 25
max_num = max(num1, num2)
print(max_num)
Output:
25
In the above code snippet, we have two numbers num1
and num2
, and we use the max()
method to find the maximum value between them. The output of the program will be 25
, which is the maximum value between the two numbers.
Syntax of max() method
The syntax of the max()
method is quite simple. It takes one or more arguments and returns the largest value among the inputs passed.
The syntax of the max() method is as follows:
max(Iterable[, key=func]) => value
Here, the Iterable
parameter is mandatory which should be any iterable object such as List, Tuple, or Set. The key
parameter is optional, which should be a function that takes a single value and returns a value to compare.
Using the max() method with Lists
We can also use the max()
method with lists to find the maximum value in a list. Here is an example of using the max()
method to find the maximum value in a list of numbers:
num_list = [10, 20, 30, 40, 50]
max_num = max(num_list)
print(max_num)
Output:
50
In the above code snippet, we have a list of numbers called num_list
, and we use the max()
method to find the maximum value in the list. The output of the program will be 50
, which is the maximum value in the list.
Using the max() method with Tuples
We can also use the max()
method with tuples to find the maximum value in a tuple. Here is an example of using the max()
method to find the maximum value in a tuple of numbers:
num_tuple = (10, 20, 30, 40, 50)
max_num = max(num_tuple)
print(max_num)
Output:
50
In the above code snippet, we have a tuple of numbers called num_tuple
, and we use the max()
method to find the maximum value in the tuple. The output of the program will be 50
, which is the maximum value in the tuple.
Using the max() method with Sets
We can also use the max()
method with sets to find the maximum value in a set. Here is an example of using the max()
method to find the maximum value in a set of numbers:
num_set = {10, 20, 30, 40, 50}
max_num = max(num_set)
print(max_num)
Output:
50
In the above code snippet, we have a set of numbers called num_set
, and we use the max()
method to find the maximum value in the set. The output of the program will be 50
, which is the maximum value in the set.
Using the max() method with key function
We can also use the max()
method with a key
function to find the maximum value based on some specific criteria. Here is an example of using the max()
method with a key
function to find the longest string in a list of strings:
string_list = ["Python", "Java", "JavaScript", "C", "C++"]
longest_string = max(string_list, key=len)
print(longest_string)
Output:
JavaScript
In the above code snippet, we have a list of strings called string_list
, and we use the max()
method with a key
function to find the longest string in the list. The output of the program will be JavaScript
, which is the longest string in the list.
Conclusion
In conclusion, the max()
method is a handy built-in function in Python3 that allows us to easily determine the maximum value among the inputs passed. We can use this method with iterable objects like lists, tuples, and sets to find the maximum value of these objects. Additionally, we can use a key
function to find the maximum value based on some specific criteria. Overall, the max()
method is a handy tool to have in our Python programming arsenal.