Practice and reinforce the concepts from Lesson 4
Estimated time: 30-40 minutes
:bulb: Remember to run each code cell in order! Use Shift+Enter to run a cell and move to the next one.
:computer: Question One: Working with Number Lists
Time estimate: 10 minutes
Step One: Create a list of multiples of 10
- Create a list containing multiples of ten from 10 to 100
- Assign this list to a variable named
num
python
num = [10,20,30,40,50,60,70,80,90,100]
- Print the list to verify it's correct
Step 2: Find an element's index
- Use the
.index()
method to find where 60 is located- Print the index value
Expected output:
5
Step 3: Modify a list element
- Change the number 60 to 65 using index assignment
- Print the updated list
Expected output:
csharp
[10, 20, 30, 40, 50, 65, 70, 80, 90, 100]
:computer: Question 2: Managing a Games List
Time estimate: 10 minutes
Step One: Create an empty list
- Create an empty list
- Assign it to a variable named
games
pythongames = []
Step 2: Add items to the list
- Use the
.append()
method to add "Minecraft"- Add "Fortnite" using
.append()
- Add "Valorant" using
.append()
- Print the complete list
Expected output:
css
['Minecraft', 'Fortnite', 'Valorant']
Step 3: Remove an item by index
- Find the index of "Fortnite" in your list
- Use
del games[index]
to remove it- Print the updated list tip Remember that list indices start at 0! The first item is at index 0, second at index 1, etc.
Expected output:
['Minecraft', 'Valorant']
in
operator to check if "Fortnite" is still in the listExpected output:
False
Time estimate: 10-15 minutes
range(1, 51)
to create numbers from 1 to 50list()
numbers
:bulb: The
range()
function doesn't include the ending value, so use 51 to get numbers up to 50!
Expected output:
csharp
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
Step 2: Extract a portion using slicing
- Use list slicing to get numbers from 21 to 30
- Remember: the number 21 is at index 20 (since we start counting from 0)
- Assign the slice to a variable named
new_numbers
- Print the new list
Expected output:
csharp
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
Step 3: Add elements at specific positions
- Use
.insert(0, 10)
to add 10 at the beginning- Use
.append(40)
to add 40 at the end- Print the final list
Expected output:
csharp
[10, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 40]
:memo: Submission
:warning: Important! Before submitting:
- Review all your code and outputs
- Make sure all expected outputs match your results
- Save your Colab notebook
:link: Submit your exercise here
:wrench: Troubleshooting
Common issues:
- IndexError: Check that you're using the correct index values
- NameError: Make sure you've run all previous cells in order
- TypeError: Verify you're using the correct methods for lists tip Need help? If you're stuck: