TalentJR

🐍 Python for kids · Class 7 · Lesson 3 of 18 · Free

Lists – Slicing, Methods & Functions

⏱ About 30 minutes · ▶ run real Python here — nothing to install · no sign-in

In this lesson you will

  • Cut out parts of a list with slicing, like marks[1:4].
  • Change a list with append, insert, pop, remove and sort.
  • Write functions that take a list and return a new result.

Your class WhatsApp group has a list of names that keeps changing. Someone joins, someone leaves, and the admin sorts it A to Z. Python lists can do all of that with one short command each.

How do I get part of a list with slicing?

In Class 6 you used one index, like names[0]. A slice uses two: list[start:stop].

Like range(), the slice starts at start and stops just before stop. It gives back a new list.

Leave out start to begin at the front. Leave out stop to go to the end. Negative numbers count from the back.

🐍 Python — you can edit this code
Output
['Tue', 'Wed', 'Thu']
['Mon', 'Tue']
['Fri', 'Sat']
['Thu', 'Fri', 'Sat']
['Mon', 'Wed', 'Fri']

💡 The third number is a step, just like in range(). days[::2] takes every second day.

How do I add items to a Python list?

append(x) adds one item at the end. It is the one you will use most.

insert(i, x) puts an item at position i. The items after it shift one place to the right.

extend(other) adds every item of another list. It joins two lists into one.

🎬 How Python thinks — step 1 of 7Python is about to run line 1
▶team = ["Rohit", "Virat"]
2team.append("Bumrah")
3team.insert(1, "Gill")
4print(team)
5team.extend(["Jadeja", "Pant"])
6print(team, len(team))
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
['Rohit', 'Gill', 'Virat', 'Bumrah']
['Rohit', 'Gill', 'Virat', 'Bumrah', 'Jadeja', 'Pant'] 6

How do I remove items from a list?

remove(x) deletes the first item equal to x. If x is not there, Python raises an error.

pop() takes the last item out and gives it back to you. pop(i) does the same for position i.

So use remove when you know the value, and pop when you know the position.

pop() is like a stack of plates in the canteen. The last plate put on top comes off first.

🎬 How Python thinks — step 1 of 7Python is about to run line 1
▶bag = ["pen", "eraser", "ruler", "pencil"]
2bag.remove("ruler")
3last = bag.pop()
4first = bag.pop(0)
5print("Took out:", last, "and", first)
6print("Still in bag:", bag)
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Took out: pencil and pen
Still in bag: ['eraser']

What is the difference between sort() and sorted()?

sort() changes the list itself and gives back None. The old order is gone.

sorted(list) leaves the list alone and gives back a new, sorted copy.

Both take reverse=True to sort from biggest to smallest. Use sorted() when you still need the original order.

🐍 Python — you can edit this code
Output
Original: [72, 95, 58, 88]
Ranked: [95, 88, 72, 58]
After sort(): [58, 72, 88, 95]
Count of 88: 1 Position of 88: 2

How do I pass a list to a function in Python?

A list can be an argument, just like a number. The function can loop over it and return an answer.

Here, one function returns the average. Another builds a new list of only the passing marks.

Building a new list is a common pattern: start with [] and append inside a loop.

Notice that passed() has a default pass mark of 35. A different school can call passed(test, 40).

🐍 Python — you can edit this code
Output
Average: 53.0
Passed: [45, 78, 90]

Why does changing a list inside a function change it outside too?

A list variable does not hold a copy. It points to one list, like a name tag on one box.

When you pass a list to a function, the function gets a tag on the same box. append() changes that box.

If you want a separate copy, use list.copy() or a full slice like marks[:].

This is useful, but it can surprise you. Always ask: should this function change my list, or not?

🎬 How Python thinks — step 1 of 10Python is about to run line 1
▶def add_bonus(scores):
2 scores.append(100)
3
4mine = [60, 70]
5add_bonus(mine)
6print("mine:", mine)
7backup = mine.copy()
8backup.append(5)
9print("mine:", mine)
10print("backup:", backup)
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
mine: [60, 70, 100]
mine: [60, 70, 100]
backup: [60, 70, 100, 5]

🤔 Predict the output

Think like Python: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

nums = [10, 20, 30, 40, 50]
print(nums[1:3])

Question 2. What does this print?

a = [3, 1, 2]
b = a
b.append(4)
print(a)

Question 3. What does this print?

x = [5, 2, 8]
y = x.sort()
print(y)

🧩 Your challenge

Write a function top_three(scores) that returns the three highest scores, biggest first, without changing the original list. Read 6 scores from the user into a list, then print the original list and the top three.

Expected output (with the inputs given):
Score: 67
Score: 92
Score: 45
Score: 88
Score: 73
Score: 99
All scores: [67, 92, 45, 88, 73, 99]
Top three: [99, 92, 88]
🐍 Python — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

fruits = ["apple", "mango"]
fruits.remove("banana")

ValueError: list.remove(x): x not in list

✅ Right

fruits = ["apple", "mango"]
if "banana" in fruits:
    fruits.remove("banana")
print(fruits)

remove() fails when the item is not in the list. Check with in first.

❌ Wrong

marks = [40, 90, 70]
marks = marks.sort()
print(marks[0])

TypeError: 'NoneType' object is not subscriptable

✅ Right

marks = [40, 90, 70]
marks.sort()
print(marks[0])

sort() returns None, so marks = marks.sort() throws away your list. Call marks.sort() on its own line.

❌ Wrong

names = ["Asha", "Ravi"]
names.append("Neha", "Tara")

TypeError: list.append() takes exactly one argument (2 given)

✅ Right

names = ["Asha", "Ravi"]
names.extend(["Neha", "Tara"])
print(names)

append() adds exactly one item. To add several at once, put them in a list and use extend().

📌 Remember

  • A slice list[start:stop] gives a new list from start up to, but not including, stop.
  • append adds at the end, insert at a position, and extend adds a whole list.
  • remove deletes by value; pop deletes by position and gives the item back.
  • sort() changes the list and returns None; sorted() returns a new sorted list.
  • Functions can take lists, loop over them and return new lists or numbers.
  • Two names can point to one list; use copy() when you need a separate one.

Finished the quiz and the challenge?

Questions students ask

What does [::-1] mean in Python?

It is a slice with step -1, so it walks backwards. nums[::-1] gives a reversed copy of the list.

What is the difference between append and extend in Python?

append adds one item, even if that item is a list. extend adds each item of another list one by one.

Does slicing a list change the original list?

No. A slice is a new list. The original stays exactly as it was.

How do I sort a list from highest to lowest in Python?

Use marks.sort(reverse=True) to change the list, or sorted(marks, reverse=True) to get a new list.