Вступ до Python для розробників
Jasmin Ludolf
Senior Data Science Content Developer
# Змінні з назвами інгредієнтів
ingredient_one = "pasta"
ingredient_two = "tomatoes"
ingredient_three = "garlic"
ingredient_four = "basil"
ingredient_five = "olive oil"
ingredient_six = "salt"
# Список інгредієнтів
ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]
# Список інгредієнтів із використанням змінних як значень
ingredients = [ingredient_one, ingredient_two, ingredient_three,
ingredient_four, ingredient_five, ingredient_six]
# Перевірте тип даних списку
print(type(ingredients))
<class 'list'>
# Виведіть усі значення у змінній-списку
print(ingredients)
['pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt']
[]a_list[index]ingredients = ["pasta", "tomatoes",
"garlic", "basil", "olive oil", "salt"]
# Отримайте значення за першим індексом
print(ingredients[0])
pasta
# Отримайте четвертий елемент
print(ingredients[3])
basil
ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]
# Отримайте останній елемент списку
print(ingredients[5])
salt
# Отримайте останній елемент списку
print(ingredients[-1])
salt
ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]
# Доступ до другого та третього елементів
print(ingredients[1:3])
["tomatoes", "garlic"]
[first_element:last_element + 1]ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]
# Доступ до всіх елементів починаючи з третього індексу
print(ingredients[3:])
['basil', 'olive oil', 'salt']
# Отримайте перші три елементи
print(ingredients[:3])
['pasta', 'tomatoes', 'garlic']
ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]# Доступ через один елемент print(ingredients[::2])
['pasta', 'garlic', 'olive oil']]
# Доступ через два елементи, починаючи з другого
print(ingredients[1::3])
['tomatoes', 'olive oil']
Вступ до Python для розробників