Nhập môn Python dành cho Developer
Jasmin Ludolf
Senior Data Science Content Developer
# Biến tên nguyên liệu
ingredient_one = "pasta"
ingredient_two = "tomatoes"
ingredient_three = "garlic"
ingredient_four = "basil"
ingredient_five = "olive oil"
ingredient_six = "salt"
# Danh sách nguyên liệu
ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]
# Danh sách nguyên liệu dùng biến làm giá trị
ingredients = [ingredient_one, ingredient_two, ingredient_three,
ingredient_four, ingredient_five, ingredient_six]
# Kiểm tra kiểu dữ liệu của danh sách
print(type(ingredients))
<class 'list'>
# In mọi giá trị trong biến danh sách
print(ingredients)
['pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt']
[]a_list[index]ingredients = ["pasta", "tomatoes",
"garlic", "basil", "olive oil", "salt"]
# Lấy giá trị tại chỉ số đầu tiên
print(ingredients[0])
pasta
# Lấy phần tử thứ tư
print(ingredients[3])
basil
ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]
# Lấy phần tử cuối của danh sách
print(ingredients[5])
salt
# Lấy phần tử cuối của danh sách
print(ingredients[-1])
salt
ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]
# Truy cập phần tử thứ hai và thứ ba
print(ingredients[1:3])
["tomatoes", "garlic"]
[first_element:last_element + 1]ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]
# Truy cập tất cả phần tử từ chỉ số 3 trở đi
print(ingredients[3:])
['basil', 'olive oil', 'salt']
# Lấy ba phần tử đầu tiên
print(ingredients[:3])
['pasta', 'tomatoes', 'garlic']
ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]# Truy cập mỗi phần tử thứ hai print(ingredients[::2])
['pasta', 'garlic', 'olive oil']]
# Truy cập mỗi phần tử thứ ba, bắt đầu tại phần tử thứ hai
print(ingredients[1::3])
['tomatoes', 'olive oil']
Nhập môn Python dành cho Developer