搜尋此網誌

2026年5月6日星期三

Lists in Python

In Python, list slicing lets you extract portions of a list using the syntax:

list[start:stop:step]

Components

  • start → index where the slice begins (inclusive).

  • stop → index where the slice ends (exclusive).

  • step → interval between indices (default is 1).

numbers = [10, 20, 30, 40, 50, 60] print(numbers[1:4]) # [20, 30, 40] (from index 1 to 3) print(numbers[:3]) # [10, 20, 30] (first 3 elements) print(numbers[3:]) # [40, 50, 60] (from index 3 to end) print(numbers[::2]) # [10, 30, 50] (every 2nd element) print(numbers[::-1]) # [60, 50, 40, 30, 20, 10] (reversed list)

numbers[1:4]
  • Start = 1 → begin at index 1 (the second element, which is 20).

  • Stop = 4 → go up to, but not including, index 4.

  • So you get elements at indices 1, 2, and 320, 30, 40.

Python slicing always excludes the stop index. That’s why 50 (at index 4) isn’t included.

print(numbers[::2])

  • Start = empty → defaults to the beginning of the list (index 0).

  • Stop = empty → defaults to the end of the list.

  • Step = 2 → take every second element.

So Python picks indices 0, 2, 4, ... until the end

myList = [1, 2, 3, 4, 5] print(myList[0:6:2])

Start = 0 → begin at index 0 (1).

Stop = 6 → go up to, but not including, index 6.

Step = 2 → take every second element.

Notice that even though you wrote stop = 6, your list only goes up to index 4. Python slicing doesn’t throw an error — it just stops at the end of the list. That’s why you still get [1, 3, 5].

The range() function in Python generates a sequence of integers, starting from a given start (default 0), stopping before a given stop, and incrementing by a given step (default 1). It’s most often used in loops to control iteration.

range(start, stop, step)

start → optional, default 0. First number in the sequence.

stop → required. Sequence ends before this number.

step → optional, default 1. Difference between consecutive numbers.

Range function is immutable.

>>> for i in range(5): ...     print(i) ... 0 1 2 3 4 >>>

>>> myList = [1,2,3,4] ... myList.append(5) ... print(myList) ... [1, 2, 3, 4, 5] >>>

In Python, lists have several useful methods for adding and removing elements. Let’s look at insert(), remove(), and pop() side by side:

numbers = [10, 20, 30] numbers.insert(1, 15) # insert 15 at index 1 print(numbers) # [10, 15, 20, 30]

numbers = [10, 20, 30, 20] numbers.remove(20) # removes the first 20 print(numbers) # [10, 30, 20]

pop(): remove and return an element at a given index (default is the last element).

>>> myList = [1,2,3,4,5]
>>> myList.pop()
5

numbers = [10, 20, 30] x = numbers.pop(1) # removes element at index 1 print(x) # 20 print(numbers) # [10, 30]

>>> myList = [1,2,3,4,5]
... while len(myList):
...     print(myList.pop())
...
5
4
3
2
1

Loop ends because len(myList) is now 0.

>>> a = [1,2,3,4,5]
... b = a.copy()
... a.append(6)
... print(a)
... print(b)
...
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5]

b = a.copy() → makes a shallow copy of a.

  • Now b has its own separate list [1,2,3,4,5].

Microsoft Copilot

沒有留言:

發佈留言