티스토리 뷰
-. while 문
조건식이 참인 동안 반복해서 내부 블록의 구문을 수행
while <조건식>:
<구문>
※while문을 이용하여 5부터 1까지 출력하기
>>> value = 5
>>> while value > 0:
print(value)
value -= 1
5
4
3
2
1
-. for 문
인자로 받은 시퀀스형 객체와 이터레이션이 가능한 객체를 순차적으로 순회
for <아이템 I> in <Sequence형 객체 S >:
<구문>
인자로 받은 시퀸스 객체 S의 아이템을 순차적으로 아이템 I에 할당, 받은 아이템 I로 구문을 수행
시퀸스 객체 S의 모든 아이템을 순회하면 종료 또는 Break를 만나면 종료
>>> L = ['Apple' , 100 , 15.23]
>>> for i in L:
print(i,type(i))
Apple <class 'str'>
100 <class 'int'>
15.23 <class 'float'>
>>> for k,v in d.items():
print(k,v)
orange 200
apple 100
banana 300
※구구단 만들기
>>> for x in [2,3]:
print(x,'단')
for y in [1,2,3,4,5,6,7,8,9]:
print(x,'*',y,"=",x*y)
2 단
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
3 단
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
-. break, continue, else
break, continue는 반복문을 제어할 때 사용
break : 반복문의 내부블록을 벗어남
continue : continue이후의 반복문을 수행하지 않고 다음 아이템선택
ex)
>>> L = [1,2,3,4,5,6,7,8,9,10]
>>> for i in L: #break를 이용하여 Item이 5보다 크면 반복문 종료
if i > 5:
break
print(i)
1
2
3
4
5
>>> for i in L: #continue를 이용하여 홀수만 출력
if i % 2 == 0:
continue
print (i)
1
3
5
7
9
반복문 수행도중 break로 종료되지 않고 끝까지 수행된 경우 else블록 수행
>>> L=[1,2,3]
>>> \
for i in L:
print(i)
else:
print("Finish")
1
2
3
Finish
>>> \
for i in L:
print(i)
break
else:
print("Finish")
1
'Python' 카테고리의 다른 글
Python, range() (0) | 2016.04.21 |
---|---|
Python, 연산자 (0) | 2016.04.21 |
Python, 참/거짓(bool) (0) | 2016.04.21 |
Python, if문 (0) | 2016.04.21 |
Python, 이터레이터(Iterator) & 제네레이터(Generator) (0) | 2016.04.21 |
- Total
- Today
- Yesterday
- activebackground
- Linux
- borderwidth
- 상수
- disabledforeground
- Private
- highlightbackground
- apache
- Module
- tkinter
- 폼
- Java
- Excel
- 파이썬
- Composite Key
- onetomany
- tkinter command & bind [명령어묶기와 사건묶기] Python
- ManyToOne
- highlightthickness
- command
- 리눅스
- IdClass
- indicatoron
- checkbutton
- activeforeground
- vba
- fetch join
- Python
- JPA
- FetchType
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |