This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Acessar item do dicionário | |
bandas_dicionario = {"Rock Alternativo":"Red Hot Chili Peppers", "Indie Rock": "The Strokes", "Punk":"Ramones"} | |
#Método Get com mensagem para elemento não encontrado | |
bandas_punk = bandas_dicionario.get("Punk", "Não foram encontradas bandas para o estilo.") | |
print(bandas_punk) #Ramones | |
#Método Get com mensagem para elemento não encontrado | |
bandas_heavy_metal = bandas_dicionario.get("Heavy Metal", "Não foram encontradas bandas para o estilo.") | |
print(bandas_heavy_metal) #Não foram encontradas bandas para o estilo. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Remover um item do dicionário | |
bandas_dicionario = {"Rock Alternativo":"Red Hot Chili Peppers", "Indie Rock": "The Strokes", "Punk":"Ramones"} | |
#Método Remove | |
bandas_dicionario.pop("Heavy Metal", None) | |
print(bandas_dicionario) #{'Rock Alternativo': 'Red Hot Chili Peppers', 'Indie Rock': 'The Strokes', 'Punk': 'Ramones'} | |
#Instrução Del | |
del bandas_dicionario["Rock Alternativo"] | |
print(bandas_dicionario) #{'Indie Rock': 'The Strokes', 'Punk': 'Ramones'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Adicionar um item do dicionário | |
bandas_dicionario = {"Rock Alternativo":"Red Hot Chili Peppers", "Indie Rock": "The Strokes", "Punk":"Ramones"} | |
#Método update | |
bandas_dicionario.update({"Heavy Metal": "Iron Maiden"}) | |
print(bandas_dicionario) #{'Rock Alternativo': 'Red Hot Chili Peppers', 'Indie Rock': 'The Strokes', 'Punk': 'Ramones', 'Heavy Metal': 'Iron Maiden'} | |
#Pela nova chave e valor associado | |
bandas_dicionario["Heavy Metal"] = "Black Sabbath" | |
print(bandas_dicionario) #{'Rock Alternativo': 'Red Hot Chili Peppers', 'Indie Rock': 'The Strokes', 'Punk': 'Ramones', 'Heavy Metal': 'Black Sabbath'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Acessar item do dicionário | |
bandas_dicionario = {"Rock Alternativo":"Red Hot Chili Peppers", "Indie Rock": "The Strokes", "Punk":"Ramones"} | |
banda_punk = bandas_dicionario["Punk"] | |
print(banda_punk) #Ramones | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Dicionários Python | |
bandas_dicionario = {"Rock Alternativo":"Red Hot Chili Peppers", "Indie Rock": "The Strokes", "Punk":"Ramones"} | |
print(type(bandas_dicionario)) #<class 'dict'> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Criar um set vazio. | |
bandas_rock = set() | |
print(type(bandas_rock)) #<class 'set'> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Remover um item do Set Python com remove() | |
bandas_rock_alternativo = {"Red Hot Chili Peppers", "Muse", "The Killers"} | |
bandas_rock_alternativo.remove("Muse") #Remove o item pelo próprio objeto | |
print(bandas_rock_alternativo) #{'Red Hot Chili Peppers', 'The Killers'} | |
#Remover um item do Set Python com discard() | |
bandas_rock_alternativo = {"Red Hot Chili Peppers", "Muse", "The Killers"} | |
bandas_rock_alternativo.discard("Muse") #Remove o item pelo próprio objeto | |
print(bandas_rock_alternativo) #{'Red Hot Chili Peppers', 'The Killers'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Adicionando um item no Sets no Python | |
bandas_rock_alternativo = {"Red Hot Chili Peppers", "Muse", "The Killers"} | |
bandas_rock_alternativo.add("Pearl Jam") # Adiciona um elemento no Set já criado. | |
print(bandas_rock_alternativo) #{'Muse', 'Pearl Jam', 'Red Hot Chili Peppers', 'The Killers'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Operações com Sets no Python | |
bandas_rock_alternativo = {"Red Hot Chili Peppers", "Muse", "The Killers"} | |
bandas_indie_rock = {"The Killers", "Muse", "The Strokes"} | |
# União | |
print("União - Todas as bandas (Únicas) ") | |
print(bandas_rock_alternativo | bandas_indie_rock) #{'The Killers', 'Red Hot Chili Peppers', 'The Strokes', 'Muse'} | |
print("==========================================================") | |
# Interseção |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sets igonoram itens duplicados e retornam apenas os itens únicos | |
bandas_sets = {"Metallica", "AC/DC", "The Who", "Ramones", "Metallica"} #Metallica está duplicado | |
print(bandas_sets) #{'AC/DC', 'Ramones', 'The Who', 'Metallica'} #retorna as bandas únicas, com apenas uma banda Metallica. |
NewerOlder