From 3d63c7e10a5671e2d8af1b3b0e7a3f42a1c2fd5a Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Fri, 27 May 2016 16:03:53 +0530 Subject: [PATCH 1/3] Create Python-Function-MAX.md --- Python-Function-MAX.md | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Python-Function-MAX.md diff --git a/Python-Function-MAX.md b/Python-Function-MAX.md new file mode 100644 index 0000000000..78b7b589a9 --- /dev/null +++ b/Python-Function-MAX.md @@ -0,0 +1,49 @@ +# Python max(x) + +`max()` is a built-in function in Python 3. It returns the smallest item in an iterable or the smallest of two or more arguments. + +## Arguments +This function takes two or more numbers or any kind of iterable as an argument. While giving an iterable as an argument we must make sure that all the elements in the iterable are of the same type. This means that we cannot pass a list which has both string and integer values stored in it. + +Valid Arguments: +```python +max(2, 3) +max([1, 2, 3]) +max('a', 'b', 'c') +``` +Invalid Arguments: +```python +max(2, 'a') +max([1, 2, 3, 'a']) +max([]) +``` + +## Return Value + +The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments +is returned. If the iterable is empty and default is not provided, a ValueError is raised. + +## Code Sample + +```python +print(max(2, 3)) # Returns 3 as 3 is the largest of the two values +print(max(2, 3, 23)) # Returns 23 as 23 is the largest of all the values + +list1 = [1, 2, 4, 5, 54] +print(max(list1)) # Returns 54 as 54 is the largest value in the list + +list2 = ['a', 'b', 'c' ] +print(max(list2)) # Returns 'c' as 'c' is the largest in the list in alphabetical order + +list3 = [1, 2, 'abc', 'xyz'] +print(max(list3)) # Gives TypeError as values in the list are of different type + +#Fix the TypeError mentioned above first before moving on to next step + +list4 = [] +print(max(list4)) # Gives ValueError as the argument is empty +``` + +:rocket: [Run Code](https://repl.it/CVok) + +[Official Docs](https://docs.python.org/3/library/functions.html#max) From 6f5f2942d6b62d24b2d0fa8db86c6ac938c9b714 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Fri, 27 May 2016 16:06:59 +0530 Subject: [PATCH 2/3] Update Python-Function-MAX.md --- Python-Function-MAX.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python-Function-MAX.md b/Python-Function-MAX.md index 78b7b589a9..5ce529e6ec 100644 --- a/Python-Function-MAX.md +++ b/Python-Function-MAX.md @@ -1,6 +1,6 @@ # Python max(x) -`max()` is a built-in function in Python 3. It returns the smallest item in an iterable or the smallest of two or more arguments. +`max()` is a built-in function in Python 3. It returns the largest item in an iterable or the largest of two or more arguments. ## Arguments This function takes two or more numbers or any kind of iterable as an argument. While giving an iterable as an argument we must make sure that all the elements in the iterable are of the same type. This means that we cannot pass a list which has both string and integer values stored in it. From 0691f5083a51a84c3c6cedd99d171c9b2a4d8bf3 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Fri, 27 May 2016 16:19:44 +0530 Subject: [PATCH 3/3] Update Python-Function-MAX.md --- Python-Function-MAX.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python-Function-MAX.md b/Python-Function-MAX.md index 5ce529e6ec..e5a58578c1 100644 --- a/Python-Function-MAX.md +++ b/Python-Function-MAX.md @@ -21,7 +21,7 @@ max([]) ## Return Value The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments -is returned. If the iterable is empty and default is not provided, a ValueError is raised. +is returned. If the iterable is empty and default is not provided, a `ValueError` is raised. ## Code Sample