[Python] for in 반복문, range, enumerate

토끼는 개발개발·2022년 1월 10일
0

Python

목록 보기
7/11
post-thumbnail

✏️ 1. for in 반복문

for item in iterable:
...반복할 구문 ...

  • iterable : list, dictionary, set, string, string, tuple, bytes 타입 + range

✨ 다음 예시를 살펴보자.

var = [1,2,3,4]
for i in var:
	print(i)
#출력
1
2
3
4


✏️ 2. range

range(시작숫자, 종료숫자, step)

  • 시작숫자, step 생략가능
  • 종료숫자-1까지 출력

✨ 다음 예시를 살펴보자.

>>> list(range(10,20,2))
[10, 12, 14, 16, 18]
for i in range(5):
	print(i)
#출력
0
1
2
3
4


✏️ 3. enumerate

enumetate(iterable)

  • 인덱스 번호와 컬렉션의 원소를 tuple형태로 반환
  • 반복문 사용 시 몇 번째 반복문인지 확인이 필요할 때 사용

✨ 다음 예시를 살펴보자.

t = [1, 5, 7]
for i in enumerate(t):
	print(i)
#출력
(0,1)
(1,5)
(2,7)
for i,v in enumerate(t):
	print("index: {}, value: {}".format(i,v))
#출력
index : 0, value: 1
index : 1, value: 5
index : 2, value: 7
profile
하이 이것은 나의 깨지고 부서지는 샏 스토리입니다

0개의 댓글