-
Notifications
You must be signed in to change notification settings - Fork 49
/
009_for_cycle.py
37 lines (28 loc) · 961 Bytes
/
009_for_cycle.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
# Video https://youtu.be/VH_YaAI-BKI
numbers = [1, 2, 3, 4, 5, 10]
# Pomocou for cyklu vieme prechadzat cez polia
for number in numbers:
print(number)
# Alebo cez retazce.
for letter in "Michal Hucko":
print(letter)
# Funkcia range nam pomaha vytvorit pocitadlo, alebo pole cisel <dolnyny interval; horny interval)
counter = range(0, 5)
print(counter)
# Mozme definovat o kolko sa maju prvky v pcitadle zvysovat pomocou tretieho argumentu
range(0, 10, 2)
range(10, 1, -2)
# Pozor na nekonecne pole
range(0, 10, -2)
# Funkcia range a cyklus for sa casto pouzivaju spolu
for number in range(0, 10, 2):
print(number)
# Takti vieme iterovat cez akekolvek pole
letters = ["M", "i", "c", "h", "a", "l", 7, 8.7, [4, 5]]
for item in letters:
print(item)
# Do cyklu vieme pridat aj podmienky. Pozor na odsadenie (tabulator, 4 medzery)
letters = ["M", "i", "c", "h", "a", "l"]
for letter in letters:
if letter != 'c':
print(letter)