Skip to content
This repository has been archived by the owner on Apr 1, 2021. It is now read-only.

Create Python-Function-LEN.md #1030

Merged
6 commits merged into from
May 25, 2016
Merged
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions Python-Function-LEN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Python len(x)

`len()` is a built in function in Python 3. This method returns the length (the number of items) of an object. It takes one argument `x`.
Copy link
Member

Choose a reason for hiding this comment

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

This method returns the length (the number of items) of an object

What do you mean by length of an object?

Copy link

@ghost ghost May 25, 2016

Choose a reason for hiding this comment

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

built in function built-in function

argument x. argument, x.


## Arguments

It takes one argument `x` - argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).
Copy link

Choose a reason for hiding this comment

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

argument x argument, x

argument may This argument may


## Return Value

This function returns the number of elements in the list
Copy link
Member

Choose a reason for hiding this comment

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

This function returns the number of elements in the list

But you just mentioned sequence and collection

Copy link

Choose a reason for hiding this comment

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

list list.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alayek
Object refers to anything which is passed as an argument. It can be a sequence or a collection.
Please refer: http://www.diveintopython.net/getting_to_know_python/everything_is_an_object.html

Copy link
Member

Choose a reason for hiding this comment

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

Yes, in Python, everything is an object. It's too broad. For instance, what would be len(4)? 4 is an int object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its according to the official documentation and that is why I have mentioned that "argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set)"


## Code Sample

```python
list1 = [123, 'xyz', 'zara']
str1 = 'basketball'
print(len(list1)) # prints 3
print(len(str1)) # prints 10
```

:rocket: [Run Code](https://repl.it/CUmt)
Copy link
Member

Choose a reason for hiding this comment

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

Add more example with tuple and dictionaries. Also add comments as to why which value would be printed.


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