카테고리 없음

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 = [135]
 
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
= [1234]
= [567]
 
= 0
while i < len(a):
    j = 0
    while j < len(b):
        print (a[i], b[j]),
        j += 1
    i += 1
 
<<<<<실행결과>>>>>
(15) (16) (17) (25) (26) (27) (35) (36) (37) (45) (46) (47)
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
= [123]
= [2456]
= [6789]
 
= 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
 
 
<<<<<실행결과>>>>>
(126) (127) (128) (129) (146) (147) (148) (149) (156) (157) (158) (159) (166) (167) (168) (169) (226) (227) (228) (229) (246) (247) (248) (249) (256) (257) (258) (259) (266) (267) (268) (269) (326) (327) (328) (329) (346) (347) (348) (349) (356) (357) (358) (359) (366) (367) (368) (369)
cs




반응형