개발 => 복습 후 재정리 대기/Python
[Python][문법] 반복문 for, enumerate, break
fruits = ['사과', '배', '감', '수박', '딸기']
for fruit in fruits:
print(fruit)
people = [
{'name': 'bob', 'age': 20},
{'name': 'carry', 'age': 38},
{'name': 'john', 'age': 7},
{'name': 'smith', 'age': 17},
{'name': 'ben', 'age': 27},
{'name': 'bobby', 'age': 57},
{'name': 'red', 'age': 32},
{'name': 'queen', 'age': 25}
]
for person in people:
name = person['name']
age = person['age']
if age > 30:
print(name, ' : ', age)
# carry : 38
# bobby : 57
# red : 32
for i, person in enumerate(people):
name = person['name']
age = person['age']
print(i, name, ' : ', age)
if i > 2:
break
# 0 bob : 20
# 1 carry : 38
# 2 john : 7
# 3 smith : 17
fruits = ['사과', '배', '감', '수박', '딸기']
for fruit in fruits:
print(fruit)
people = [
{'name': 'bob', 'age': 20},
{'name': 'carry', 'age': 38},
{'name': 'john', 'age': 7},
{'name': 'smith', 'age': 17},
{'name': 'ben', 'age': 27},
{'name': 'bobby', 'age': 57},
{'name': 'red', 'age': 32},
{'name': 'queen', 'age': 25}
]
for person in people:
name = person['name']
age = person['age']
if age > 30:
print(name, ' : ', age)
# carry : 38
# bobby : 57
# red : 32
for i, person in enumerate(people):
name = person['name']
age = person['age']
print(i, name, ' : ', age)
if i > 2:
break
# 0 bob : 20
# 1 carry : 38
# 2 john : 7
# 3 smith : 17
'개발 => 복습 후 재정리 대기 > Python' 카테고리의 다른 글
[Python][문법] function (함수) (0) | 2021.05.05 |
---|---|
[Python][문법] for, if 예제 (0) | 2021.05.05 |
[Python][문법] 조건문 if (0) | 2021.05.05 |
[Python][문법] list + dictionary (0) | 2021.05.05 |
[Python][문법] Dictionary (0) | 2021.05.05 |
댓글