0% found this document useful (0 votes)
8 views3 pages

Python List Slicing Questions

The document contains a series of Python list slicing questions that test the reader's understanding of list slicing syntax and behavior. It includes tasks such as predicting outputs, writing slicing expressions, and explaining specific slicing results. The questions cover various scenarios, including extracting elements, reversing lists, and handling out-of-range indices.

Uploaded by

Aman Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Python List Slicing Questions

The document contains a series of Python list slicing questions that test the reader's understanding of list slicing syntax and behavior. It includes tasks such as predicting outputs, writing slicing expressions, and explaining specific slicing results. The questions cover various scenarios, including extracting elements, reversing lists, and handling out-of-range indices.

Uploaded by

Aman Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python List Slicing Questions

1. What will be the output of the following code?

my_list = [1, 2, 3, 4, 5]

print(my_list[1:4])

2. Write the slicing expression to extract the first three elements from the list my_list = [10, 20, 30,

40, 50].

3. What will the output be for this code?

my_list = [5, 10, 15, 20, 25]

print(my_list[:3])

4. Write the slicing expression to get all elements of the list except the last one.

Example: my_list = [1, 2, 3, 4, 5]

5. Predict the output:

my_list = [2, 4, 6, 8, 10]

print(my_list[2:])

6. Write the slicing expression to reverse the list my_list = [1, 2, 3, 4, 5].

7. Predict the output:

my_list = [10, 20, 30, 40, 50]

print(my_list[-3:-1])

8. What happens if the slicing indices are out of range?

my_list = [1, 2, 3]

print(my_list[1:10])

9. Extract every second element from the list my_list = [1, 2, 3, 4, 5, 6, 7, 8].
10. Write a slicing expression to extract the last three elements of my_list = [5, 10, 15, 20, 25, 30].

11. Predict the output of the following code:

my_list = ['a', 'b', 'c', 'd', 'e']

print(my_list[::2])

12. Extract all elements of the list except the first two using slicing.

Example: my_list = [100, 200, 300, 400, 500]

13. What will be the output?

my_list = [1, 2, 3, 4, 5]

print(my_list[-1:-5:-1])

14. Write a slicing expression to extract every third element from the list starting at index 1.

Example: my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

15. Predict the output of this code:

my_list = [10, 20, 30, 40, 50, 60]

print(my_list[-2::-1])

16. How can you extract the middle three elements from a list my_list = [10, 20, 30, 40, 50, 60, 70]?

17. Write a slicing expression to extract a sublist from index 2 to 8 with a step of 2.

Example: my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

18. Explain what the following slicing expression does:

my_list = [1, 2, 3, 4, 5, 6]

sub_list = my_list[::-2]

19. Write a slicing expression to create a copy of the entire list my_list.

20. Predict the output for the code below:


my_list = [10, 20, 30, 40, 50]

print(my_list[1:4:2])

You might also like