Skip to content

Slices


Related 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.
 

Exercises

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

Nested loops
Multiple return

Be First to Comment

    Leave a Reply