Python built-in functions max (), min (), pythonmin

Source: Internet
Author: User

Python built-in functions max (), min (), pythonmin

max(Iterable,*[,Key,Default])

max(Arg1,Arg2,* Args[,Key])

The function is used to obtain the maximum values of multiple input parameters or the maximum values of the input iteratable object elements. The default value is Numeric. The value is greater than the default value. The value is sorted by the alphabetic order by the latter. You can also pass in the named parameter key as a function to specify the method to take the maximum value. The default naming parameter is used to specify the default value returned when the maximum value does not exist.

Eg

A. Maximum Value of multiple input parameters

print(max(1,2,3,4))

Output

4

B .1 when an iteratable object is passed in, the maximum value of its element is obtained.

s = '12345'print(max(s))

Output

5

B .2 when an iteratable object is null, you must specify the default parameter to return the default value.

print(max((),default=1))print(max(()))

Output

1 Traceback (most recent call last): File "G:/pycode/built-in functions/max_ex.py", line 8, in <module> print (max () ValueError: max () arg is an empty sequence

C. input the named parameter key as a function to specify the method for obtaining the maximum value.

s = [    {'name': 'sumcet', 'age': 18},    {'name': 'bbu', 'age': 11}]a = max(s, key=lambda x: x['age'])print(a)

Output

{'name': 'sumcet', 'age': 18}

Min () function and max () function use the same, return the minimum value

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.