if else문은..
for 문
for i in range(1,10):
for j in range(1,10):
print (i*j,end=" ")
print('')
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
scores =[80, 45, 77, 55, 91]
num=0
for score in scores:
num = num+1
if score >=60:
print("%dth student is pass." % num)
else:
print("%dth student is fail." % num)
1th student is pass.
2th student is fail.
3th student is pass.
4th student is fail.
5th student is pass.
continue
:continue 문은 실행중인 루프 블록의 나머지 명령문들을 실행하지 않고 곧바로 다음 루프로 넘어가도록 한다.
scores =[80, 45, 77, 55, 91]
num=0
for score in scores:
num = num+1
if score < 60:
continue
else:
print("%dth student, Congratulations, you are a pass." % num)
1th student, Congratulations, you are a pass.
3th student, Congratulations, you are a pass.
5th student, Congratulations, you are a pass.
break
break은 반복을 강제종료한다.
for i in range(10):
print(i)
if i == 3:
break
0
1
2
3
리스트 컴프리헨션
[표현식 for 항목 in 순회가능객체]
a=[1,2,3,4,5]
output =[num*5 for num in a]
print(output)
[5, 10, 15, 20, 25]
if 문을 포함한 형태
a=[1,2,3,4,5,6,7,8,9]
output=[num*5 for num in a if num%3==0]
print(output)
[15, 30, 45]
중첩된 형태
output =[x*y for x in range (1,5) for y in range(1,6)]
print(output)
[1, 2, 3, 4, 5, 2, 4, 6, 8, 10, 3, 6, 9, 12, 15, 4, 8, 12, 16, 20]
while문
num =0
while num <=10:
if num%3==0:
print(num)
num +=1
0
3
6
9
num=0
while num < 10:
num +=1
if num % 3 ==-0:
continue
print(num)
1
2
4
5
7
8
10
i=0
while True:
print(i)
i +=1
if i ==3:
break
0
1
2
try문
finally는 오류와 상관없이 무조건 실행된다.
'Study > Python' 카테고리의 다른 글
입력과 출력문 (0) | 2024.05.17 |
---|---|
컴프리헨션과 range() (0) | 2024.03.25 |
배열의 패킹과 언패킹 (0) | 2024.03.25 |
문자열 (1) | 2024.03.18 |
math 패키지 및 통계 (0) | 2024.03.12 |