Backend/Python

파이썬 흐름제어 (1) - if 문

meong_j 2021. 9. 14. 18:44
728x90
반응형
 if 구문 - 출력하기
#파이썬 제어문
# IF 실습

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

# 예1

if True:
    print('Good') #  파이썬은 들여쓰기 (indent) 안하면 에러

if False:
    print('Bad')

# 예2
if False:
    print('Bad!!!')
else:
    print('Good!!!')


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

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

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

print(x > y)

print(x <= y)

print()

city = ""

if city:
    print("You are in : ", city)
else:
    print("Please enter your city")


city2 = "seoul"

if city2:
    print("You are in : ", city2)
else:
    print("Please enter your city")


print()

 

 

 

 

논리연산자 - 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)
print('not:', not a > b)
print('not:', not b < c)
print(not True)
print(not False)

print()

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


print()

 

 

 

 

 

score1 = 90
score2 = 'A'


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

print()

 

 

 

 

# 예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('과락')
    

print()

#중첩 조건문

grade = 'A'
total = 95

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


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())

 

 

반응형