-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaula100_iteradores_e_iteraveis.py
45 lines (34 loc) · 1.15 KB
/
aula100_iteradores_e_iteraveis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Entendendo Iterators e Iterables
Iterator:
- Objeto que pode ser iterado;
- Objeto que retorna um dado, sendo um elemento por vez quando uma função next() é chamada.
Iterable:
- Objeto que irá retornar um iterador quando a função iter() for chamada.
"""
nome = 'Geek' # É um iterable, mas não é um iterator
numeros = [1, 2, 3, 4, 5, 6] # É um iterable, mas não é um iterator
print(nome)
print(numeros)
# print(next(nome)) # TypeError: 'str' object is not an iterator
# print(next(numeros)) # TypeError: 'str' object is not an iterator
# Então para iterar com next, precisamos converter os itens em iterators:
it1 = iter(nome)
it2 = iter(numeros)
# Agora podemos iterar um a um:
print(next(it1))
print(next(it1))
print(next(it1))
print(next(it1))
# E se tentarmos usar mais um next(it1) dará um StopIteration Error!
# Da mesma forma com os números:
print(next(it2))
print(next(it2))
print(next(it2))
print(next(it2))
print(next(it2))
print(next(it2))
# Por baixo dos panos quando usamos um for, é a mesma coisa que acontece do next acima:
for letra in nome:
print(f'{letra}')
# O Python não deixa chegar no erro de StopIteration