Introduction to Python for Developers
Jasmin Ludolf
Senior Data Science Content Developer
# Variables of ingredient names
ingredient_one = "pasta"
ingredient_two = "tomatoes"
ingredient_three = "garlic"
ingredient_four = "basil"
ingredient_five = "olive oil"
ingredient_six = "salt"
# List of ingredients
ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]
# List of ingredients using variables as values
ingredients = [ingredient_one, ingredient_two, ingredient_three,
ingredient_four, ingredient_five, ingredient_six]
# Check the data type of a list
print(type(ingredients))
<class 'list'>
# Print all values in the list variable
print(ingredients)
['pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt']
[]a_list[index]ingredients = ["pasta", "tomatoes",
"garlic", "basil", "olive oil", "salt"]
# Get the value at the first index
print(ingredients[0])
pasta
# Get the fourth element
print(ingredients[3])
basil
ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]
# Get the last element of a list
print(ingredients[5])
salt
# Get the last element of a list
print(ingredients[-1])
salt
ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]
# Access the second and third elements
print(ingredients[1:3])
["tomatoes", "garlic"]
[first_element:last_element + 1]ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]
# Access all elements from the third index onwards
print(ingredients[3:])
['basil', 'olive oil', 'salt']
# Get the first three elements
print(ingredients[:3])
['pasta', 'tomatoes', 'garlic']
ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]# Access every second element print(ingredients[::2])
['pasta', 'garlic', 'olive oil']]
# Access every third element, starting at the second
print(ingredients[1::3])
['tomatoes', 'olive oil']
Introduction to Python for Developers