🐸

The Slice

Python Basicspython-basics-25-the-slice
Reward: 120 XP
|

The Slice

Sometimes Hoppy wants to grab a bunch of items at once. Like grabbing everything from the Map to the Snack.

This is called Slicing. It allows you to get a sub-part of a list.

Slicing Syntax

backpack = ["Map", "Compass", "Snack", "Water", "Rope"]

# Get items from index 1 up to (but NOT including) 3
# Index 1: "Compass"
# Index 2: "Snack"
# Index 3: "Water" (Stop here!)
sub_bag = backpack[1:3] 
# Result: ["Compass", "Snack"]

Your Mission

1
The Scroll

We have a list runes with 5 magical symbols.

2
Extract

Create a new variable secret that contains the runes from index 1 to 4.

3
Reveal

Print the secret list.

Suggested Solution
Click to expand

Remember, the second number is where it stops. So [1:4] gives you indices 1, 2, and 3.

runes = ["A", "B", "C", "D", "E"]
secret = runes[1:4]
print(secret)
Advanced Tips
Want more? Click to expand

If you leave out the first number, it starts from the beginning. If you leave out the second, it goes to the end!

print(backpack[:2]) # First 2 items
print(backpack[2:]) # Everything from index 2 onwards
pymain.py
Loading...
Terminal
Terminal
Ready to run...