Python Lists
Python Lists |
Python knows many types of compound data, which are used to concatenate other values simultaneously. The most versatile is the python list, which can be written as a comma-separated list of values (objects) between square brackets. Python Lists can contain different types of items, but typically, items are all types.
Like strings (and all other underlying sequence types), lists can be indexed and truncated:
All slice operations return a new python list containing the requested elements. This means that the following fragment returns a new (shallow) copy of the list:
The python lists also support operations like concatenation:
Unlike strings, which are immutable, lists are a variable type, meaning it is possible to change their contents:
You can also add new items to the end of the python list using the append () method (we'll see more about the later methods):
It is also possible to assign slices, and this can also change the size of the list or make it completely clean:
The built-in function
len()
also applies to lists to calculate the length of the list:For example, it is possible to create python lists (create lists with other lists):
Python Lists
Reviewed by Sk Jha
on
October 09, 2019
Rating:
No comments: