Bài trước: Python căn bản (13): Python Lists
[Từ điển]
14. Python Lists (cont.1)
14.1 Access List Items
Access Items
List items are indexed and you can access them by referring to the index number.
Example
Print the second item of the list.
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
Note: The first item has index 0.
Negative Indexing
Negative indexing means start from the end
-1 refers to the last item, -2 refers to the second last item etc.
Example
Print the last item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[-1])
Range of Indexes
You can specify a range of indexes by specifying where to start and where to end the range.
When specifying a range, the return value will be a new list with the specified items.
Example
Return the third, fourth, and fifth item:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
Note: The search will start at index 2 (included) and end at index 5 (not included).
Remember that the first item has index 0.
By leaving out the start value, the range will start at the first item:
Example
This example returns the items from the beginning to, but NOT including, "kiwi":
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[:4])
By leaving out the end value, the range will go on to the end of the list:
Example
This example returns the items from "cherry" to the end:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:])
Range of Negative Indexes
Specify negative indexes if you want to start the search from the end of the list.
Example
This example returns the items from "orange" (-4) to, but NOT including "mango" (-1):
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[-4:-1])
Check if Item Exists
To determine if a specified item is present in a list use the in keyword.
Example
Check if "apple" is present in the list:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in
the fruits list")
14.2 Change Item Value
To change the value of a specific item, refer to the index number.
Example
Change the second item:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
Change a Range of Item Values
To change the value of items within a specific range, define a list with the new values, and refer to the range of index numbers where you want to insert the new values.
Example
Change the values "banana" and "cherry" with the values "blackcurrant" and "watermelon":
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)
If you insert more items than you replace, the new items will be inserted where you specified, and the remaining items will move accordingly.
Example
Change the second value by replacing it with two new values:
thislist = ["apple", "banana", "cherry"]
thislist[1:2] = ["blackcurrant", "watermelon"]
print(thislist)
Note: The length of the list will change when the number of items inserted does not match the number of items replaced.
If you insert less items than you replace, the new items will be inserted where you specified, and the remaining items will move accordingly.
Example
Change the second and third value by replacing it with one value:
thislist = ["apple", "banana", "cherry"]
thislist[1:3] = ["watermelon"]
print(thislist)
Insert Items
To insert a new list item, without replacing any of the existing values, we can use the insert() method.
The insert() method inserts an item at the specified index.
Example
Insert "watermelon" as the third item:
thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "watermelon")
print(thislist)
Note: As a result of the example above, the list will now contain 4 items.
14.3 Exercise
1. What will be the result of the following code:
mylist = ['apple',
'banana', 'cherry']
print(mylist[-1])
A. apple
B. banana
C. -1
D. cherry
2. Print the second item in the fruits list.
fruits = ["apple", "banana", "cherry"]
print(_______)
A. fruits[0]
B. fruits[1]
C. fruits[2]
D. fruits[-1]
3. What will be the result of the following code:
mylist = ['apple', 'banana', 'cherry', 'orange', 'kiwi']
print(mylist[1:4])
A. ['banana', 'cherry', 'orange']
B. ['banana', 'cherry', 'orange', 'kiwi']
C. ['cherry', 'orange', 'kiwi']
D. [1:4]
4. Use a range of indexes to print the third, fourth, and fifth item in the list.
fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(fruits[______])
A. 3:5
B. 2:5
C. 2:4
D. 3:6
5. What will be the result of the following code:
mylist = ['apple', 'banana', 'cherry']
mylist[0] = 'kiwi'
print(mylist[1])
A. banana
B. cherry
C. apple
D. kiwi
6. What will be the result of the following code:
mylist = ['apple', 'banana', 'cherry']
mylist[1:2] = ['kiwi',
'mango']
print(mylist[2])
A. kiwi
B. mango
C. banana
D. cherry
7. Write a Python program to create a list to store student’s
information, including: name, age, gender, GPA. Print the student’s information
out following this pattern:
Information of Student:
[1] Name: Teo
[2] Age: 16
[3] Gender: male
[4] GPA: 3.0
-----
The answer hints:
1(D), 2(B), 3(A), 4(B), 5(A), 6(B)
7. Write a Python program to create a list to store student’s information,
including: name, age, gender, GPA. Print the student’s information out
following this pattern:
Information of Student:
[1] Name: Teo
[2] Age: 16
[3] Gender: male
[4] GPA: 3.0
[code14_7.py]
# Create a list to store student information
studentInfo = ["Teo", 16, "male", 3.0]
# Print the student's information
print("Information of Student:")
print(f"[1] Name: {studentInfo[0]}")
print(f"[2] Age: {studentInfo[1]}")
print(f"[3] Gender: {studentInfo[2]}")
print(f"[4] GPA: {studentInfo[3]}")
-----
Cập nhật: 31/10/2024
Bài sau: Python căn bản (15): Python Lists (cont.2)
-----
[Nội dung tham khảo từ w3schools, pynative và Internet]
Bạn muốn học Python căn bản tại Đà Lạt, liên hệ