A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://realpython.com/ref/builtin-functions/max/ below:

Python’s Built-in Functions – Real Python

The built-in max() function allows you to find the largest values in an iterable or among multiple arguments. This function is versatile and can handle various data types, including numbers, strings, and more:

max() Signatures Arguments Argument Description Default Value iterable An iterable object, such as a list, tuple, dictionary, or string. Required argument default A value to return if the iterable is empty. Keyword-only argument key A single-argument function to customize the comparison criteria. None *args An undefined number of arguments. - Return Value max() Examples

With a list of numeric values as an argument:

With a string as an argument:

With multiple string arguments:

max() Common Use Cases

The most common use cases for the max() functions include:

max() Real-World Example

Suppose you have a dictionary of product prices and want to identify the most expensive product. You can use max() with the .items() method and a lambda function as the key argument:

This example demonstrates how max() can help you identify the most expensive products by using the dictionary’s values for comparison.

max() in Custom Classes

You can support max() in custom classes by implementing the .__gt__() special method. Here’s a quick example:

This implementation allows you to use max() on instances of the Person class, comparing them by birthdate.

Tutorial

Python's min() and max(): Find Smallest and Largest Values

In this tutorial, you'll learn how to use Python's built-in min() and max() functions to find the smallest and largest values. You'll also learn how to modify their standard behavior by providing a suitable key function. Finally, you'll code a few practical examples of using min() and max().

basics python

For additional information on related topics, take a look at the following resources:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4