Skip to content
This repository was archived by the owner on Apr 1, 2021. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Python-Function-MIN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Python min(x)

`min()` 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
min(2, 3)
min([1, 2, 3])
min('a', 'b', 'c')
```
Invalid Arguments:
```python
min(2, 'a')
min([1, 2, 3, 'a'])
min([])
```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please provide some examples of valid and invalid arguments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

## Return Value

The smallest item in the iterable is returned. If two or more positional arguments are provided, the smallest of the positional arguments
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is smaller decided? For numbers and strings it's straightforward, but what if I pass a list of complex objects, like exam results, and I was to pick based on value in one field. Would min() be the right function for that?

Copy link
Contributor Author

@varunu28 varunu28 May 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alayek
Are you looking to pass a dictionary as an argument?

If we pass something as d = {320:1, 321:0, 322:3} as an argument and then use min(d, key=d.get), we would get 321 as output as this key has the lowest value corresponding to it.

is returned. If the iterable is empty and default is not provided, a ValueError is raised.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't change lines in the middle of a sentence.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atjonathan I checked the page in its rendered form - the break is not coming up there.


## Code Sample

```python
print(min(2, 3)) # Returns 2 as 2 is the smallest of the two values
print(min(2, 3, -1)) # Returns -1 as -1 is the smallest of the two values

list1 = [1, 2, 4, 5, -54]
print(min(list1)) # Returns -54 as -54 is the smallest value in the list

list2 = ['a', 'b', 'c' ]
print(min(list2)) # Returns 'a' as 'a' is the smallest in the list in alphabetical order

list3 = [1, 2, 'abc', 'xyz']
print(min(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(min(list4)) # Gives ValueError as the argument is empty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the previous one gives TypeError, the user would never see the ValueError here. So maybe leave an instruction here that one needs to fix the earlier one, and then run again to see this error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

```

:rocket: [Run Code](https://repl.it/CVir/4)

[Official Docs](https://docs.python.org/3/library/functions.html#min)