본문 바로가기

파이썬 기초 공부

조건문(If 문)

  • 파이썬 제어문

기본 형식

print(type(True)) # 0이 아닌 수 , "abc", [1, 2, 3] (3, 2, 3) ...
print(type(False)) # 0, "", [], (), {} ... 
# 예1

if True:
    print('Good')

if False:
    print('Bad')

# 예2

if False:
    print('Bad')
else:
    print('Good')

# 관계 연산자
# >, >=, <, <=, ==, !=
x= 15
y= 10

# 양변이 같은 경우 참
print(x == y) ## False

# 앙 변이 다를 때 참
print(x != y) ## True

# 왼쪽이 클 때 창
print(x > y) ## True

# 왼쪽이 크거나 캍을 때 창
print(x >= y) ## True

# 오른쪽이 클 때 창
print(x < y) ## False

# 오른쪽이 크거나 같을 때 창
print(x <= y) ## False

# 예3
city = ""
if city:
    print("You are in:", city)
else:
    print("plz enter your city!")

# 예4
city2 = "Seoul"
if city2:
    print("You are in:", city2)
else:
    print("plz enter your city!")

논리연산자(and, or, not)

# 논리연산자(중요)
# and, or, not

a = 75
b = 40
c = 10


print('and : ', a > b and b > c) # a > b > c
print('or : ', a > b  or b > c) # 앞에서 True면 뒤를 확인 안하고 True 출력
print('not :', not a > b)
print('not :', not b > c)
print(not True)
print(not False)

# 산술, 관계, 논리 우선순위
# 산술 > 관계 > 논리
print('e1 :', 3 + 12 > 7 + 3)
print('e2 :', 5 + 10 * 3 > 7 + 3 * 20)
print('e3 :', 5 + 10 > 3 and 7 + 3 == 10)
print('e3 :', 5 + 10 > 0 and not 7 + 3 == 10)


score1 = 70
score2 = 'A'
# 예4
# 복수의 조건이 모두 참일 경우에 실행
if score1 >= 90 and score2 == 'A':
    print('Pase')
else:
    print('Fail')

# 예5
id1 = 'vip'
id2 = 'admin'
grade = 'platinum'

if id1 == 'vip' or id2 == 'admin':
    print('관리자 입장')

if id2 == 'admin' and grade == 'platinum' :
    print('최고 관리자')

다중, 중첩 조건문

# 예6
# 다중조건문

num  = 90

if num >= 90:
    print('Grade : A')
elif num >= 80:
    print('Grade : B')
elif num >= 70:
    print('Grade : C')
else:
    print('과락')

# 중첩 조건문

grade = 'A'
total = 95

if grade == 'A':
    if total >= 90:
        print('장학금 100%')
    elif total >= 80:
        print('장학금 80%')
    else:
        print('장학금 50%')
else:
    print('과락')


# in, not in

q = [10, 20, 30]
w = {70, 80, 90, 100}
e = {"name": "Lee", "city": "Seoul", "grade": "A"}
r = (10, 12, 14)

print(15 in q)
print(90 in w)
print(12 not in r)
print("name" in e)
print("Seoul" in e.values())

'파이썬 기초 공부' 카테고리의 다른 글

조건문(while 문)  (0) 2023.05.10
조건문(FOR문)  (0) 2023.05.10
파이썬 기초 자료형(집합)  (0) 2023.04.23
파이썬 기초 자료형(딕셔너리)  (0) 2023.04.23
파이썬 기초 자료형(튜플)  (0) 2023.04.23