#boolean : T/F 조건식
#if문
#for순환문(loop)
#while문
boolean
#boolean : T/F 조건식
a = True
print(a)
print(type(a))
print(1==1) #좌변과 우변이 같냐?
print(2 > 1) #2가 1보다 큰가?
print( 1 >=1 ) #1이 1이상인가?
print( 1 != 1) # 1과 1이 다른가?
-----------
결과값
True
<class 'bool'>
True
True
True
False
if문
score = 80
# A) 단순 IF(아님말고)
if score >= 60 :
print("합격입니다")
print("축하합니다")
if score <= 60:
print("불합격입니다")
-> 합격입니다
축하합니다
# B)양자택일
if score >= 60:
print("합격입니다")
print("축하합니다")
else:
print("불합격")
-> 합격입니다
축하합니다
# C)다중택일
if score >= 90:
print('a')
elif score >= 80:
print('b')
elif score >= 70:
print('c')
elif score >= 60:
print('d')
else:
print('f')
-> b
순환문(loop)
for i in range(1, 10 + 1): # 1부터 10이 될때까지 + 1 반복
print(i)
>>>
1
2
3
4
5
6
7
8
9
10
---------------
#반대로
for i in range(10, 0, -1): # 10부터 0이 될때까지 -1 반복 0뒤에 ,를 붙임
print(i)
>>>
10
9
8
7
6
5
4
3
2
1
----------------
#중첩 for
for i in range(2, 10): #단 2단부터 9단까지
for j in range(1, 10): #곱 1부터 9를 곱하다.
#print(i*j)
print(i*j, end =' ') # end =' ' 수평으로 나오게 한다.
print() #줄바꿈
>>>
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
while 문
'''
while 조건식:
본문...
'''
i = 1
while i <= 10: # 10이하가 될때까지 1씩 증가하라
print(i) #무한 루프를 조심해야 한다.
i += 1 #무한 루프 시키지 않기 위해.
>>>
1
2
3
4
5
6
7
8
9
10
----------------------------------------------
#while만을 이용해 10부터 1까지 반복하고 마지막에 로켓발사가 나오게 한다.
i = 10
while i >= 1:
print(i)
i -= 1
if i == 0:
print("로켓발사")
>>>
10
9
8
7
6
5
4
3
2
1
로켓발사
----------------------------
#더 간간히 줄이고 로켓발사가 나오게 하는 방법. 파이썬은 루프를 빠져나오면 자동으로 다음줄 실행
i = 10
while i >= 1:
print(i)
i -= 1
print("로켓발사")
----------------------------
#1~10 합을 구하라.
h = 0
for i in range(1, 11): # 1부터 10까지 더하라.
h += i
print(h)
>>>
55
----------------------------
h = 0
i = 1
while i <= 10: # i가 10이하가 될때까지
h += i # 0에 i를 더하는데
i += 1 # i는 1씩 증가한다.
print(h)
>>>
55
반응형
'AI > Python' 카테고리의 다른 글
Python Basic #09 def (0) | 2021.07.23 |
---|---|
Python Basic #08 while / break / continue (0) | 2021.07.23 |
Python Basic #06 tuple (0) | 2021.07.23 |
Python Basic #05 .append() / .insert(,) / .extend() / +=[] (0) | 2021.07.23 |
Python Basic #04 list (0) | 2021.07.23 |