Lists

Lists

In variables, one name can contain one value. But when you need to store many values with one name, we use lists. To create a list we use square brackets[] with comma-separated values. A list can contain different types of values.

-------------------------------------------------------------

Code: -

myList = [5,8,"python",True,60]

print(myList)

Output: -

[5,8,"python",True,60]

--------------------------------------------------------------

To access a specific value from a list we use square brackets after the name of the list and put the index number in the bracket.

-------------------------------------------------------------

Code: -

myList = [5,8,"python",True,60]

print(myList[2])

Output: -

python

--------------------------------------------------------------

***Note that list indexing starts from 0 and not from 1***

The same way one can change the value of an index in a list.

-------------------------------------------------------------

Code: -

myList = [5,8,"python",True,60]

myList[2] = Coding

print(myList[2])

Output: -

Coding

--------------------------------------------------------------

List slicing is the same as string slicing.


Comments

Post a Comment

Popular Posts