Python Exercises For Beginners
Python programming exercises with solutions for beginners.
On this page
1. List Slicing
Given a list of 10 elements.
l = list(range(10))
- Slice first five elements
- Slice last five elements
- Slice element 2 to 5 index. (include index 5 element)
- Create a copy of list using list
- Reverse the list using list slicing
- Filter odd index elements
- Filter even index elements
Hint
use[a: b: c]
operation to slice a list.Solution
- Slice first five elements
first_five = l[:5]
- Slice last five elements
last_five = l[5:]
- Slice element 2 to 5 index
elements_2_to_5 = l[2:6]
- Create a copy of list using list
l_copy = l[:]
- Reverse the list using list slicing
l_reverse = l[::-1]
- Filter odd index elements
l_odd = l[1::2]
- Filter even index elements
l_odd = l[::2]