Skip to content

Exercise 14 : Slices

Alternative course

Slices

A slice can be taken from a string or list, just as you can take a slice from a pizza.
Python starts numbering of string and list elements from zero.

Slice Example:

#!/usr/bin/python
persons = [ "John", "Marissa", "Pete", "Dayton" ]

slice = persons[0:2]
print(slice)

What you see on success

The program above should show a slice of the list persons.
 
python slice

Study drill

Try the exercises below

1. Take a slice of the list below:
   pizzas = ["Hawai","Pepperoni","Fromaggi","Napolitana","Diavoli"]

2. Given the text "Hello World", take the slice "World"

After completing these continue with the next exercise.

Answers

Exercise 13 : Nested loops
Exercise 15: Multiple return
Published inUncategorized

Be First to Comment

    Leave a Reply