0% found this document useful (0 votes)
24 views5 pages

Map and Slicing

This document provides a series of exercises for students to practice slicing and mapping in Python. It includes three parts: slicing exercises, mapping exercises, and combined exercises that integrate both concepts. Each exercise is accompanied by examples to illustrate the expected output.

Uploaded by

Nourhene Aouidi
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)
24 views5 pages

Map and Slicing

This document provides a series of exercises for students to practice slicing and mapping in Python. It includes three parts: slicing exercises, mapping exercises, and combined exercises that integrate both concepts. Each exercise is accompanied by examples to illustrate the expected output.

Uploaded by

Nourhene Aouidi
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/ 5

Here are some exercises to help students practice slicing and mapping in Python.

Part 1: Slicing Exercises

1. Reverse a List:
a. Write a program that takes a list of numbers as input and prints the list in
reverse order using slicing.

Example:

numbers = [1, 2, 3, 4, 5]
# Output should be [5, 4, 3, 2, 1]

2. Extract Even Indexed Elements:


a. Write a program that takes a list of numbers as input and prints only the
elements at even indices (0, 2, 4, etc.) using slicing.

Example:

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


# Output should be [10, 30, 50]

3. Extract a Sublist:
a. Given a list of numbers, extract and print a sublist from index 2 to index 4
(inclusive) using slicing.

Example:

numbers = [100, 200, 300, 400, 500, 600]


# Output should be [300, 400, 500]

4. Remove the First and Last Elements:


a. Write a program that removes the first and last element from a list of
numbers using slicing, then print the result.

Example:

numbers = [1, 2, 3, 4, 5, 6]
# Output should be [2, 3, 4, 5]
5. Create a New List with Every Other Element:
a. Write a program that takes a list of numbers as input and creates a new
list that contains every other element from the original list using slicing.

Example:

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


# Output should be [10, 30, 50]

Part 2: Mapping Exercises

1. Square Each Element:


a. Write a program that takes a list of numbers as input and creates a new
list that contains the square of each number using the map() function.

Example:

numbers = [1, 2, 3, 4, 5]
# Output should be [1, 4, 9, 16, 25]

2. Convert All Strings to Uppercase:


a. Write a program that takes a list of strings and converts each string to
uppercase using the map() function.

Example:

words = ["hello", "world", "python"]


# Output should be ['HELLO', 'WORLD', 'PYTHON']

3. Add 10 to Each Element:


a. Write a program that takes a list of numbers as input and creates a new
list with 10 added to each number using the map() function.

Example:

numbers = [1, 2, 3, 4, 5]
# Output should be [11, 12, 13, 14, 15]

4. Calculate the Length of Each String:


a. Write a program that takes a list of strings and creates a new list with the
length of each string using the map() function.

Example:

words = ["apple", "banana", "cherry"]


# Output should be [5, 6, 6]

5. Extract the First Character of Each String:


a. Write a program that takes a list of strings and creates a new list
containing the first character of each string using the map() function.

Example:

words = ["apple", "banana", "cherry"]


# Output should be ['a', 'b', 'c']

Part 3: Combined Exercises (Slicing + Mapping)

1. Reverse and Square Each Element:


a. Write a program that takes a list of numbers as input. First, reverse the
list using slicing, then square each element of the reversed list using
map().

Example:

numbers = [1, 2, 3, 4, 5]
# Output should be [25, 16, 9, 4, 1]

2. Extract Even Indexed Elements and Double Them:


a. Write a program that takes a list of numbers as input. Extract the
elements at even indices using slicing, then double each element using
map().

Example:

numbers = [1, 2, 3, 4, 5, 6]
# Output should be [2, 6, 10]
3. Apply a Function to Each Slice:
a. Given a list of numbers, extract every other element (use slicing) and then
apply a function that subtracts 5 from each number using map().

Example:

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


# Output should be [5, 25, 45]

4. Create a List of Lengths of Substrings:


a. Write a program that takes a string as input, slices the string into
substrings of length 3, and then calculates the length of each substring
using map().

Example:

word = "abcdefghijk"
# Output should be [3, 3, 3, 3, 3]

5. Apply Mapping on Sliced Lists:


a. Write a program that takes a list of numbers as input, slices it into two
halves, and then adds 5 to each element in both halves using map().

Example:

numbers = [1, 2, 3, 4, 5, 6]
# Output should be [6, 7, 8] for the first half and [9, 10, 11] for
the second half

Bonus: Challenge Exercise

• Slicing and Mapping with Multiple Lists:


o Given two lists, slice the first list to get every other element, and slice the
second list to get the last three elements. Then, use map() to add
corresponding elements from both lists.

Example:

list1 = [1, 2, 3, 4, 5, 6]
list2 = [10, 20, 30, 40, 50, 60]
# Output should be [11, 33, 55] (after applying map to sliced lists)

You might also like