• Python

Command Palette

Search for a command to run...

Native Functions
  • Python Abs Function
  • Python Any All Functions
  • Python Zip Function
  • Python Sum Function
  • Python Filter Function
  • Python Max Min Functions
  • Python Map Function
  • Python Round Function
  • Python Sorted Function
Data Types
  • Python Integers
  • Python Floats
  • Python Complex Numbers
  • Python List Type
  • Python Tuple Type
  • Python String Type
  • Python Range Type
Collections.abc Module
  • Python Containers
  • Python Hashable
  • Python Iterable
  • Python Iterators
  • Python Sequence
  • Python Mutable Sequence

Create an Account

FREE

Join our community to access more courses.

Create Account

On this page

What do max() and min() do?Basic SyntaxExamples with numbers, strings, listsUsing the key parameterUsing the default parameterSummary
      • Blog

      Python Max Min Functions

      What do max() and min() do?

      • max() returns the largest item in an iterable or among two or more arguments.
      • min() returns the smallest item in an iterable or among two or more arguments.

      Both can be customized with a key function and a default value.

      Basic Syntax

      max(iterable, *[, key, default])
      min(iterable, *[, key, default])
      max(arg1, arg2, *args[, key])
      min(arg1, arg2, *args[, key])
      
      • iterable: The sequence to search.
      • key: (Optional) Function to customize comparison.
      • default: (Optional) Value to return if the iterable is empty.

      Examples with numbers, strings, lists

      numbers = [3, 1, 4, 2]
      print(max(numbers))  # Output: 4
      print(min(numbers))  # Output: 1
      
      words = ["apple", "banana", "cherry"]
      print(max(words))  # Output: cherry (alphabetical order)
      print(min(words))  # Output: apple
      

      Using the key parameter

      words = ["apple", "banana", "cherry"]
      print(max(words, key=len))  # Output: banana
      print(min(words, key=lambda w: w[-1]))  # Output: banana (by last letter)
      

      Using the default parameter

      empty = []
      print(max(empty, default=0))  # Output: 0
      

      Summary

      • max() and min() find the largest/smallest item in an iterable or among arguments.
      • Use key for custom comparison and default for empty iterables.
      • Work with numbers, strings, and more.

      Continue Learning

      Python Variables

      Popular

      Getting Started: Understanding Variables in Python In programming, we often need to store informatio

      Python Strings

      For You

      Working with Text: Understanding Strings in Python Text is everywhere in the world of programming. W

      Python Type Conversion

      For You

      Changing Data Types: Understanding Type Conversion In Python, data has different types like whole nu

      Personalized Recommendations

      Log in to get more relevant recommendations based on your reading history.