Programming Question. PLs tell fast
Answers
A list named L is created.
> L = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
To display 20, 30, 40, 50 from the list, write the statement given below:
> print(L[1 : 5])
Explanation: The required numbers in the list are present in the range (1, 4). They are accessed using list slicing. As last value is excluded, we will write 5 instead of 4.
To display last 5 elements, write the statement given below:
> print(L[-5 : ])
Explanation: Python supports negative indexing. Index of the fifth element of the list from the last is -5. To access them, we wrote L[-5 : ].
Output of print(L[-5 : -1]) is:
> [60, 70, 80, 90]
Explanation: Here, we are accessing last 5 elements. The last element is excluded. So, the output is [60, 70, 80, 90].
List: A list is one of the most widely used data types in Python. It can store multiple items of different types inside a single variable.
Declaration: list_name = () or, list_name = [ ]
Initialization: list_name = [element1, element2, . . .]
Characteristics:
- Ordered.
- Changeable or mutable.
- Allow duplicates.
- Dynamic.
- Can be nested.