This file contains hidden or 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
| >>> t = (5, 2, 3, 7, 8, 1) | |
| >>> t.sort() # tentando ordenar uma tupla | |
| Traceback (most recent call last): | |
| File "<stdin>", line 1, in <module> | |
| AttributeError: 'tuple' object has no attribute 'sort' | |
| # convertendo uma tupla em lista |
This file contains hidden or 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
| >>> t = (1) | |
| >>> t | |
| 1 | |
| >>> type(t) # t não recebe uma lista, e sim um inteiro | |
| <type 'int'> | |
| # Veja a diferença | |
| >>> t = (1,) | |
| >>> t | |
| (1,) |
This file contains hidden or 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
| # criando uma tupla vazia | |
| >>> t_empty = () | |
| >>> t_empty | |
| () | |
| # criando uma tupla com valores | |
| >>> t = (1, 2, 3, 'Fernando') | |
| >>> t | |
| (1, 2, 3, 'Fernando') |
This file contains hidden or 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
| >>> usuario = {'nome': 'Fernando', 'idade': 27} | |
| >>> len(usuario) | |
| 2 | |
| >>> del(usuario['idade']) | |
| >>> usuario | |
| {'nome': 'Fernando'} |
This file contains hidden or 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
| >>> usuario = {'nome': 'Fernando'} | |
| >>> idade = {'idade': 27} | |
| >>> usuario.update(idade) | |
| >>> usuario | |
| {'idade': 27, 'nome': 'Fernando'} | |
| >>> usuario.update({'idade':28}) | |
| >>> usuario | |
| {'idade': 28, 'nome': 'Fernando'} |
This file contains hidden or 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
| >>> d = {'nome': 'Fernando} | |
| >>> d.get('nome') | |
| 'Fernando' | |
| >>> d['nome'] | |
| 'Fernando' | |
| >>> d.get('a') | |
| >>> |
This file contains hidden or 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
| # Cria um novo dicionário | |
| >>> d = {'id': 1, 'nome': 'Fernando', 'idade': 27} | |
| # Atribui d a d2 | |
| >>> d2 = d | |
| >>> d2 | |
| {'id': 1, 'nome': 'Fernando', 'idade': 27} | |
| # Repare que atribuindo d a d2 ambos possuem a mesma referência na memória | |
| >>> id(d) |
This file contains hidden or 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
| >>> d = {'id': 1, 'nome': 'Fernando', 'idade': 27} | |
| >>> d.values() | |
| [27, 1, 'Fernando'] |
This file contains hidden or 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
| >>> d = {'id': 1, 'nome': 'Fernando', 'idade': 27} | |
| >>> d.keys() | |
| ['idade', 'id', 'nome'] |
This file contains hidden or 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
| >>> d = {1: 'id', 'lista': [1, 2, 3, 4], 'id': 1} | |
| >>> d.has_key('lista') | |
| True | |
| >>> d.has_key(123) | |
| False |