Added example on 2d lists and changed format (#23665)
updated the code format along with an example of 2d list.
This commit is contained in:
@ -12,18 +12,31 @@ It takes one argument, `x`. This argument may be a sequence (such as a string, b
|
|||||||
This function returns the number of elements in the argument which is passed to the `len()` function.
|
This function returns the number of elements in the argument which is passed to the `len()` function.
|
||||||
|
|
||||||
## Code Sample
|
## Code Sample
|
||||||
|
```py
|
||||||
list1 = [123, 'xyz', 'zara'] # list
|
>>> list1 = [123, 'xyz', 'zara'] # list
|
||||||
print(len(list1)) # prints 3 as there are 3 elements in the list1
|
>>> print(len(list1)) # prints 3 as there are 3 elements in the list1
|
||||||
|
3
|
||||||
str1 = 'basketball' # string
|
```
|
||||||
print(len(str1)) # prints 10 as the str1 is made of 10 characters
|
```py
|
||||||
|
>>> str1 = 'basketball' # string
|
||||||
tuple1 = (2, 3, 4, 5) # tuple
|
>>> print(len(str1)) # prints 10 as the str1 is made of 10 characters
|
||||||
print(len(tuple1)) # prints 4 as there are 4 elements in the tuple1
|
10
|
||||||
|
```
|
||||||
dict1 = {'name': 'John', 'age': 4, 'score': 45} # dictionary
|
```py
|
||||||
print(len(dict1)) # prints 3 as there are 3 key and value pairs in the dict1
|
>>> tuple1 = (2, 3, 4, 5) # tuple
|
||||||
|
>>> print(len(tuple1)) # prints 4 as there are 4 elements in the tuple1
|
||||||
|
4
|
||||||
|
```
|
||||||
|
```py
|
||||||
|
>>> dict1 = {'name': 'John', 'age': 4, 'score': 45} # dictionary
|
||||||
|
>>> print(len(dict1)) # prints 3 as there are 3 key and value pairs in the dict1
|
||||||
|
3
|
||||||
|
```
|
||||||
|
```py
|
||||||
|
>>> d2=[[1,2,3],[3,4,5],[5,6,7,2]] # 2 dimensional list or a list of lists
|
||||||
|
>>> print(len(d2)) #prints 3 because there are 3 different lists in the outer list.
|
||||||
|
3
|
||||||
|
```
|
||||||
|
|
||||||
 <a href='https://repl.it/CUmt/15' target='_blank' rel='nofollow'>Run Code</a>
|
 <a href='https://repl.it/CUmt/15' target='_blank' rel='nofollow'>Run Code</a>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user