This repository has been archived by the owner on Apr 1, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 307
Article: Python-Function-MAX.md #1045
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Python max(x) | ||
|
||
`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. | ||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. - a ValueError is raised
+ a `ValueError` is raised There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done.Please check |
||
|
||
## 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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you say numbers, what do you mean?
int
orfloat
? Or both?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can also compare between Strings, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
It can take both int and floats and it can even compare them together.
It works for strings too. It checks for lexicographical order.
eg: max('abc', 'abb', 'abc') # returns 'abc'