Thursday, June 11, 2020

More about Lists.....


The list starts with an open square bracket and ends with a closed square bracket.

The elements inside a list may have different types. Some of them may be integers, others floats and yet others may be lists.

Python has adopted a convention stating that the elements in a list are always numbered starting from zero. This means that the item stored at the beginning of the list will have the number zero.


Indexing Lists

The value inside the brackets which selects one element of the list is called an index, while the operation of selecting an element from the list is known as indexing.









And now we want the value of the fifth element to be copied to the second element.






Accessing list content





The len( ) function


Removing elements from a list
Here we used the keyword “del”. It’s an instruction, not a function.







We can't access an element which doesn't exist - we can neither get its value nor assign it a value. Both of these instructions will cause runtime errors now.












Negative Indices are legal
In lists, negative indices are also legal. See the below example.












Next Example,












Output is



No comments:

Post a Comment