카테고리 없음
Day03. while-else 문
파이3.14
2017. 5. 22. 06:56
while-else 문
- break문에 걸리지 않고 while 종료시 else문 호출
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 | nums = [1, 3, 5] position = 0 ###인덱스 변수로 사용 while position < len(nums): number = nums[position] if number % 2 == 0: print 'even number' break print number position += 1 else: print 'no even number' print 'position=', position <<<<<실행결과>>>>> 1 3 5 no even number position= 3 | cs |
### 이중 while문, big o notation = n^2
1 2 3 4 5 6 7 8 9 10 11 12 13 | a = [1, 2, 3, 4] b = [5, 6, 7] i = 0 while i < len(a): j = 0 while j < len(b): print (a[i], b[j]), j += 1 i += 1 <<<<<실행결과>>>>> (1, 5) (1, 6) (1, 7) (2, 5) (2, 6) (2, 7) (3, 5) (3, 6) (3, 7) (4, 5) (4, 6) (4, 7) | cs |
### 삼중 while문, big o notaion = n^3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | a = [1, 2, 3] b = [2, 4, 5, 6] c = [6, 7, 8, 9] i = 0 while i < len(a): j = 0 while j < len(b): k = 0 while k < len(c): print (a[i], b[j], c[k]), k += 1 j += 1 i += 1 <<<<<실행결과>>>>> (1, 2, 6) (1, 2, 7) (1, 2, 8) (1, 2, 9) (1, 4, 6) (1, 4, 7) (1, 4, 8) (1, 4, 9) (1, 5, 6) (1, 5, 7) (1, 5, 8) (1, 5, 9) (1, 6, 6) (1, 6, 7) (1, 6, 8) (1, 6, 9) (2, 2, 6) (2, 2, 7) (2, 2, 8) (2, 2, 9) (2, 4, 6) (2, 4, 7) (2, 4, 8) (2, 4, 9) (2, 5, 6) (2, 5, 7) (2, 5, 8) (2, 5, 9) (2, 6, 6) (2, 6, 7) (2, 6, 8) (2, 6, 9) (3, 2, 6) (3, 2, 7) (3, 2, 8) (3, 2, 9) (3, 4, 6) (3, 4, 7) (3, 4, 8) (3, 4, 9) (3, 5, 6) (3, 5, 7) (3, 5, 8) (3, 5, 9) (3, 6, 6) (3, 6, 7) (3, 6, 8) (3, 6, 9) | cs |
반응형