A list consists of items separated by commas and enclosed within square brackets ([]). Lists are similar to arrays. Actually, an array contains values of a similar data type, a list, on the other hand, can have to belong to different data types.
The values stored in a list are accessed using indexes. The index of the first element is 0, and n-1 is the index of the last element, where n is the number of elements in the list.
We can use slice, concatenation, and repetition operations on lists.
Python List Example
a=[10,"john",20,90] b=[40,"smith",56] print(a) print(a[0]) # Prints first element of the list print(a[1:3]) # Prints elements starting from 2nd element to 3rd element print(a[:3]) # Prints elements starting from first element to 3rd element print(b*2) # Repeats the list print(a+b) # Concatenates two lists
Output:
[10, 'john', 20, 90] 10 ['john', 20] [10, 'john', 20] [40, 'smith', 56, 40, 'smith', 56] [10, 'john', 20, 90, 40, 'smith', 56]
Leave a comment