Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- File
- bitcoin
- 리뷰
- windows
- 책
- go
- FOR
- Callback
- channel
- JavaScript
- Golang
- c++
- write
- 영화
- Linux
- mutex
- tcp
- window
- Close
- Sync
- install
- API
- Python
- C
- http
- package
- range
- json
- go언어
- GO 언어
Archives
- Today
- Total
Code Habit
[Python] range(), enumerate(), items() 사용 본문
1. range
- 범위 자료형으로 for문의 반복 범위를 정할 때 사용할 수 있다.
for i in range(5) :
print(i)
#결과
0
1
2
3
4
2. 리스트와 range함수 조합하기
- for문에서 리스트의 인덱스, 값 두개에 접근하기 위해 조합할 수 있다.
array = ["가", "나", "다", "라"]
for i in range(len(array)) :
print("{}번째 요소 : {}".fromat(i, array[i]))
3. enumerate
- iterable object로 for문에서 리스트등의 범위 자료형에 접근할 때 사용할 수 있다.
array = ["가", "나", "다", "라"]
for i, value in enumerate(array) :
print("{}번째 요소 : {}".format(i, value))
4. items()
- for문에서 딕셔너리와 같은 범위 자료형에 접근할 때 items()함수를 사용할 수 있다.
dict_a = {
"key1" : "value1",
"key2" : "value2",
"key3" : "value3"
}
for key, element in dict_a.items() :
print("dict_a[{}] : {}".format(key, element))