Python Max() - Programiz

Tutorials Courses Python JavaScript TypeScript SQL HTML CSS C C++ Java R Ruby RUST Golang Kotlin Swift C# DSA

Become a certified Python programmer.

ENROLL

Popular Tutorials

Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning Python

Popular Examples

Add two numbers Check prime number Find the factorial of a number Print the Fibonacci sequence Check leap year Explore Python Examples

Reference Materials

Built-in Functions List Methods Dictionary Methods String Methods View all Programiz Pro Logo

Created with over a decade of experience.

  • Learn
  • Practice
  • Compete
Learn Python Learn HTML Learn JavaScript Learn SQL Learn DSA Learn C Learn C++ Learn Java View all Courses on Programiz Pro Logo Python Basics Python Intermediate C++ Basics C++ Intermediate C++ OOP C Programming Java Basics Java Intermediate Java OOP View all Courses on Programiz Pro Logo Python Challenges JavaScript Challenges Java Challenges C++ Challenges C Challenges View all Challenges on Programiz Pro Logo Learn Practice Compete

Certification Courses

Created with over a decade of experience and thousands of feedback.

Learn Python Learn HTML Learn JavaScript Learn SQL Learn DSA View all Courses on Programiz Pro Logo Learn C Learn C++ Learn Java Python JavaScript TypeScript SQL HTML CSS C C++ Java More languages

Become a certified Python programmer.

Try Programiz PRO!

Popular Tutorials

Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning Python All Python Tutorials

Reference Materials

Built-in Functions List Methods Dictionary Methods String Methods View all Python JavaScript C C++ Java R Kotlin

Become a certified Python programmer.

Try Programiz PRO!

Popular Examples

Add two numbers Check prime number Find the factorial of a number Print the Fibonacci sequence Check leap year All Python Examples

Built-in Functions

  • Python abs()
  • Python any()
  • Python all()
  • Python ascii()
  • Python bin()
  • Python bool()
  • Python bytearray()
  • Python callable()
  • Python bytes()
  • Python chr()
  • Python compile()
  • Python classmethod()
  • Python complex()
  • Python delattr()
  • Python dict()
  • Python dir()
  • Python divmod()
  • Python enumerate()
  • Python staticmethod()
  • Python filter()
  • Python eval()
  • Python float()
  • Python format()
  • Python frozenset()
  • Python getattr()
  • Python globals()
  • Python exec()
  • Python hasattr()
  • Python help()
  • Python hex()
  • Python hash()
  • Python input()
  • Python id()
  • Python isinstance()
  • Python int()
  • Python issubclass()
  • Python iter()
  • Python list() Function
  • Python locals()
  • Python len()
  • Python max()
  • Python min()
  • Python map()
  • Python next()
  • Python memoryview()
  • Python object()
  • Python oct()
  • Python ord()
  • Python open()
  • Python pow()
  • Python print()
  • Python property()
  • Python range()
  • Python repr()
  • Python reversed()
  • Python round()
  • Python set()
  • Python setattr()
  • Python slice()
  • Python sorted()
  • Python str()
  • Python sum()
  • Python tuple() Function
  • Python type()
  • Python vars()
  • Python zip()
  • Python __import__()
  • Python super()

Python Tutorials

  • Python min()
  • Python map() Function
  • Python zip()
  • Python Programming Built-in Functions
  • Python sum()
  • Python String join()
Python max()

The max() function returns the largest item in an iterable. It can also be used to find the largest item between two or more parameters.

Example

numbers = [9, 34, 11, -4, 27] # find the maximum number max_number = max(numbers) print(max_number) # Output: 34

The max() function has two forms:

# to find the largest item in an iterable max(iterable, *iterables, key, default) # to find the largest item between two or more objects max(arg1, arg2, *args, key)

1. max() with iterable arguments

max() Syntax

To find the largest item in an iterable, we use this syntax:

max(iterable, *iterables, key, default)

max() Parameters

  • iterable - an iterable such as list, tuple, set, dictionary, etc.
  • *iterables (optional) - any number of iterables; can be more than one
  • key (optional) - key function where the iterables are passed and comparison is performed based on its return value
  • default (optional) - default value if the given iterable is empty

max() Return Value

max() returns the largest element from an iterable.

Example 1: Get the largest item in a list

number = [3, 2, 8, 5, 10, 6] largest_number = max(number); print("The largest number is:", largest_number)

Output

The largest number is: 10

If the items in an iterable are strings, the largest item (ordered alphabetically) is returned.

Example 2: the largest string in a list

languages = ["Python", "C Programming", "Java", "JavaScript"] largest_string = max(languages); print("The largest string is:", largest_string)

Output

The largest string is: Python

In the case of dictionaries, max() returns the largest key. Let's use the key parameter so that we can find the dictionary's key having the largest value.

Example 3: max() in dictionaries

square = {2: 4, -3: 9, -1: 1, -2: 4} # the largest key key1 = max(square) print("The largest key:", key1) # 2 # the key whose value is the largest key2 = max(square, key = lambda k: square[k]) print("The key with the largest value:", key2) # -3 # getting the largest value print("The largest value:", square[key2]) # 9

Output

The largest key: 2 The key with the largest value: -3 The largest value: 9

In the second max() function, we have passed a lambda function to the key parameter.

key = lambda k: square[k]

The function returns the values of dictionaries. Based on the values (rather than the dictionary's keys), the key having the maximum value is returned.

Notes:

  • If we pass an empty iterator, a ValueError exception is raised. To avoid this, we can pass the default parameter.
  • If we pass more than one iterators, the largest item from the given iterators is returned.

2. max() without iterable

max() Syntax

To find the largest object between two or more parameters, we can use this syntax:

max(arg1, arg2, *args, key)

max() parameters

  • arg1 - an object; can be numbers, strings, etc.
  • arg2 - an object; can be numbers, strings, etc.
  • *args (optional) - any number of objects
  • key (optional) - key function where each argument is passed, and comparison is performed based on its return value

Basically, the max() function finds the largest item between two or more objects.

max() Return Value

max() returns the largest argument among the multiple arguments passed to it.

Example 4: Find the maximum among the given numbers

# find max among the arguments result = max(4, -5, 23, 5) print("The maximum number is:", result)

Output

The maximum number is: 23

If you need to find the smallest item, you can use the Python min() function.

Before we wrap up, let’s put your knowledge of Python max() to the test! Can you solve the following challenge?

Challenge:

Write a function to find the largest number in a list.

  • For example, for inputs [1, 2, 3, 4, 5], the output should be 5.
Check Code Previous Tutorial: Python len() Next Tutorial: Python min() Share on: Did you find this article helpful?

Sorry about that.

How can we improve it? Feedback * Leave this field blank

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community

Python References

Python Library

Python min()

Python Library

Python map() Function

Python Library

Python list()

Python Library

Python zip()

Từ khóa » Hàm Max Trong Python