(1) In Python, the most basic data structure is the list. The list is similar to an array as in C, C++, or Java since the index of the first element of the list is zero, the second element is one, and so on. However, the list is a collection of heterogeneous data elements. That means a list contains numeric as well as character data. Elements of a list are enclosed in square brackets and are separated by commas. In Python, lists are mutable. It means that the contents of the list can be changed after it has been created. Like strings, the slicing operation can also be applied to lists.
(2) Accessing Elements in a List.
The first print command shows the index number of the values in the list which need to be displayed as output.
The second print command 0:4 shows we need four values from the start.
In the third print command 0::3, we need an increment of 3 in the values from the start.
The fourth print command 4: define we need all values that start from the index value 4.
The fifth print command -1::-3 is similar to the third print command but it is the case of obtaining reverse values.
The last print command -1::-1 reverses all values.
Comments (No)