Python Collections
List
In Python, list is one of the basic data structure useful to store multiple data items. The lists are ordered and have a definite count.Lists are mutable sequences, typically used to store collections of homogeneous items. But lists can also be used to store items with different data type. The elements in a list are identified by it's index number. Index number begins with 0.
Creating list in Python
Following code will create the lists with names: squares and banks.
# List of numbers
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
# List of words
>>> banks = ['sbi', 'hdfc', 'icici']
>>> banks
['sbi', 'hdfc', 'icici']
Accessing values from a List
Following code can be used to access values from a list.
>>> banks = ['sbi', 'hdfc', 'icici']
>>> banks
['sbi', 'hdfc', 'icici']
# prints first value from a List
# prints first value from a List
>>> print(banks[0]
sbi
sbi
< Brief note on Python Collections >
< Read more about Python List >
For more details, you can refer detailed documentation available at URL: https://docs.python.org/3/library/stdtypes.html
No comments:
Post a Comment