👩‍💻 Develope/Python

[Python] enumerate(), range()

heywantodo 2023. 8. 15. 11:58
728x90
반응형

[Python] enumerate(), range()

1. enumerate

순서가 있는 자료형(List, Set, Tuple, Dictionary, String)을 입력으로 받아

인덱스 값을 포함하는객체를 돌려준다.

 

for문과 함께 사용하면 자료형의 index를 알 수 있다. 

for idx, e in enumerate(['a','b','c']):
	print(idx, e)
    
0 a
1 b
2 c

 

2. range()

연속된 숫자를 만들어주는 함수

range(1, 11) ==> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 

for문에서 많이 사용된다.

for num in range(2, 5):
    print(num)
2
3
4

범위의 증가감소도 가능하다.

for num in (2, 10, 2):
	print(num)
2
4
6
8

 

참고

https://ctkim.tistory.com/entry/python-enumerate-function

https://www.daleseo.com/python-range/

728x90
반응형