일단 하자

# 5 - 1. 조건문 본문

파이썬 웹 개발/파이썬 기초 개념

# 5 - 1. 조건문

coredump064 2019. 11. 13. 16:20

if 조건문

1. 조건문 기본형식

2. 관계 연산자 실습(>, >=, <, <=, ==, !=)

3. 논리 연산자 실습(and, or, not)

4. 다중 조건문 (if, elif, else)

5. 중첩 조건문

 

1. 조건문 기본형식

if True:
	print("Yes")

if False: # 조건문의 결과가 False이면 컴파일러가 그냥 pass한다.
	print("No") 

if False:
	print("No")
else:
	print("YesYes")

 

2. 관계 연산자

>, >=, <, <=, ==, !=

a = 10
b = 0

print(a == b)
print(a != b)
print(a > b)
print(a >= b)
print(a < b)
print(a <= b)

 

+ 참, 거진 종류 ***중요***

참 : "내용이있는문자열", [내용이있는리스트], (내용이있는튜플), {내용이있는딕셔너리), 0이 아닌 수,  True

거짓 : "", [], (), {}, 0, False

print("bool(5) ", bool(5))
print("bool([])", bool([]))
print('bool(0.00000000000001)', bool(0.00000000000001))

city = ""
if city:
    print(">>>True")
else:
    print(">>>False")

3. 논리 연산자

and, or, not

a = 100
b = 60
c = 15

print('and :', a > b and b > c)
print('or :', a > b or c > b)
print('not :', not a > b)
print(not False)
print(not True)

 

+ 산술, 관계, 논리 연산자의 연산 순서

산술 -> 관계 -> 논리 순서로 적용

print(5 + 10 > 0 and not 7 + 3 == 10)

# (((5 + 10) > 0) and (not((7 + 3) == 10)))

 

score1 = 90
score2 = 'A'

if score1 >= 90 and score2 == 'A':
	print("합격하셨습니다.")
else:
	print("죄송합니다. 불합격하셨습니다.")

 

4. 다중 조건문

if, elif, else

score = 74

if score >= 90:
	print("A등급", score)
elif score >= 80:
	print("B등급", score)
elif score >= 70:
	print("C등급", score)
else:
	print("꽝", score)

 

5. 중첩 조건문

age = 27
height = 168

if age >= 20:
	if height >= 170:
    	print("A지망 지원 가능")
    elif height >= 160:
    	print("B지망 지원 가능")
    else:
    	print("지원 불가")
else:
	print("20세 이상 지원가능")

'파이썬 웹 개발 > 파이썬 기초 개념' 카테고리의 다른 글

# 5 - 3. 조건문, 반복문 퀴즈  (0) 2019.11.13
# 5 - 2. 반복문  (0) 2019.11.13
# 4 - 5. 자료형 퀴즈  (0) 2019.11.11
# 4 - 4. 딕셔너리 및 집합  (0) 2019.11.10
# 4 - 3. 리스트 및 튜플  (0) 2019.11.10
Comments