Listas

Introdução ao Python para desenvolvedores

Jasmin Ludolf

Senior Data Science Content Developer

Problema

# Variables of ingredient names
ingredient_one = "pasta"
ingredient_two = "tomatoes"
ingredient_three = "garlic"
ingredient_four = "basil"
ingredient_five = "olive oil"
ingredient_six = "salt"
Introdução ao Python para desenvolvedores

Listas para salvar o dia!

  • Lista = armazena vários valores em uma variável
    • Pode conter qualquer combinação de tipos

 

# 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]
Introdução ao Python para desenvolvedores

Verificando o tipo de dado

# Check the data type of a list
print(type(ingredients))
<class 'list'>
Introdução ao Python para desenvolvedores

Acessando elementos de uma lista

# Print all values in the list variable
print(ingredients)
['pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt']
  • Listas são ordenadas e indexadas
    • Python indexa a partir de zero para o primeiro elemento
Introdução ao Python para desenvolvedores

Acessando elementos de uma lista

  • Lista = []
  • Acessar um elemento = 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
Introdução ao Python para desenvolvedores

Encontrando o último elemento

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
Introdução ao Python para desenvolvedores

Acessando vários elementos

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]
  • Soma 1 ao índice final porque:
    • Python retorna tudo até, mas não incluindo, esse índice
Introdução ao Python para desenvolvedores

Acessando vários elementos

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']
Introdução ao Python para desenvolvedores

Acesso alternado

ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]

# Access every second element print(ingredients[::2])
['pasta', 'garlic', 'olive oil']]
  • Retorna os itens nos índices 0, 2 e 4
# Access every third element, starting at the second
print(ingredients[1::3])
['tomatoes', 'olive oil']
  • Retorna os itens nos índices 1 e 4
Introdução ao Python para desenvolvedores

Vamos praticar!

Introdução ao Python para desenvolvedores

Preparing Video For Download...