본문 바로가기
  • Slow and Steady wins the race
abt Python/기초

python 제어문

by cloudin 2023. 8. 7.

제어문

: 조건에 따라 명령을 수행하는 '조건문'과 조건을 만족할 때까지 계속 반복하는 '반복문'이 있다.

 

조건문

- 비교 및 논리 연산자

비교연산자 의미 내용
< 작다 a<b a는 b보다 작다
> 크다 a>b a는 b보다 크다
<= 작거나 같다 a<=b a는 b보다 크거나 같다
== 같다 a==b a와 b는 같다
!= 같지 않다 a!=b a와 b는 같지 않다
논리연산자 활용 예 내용
논리곱(and) A and B A와 B 둘 다 참일 때 참, 나머지는 거짓
논리합(or) A or B A와 B 중 하나라도 참이면 참, 둘 다 거짓이면 거짓
논리 부정(not) not A A가 참이면 거짓, 거짓이면 

 

- 단일 조건에 따른 분기 (if문)

x = 90
if x > 85:
    print("합격")
# 합격

 

- 단일 조건 및 그 외 조건에 따른 분기 (else문)

# 조건이 맞으면 실행, 조건이 안 맞으면 else문을 통하여 실행
x = 75
if x > 85:
    print("합격")
else:
    print("불합격")
# 불합격

 

- 단일 조건 및 그 외 조건에 따른 분기 (if ~ elif ~ else문)

# 여러가지 조건
x = 70
if x > 85:
    print("A")
elif (x>=70 and x<85):
    print("B")
else:
    print("F")
# B
x = 70
if x > 85:
    print("A")
elif (70 <= x < 85):
    print("B")
else:
    print("F")
# B

 

- 중첩 조건에 따른 분기

# 중첩 조건에 따른 분기 예시
x = 100
if x >= 85:
    if x == 100:
        print("A+")
    else:
        print("A")
elif 70 <= x < 85:
    print("B")
else:
    print("F")
# A+

 

반복문

: 순차적 작업을 반복적으로 수행한다.

 

같은 문장을 사용하여 결과가 반복적으로 실행이 되게 하였다.

a = 0
print(a)
a = a + 1
print(a)
a = a + 1
print(a)
a = a + 1
print(a)
a = a + 1
print(a)
a = a + 1
print(a)
# 0
# 1
# 2
# 3
# 4
# 5

 

- for문 사용

for a in[0, 1, 2, 3, 4, 5]:
    print(a)
# 0
# 1
# 2
# 3
# 4
# 5

 

- 반복 범위 지정(range)

# range
print(range(0, 20, 4)) # range(0, 20, 4)
# list 형태로 보자
print(list(range(0, 20, 4))) # [0, 4, 8, 12, 16]
# 튜플 형태
print(tuple(range(0, 20, 4))) # (0, 4, 8, 12, 16)
# 세트 형태
print(set(range(0, 20, 4))) # {0, 4, 8, 12, 16}
# range 활용하여 for문
# 0부터 5미만까지 1씩 증가
for a in range(0, 5, 1):
    print(a)
# 0
# 1
# 2
# 3
# 4

# 0부터 10미만의 값을 1씩 증가
print(list(range(0, 10, 1))) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list(range(0, 10)))    # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list(range(10)))       # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# -10부터 0미만의 값을 2씩 증가
print(list(range(-10, 0, 2))) # [-10, -8, -6, -4, -2]

# 0부터 -5까지 1씩 감소
print(list(range(0, -5, -1))) # [0, -1, -2, -3, -4]

 

- 중첩 for문

# 중첩 for문
x_list = ['x1', 'x2']
y_list=['y1', 'y2']

print("x, y")
for x in x_list:
    for y in y_list:
        print(x, y)
# x, y
# x1 y1
# x1 y2
# x2 y1
# x2 y2

 

 

- while문 사용

x = 0
y = 0
print("x, y")
while(y<20):
    x = x+1
    y = y+x
    print(x, y)
# x, y
# 1 1
# 2 3
# 3 6
# 4 10
# 5 15
# 6 21

 

- 반복문을 빠져나오는 Break

a = 0
while True:
    a = a + 1
    if (a>5):
        break
    print(a)
# 1
# 2
# 3
# 4
# 5

for a in range(10):
    if a > 4:
        break
    print(a)
# 0
# 1
# 2
# 3
# 4

 

- 다음 반복을 실행하는 continue

b = 0
while True:
    b = b + 1
    if (b == 2):
        print("continue")
        continue
    if (b > 6):
        break
    print(b)
# 1
# continue
# 3
# 4
# 5
# 6

 

- 간단하게 반복하는 한 줄 for문

n = [1,3,5,7,9]
s = [i**2 for i in n]
print(s)
# [1, 9, 25, 49, 81]

n = [1,3,5,7,9]
s = [i**2 for i in n if i>=5]
print(s)
# [25, 49, 81]

 

반복 범위가 정해진 반복의 경우 : for문 사용

범위 없이 조건에 따른 반복 수행의 경우 : while문 사용

 

'abt Python > 기초' 카테고리의 다른 글

python 기본 함수  (0) 2023.08.10
python 입력과 출력  (0) 2023.08.10
python 변수와 자료형  (0) 2023.08.01
python을 활용한 연산  (0) 2023.08.01
Python 프로그래밍 기초  (0) 2023.07.31