사용자 도구

사이트 도구


wiki:ai:python:조건문

조건문

  • description : 조건문
  • author : 도봉산핵주먹
  • email : hylee@repia.com
  • lastupdate : 2020-06-22

조건문

예제 코드

# Section05-1
# 파이썬 흐름제어(제어문)
# 조건문 실습
 
 
print("type(True) :\t",type(True))
print("type(False) :\t",type(False))
 
# 기본 형식
 
# 예1
if True:
    print("if True :\t","Yes")  # 들여쓰기 중요
 
if False:
    # 출력되지 않음.
    print("False :\t","No")
 
# 예2
if False:
    # 여기는 실행되지 않음.
    print("if False :\t","You can't reach here")
else:
    # 여기가 실행된다.
    print("else :\t","Oh, you are here")
 
# 관계연산자
# >, >=, <, <=, ==, !=
 
 
a = 10
b = 0
 
# == 양 변이 같을 때 참.
print("a == b :\t",a == b)
 
# != 양 변이 다를 때 참.
print("a != b :\t",a != b)
 
# > 왼쪽이 클때 참.
print("a > b :\t",a > b)
 
# >= 왼쪽이 크거나 같을 때 참.
print("a >= b :\t",a >= b)
 
# < 오른쪽이 클 때 참.
print("a < b :\t",a < b)
 
# <= 오른쪽이 크거나 같을 때 참.
print("a <= b :\t",a <= b)
 
# 참 거짓 종류
# 참 : "내용", [내용], (내용), {내용}, 1
# 거짓 : "", [], (), {}, 0, None
 
print("#==== 참 거짓 종류 ====")
print("#=== 문자 ===")
city = ""
if city:
    print("if city = '' :\t","You are in:", city)
else:
    # 이쪽이 출력된다.
    print("if city = '' else :\t","Please enter your city")
 
city = "Seoul"
if city:
    print("if city 'Seoul' :\t","You are in:", city)
else:
    # 이쪽이 출력된다.
    print("if city 'Seoul' else :\t","Please enter your city")
 
print()
# 논리연산자
# and, or, not
print("#=== 논리연산자 ===")
 
a = 100
b = 60
c = 15
 
print('and ''a > b and b > c'':\t', a > b and b > c)  # a > b > c
print('or ''a > b or b > c'':\t', a > b or b > c)
print('not ''not a > b'':\t', not a > b)
print('not ''not b > c'':\t', not b > c)
print('not True :\t',not True)
print('not False :\t',not False)
 
# 산술, 관계, 논리 우선순위
# 산술 > 관계 > 논리 순서로 적용
 
print('3 + 12 > 7 + 3 :\t', 3 + 12 > 7 + 3)
print('5 + 10 * 3 > 7 + 3 * 20 :\t', 5 + 10 * 3 > 7 + 3 * 20)
print('5 + 10 > 3 and 7 + 3 == 10 :\t', 5 + 10 > 3 and 7 + 3 == 10)
print('5 + 10 > 0 and not 7 + 3 == 10 :\t', 5 + 10 > 0 and not 7 + 3 == 10)
 
print()
 
print("#=== 복수의 조건 ===")
 
score1 = 90
score2 = 'A'
 
# 복수의 조건이 모두 참일 경우에 실행.
if score1 >= 90 and score2 == 'A':
    print("if score1 >= 90 and score2 == 'A' :\t","합격하셨습니다.")
else:
    print("if score1 >= 90 and score2 == 'A' else :\t","불합격입니다.")
 
id1 = "gold"
id2 = "admin"
grade = 'super'
 
if id1 == "gold" or id2 == "admin":
    print("if id1 == gold or id2 == admin :\t","관리자 로그인 성공")
 
if id2 == "admin" and grade == "super":
    print("if id2 == admin and grade == super :\t","최고 관리자 로그인 성공")
 
is_work = False
 
if not is_work:
    print("if not is_work :\t","is work!")
 
print()
 
print("#=== 다중 조건 ===")
# 다중 조건문
num = 90
 
if num >= 70:
    print("if num >= 70 :\t","num ? ", num)
elif num >= 60:
    print("elif num >= 60 :\t","num ? ", num)
else:
    print("else :\t","default num")
 
print()
 
print("#=== 중첩 조건 ===")
# 중첩 조건문
 
age = 27
height = 175
 
if age >= 20:
    if height >= 170:
        print(" :\t","A지망 지원 가능")
    elif height >= 160:
        print(" :\t","B지망 지원 가능")
    else:
        print(" :\t","지원 불가")
else:
    print(" :\t","20세 이상 지원가능")
 
print()
# in, not in
 
q = [1, 2, 3]
w = {7, 8, 9, 9}
e = {"name": 'Kim', "city": "seoul", "grade": "B"}
r = (10, 12, 14)
 
print("1 in q :\t",1 in q)
print("6 in w :\t",6 in w)
print("12 not in r :\t",12 not in r)
print("name in e :\t","name" in e)  # key 검색
print("seoul in e.values() :\t","seoul" in e.values())  # value 검색

실행 콘솔

type(True) :     <class 'bool'>
type(False) :    <class 'bool'>
if True :        Yes
else :   Oh, you are here
a == b :         False
a != b :         True
a > b :  True
a >= b :         True
a < b :  False
a <= b :         False
#==== 참 거짓 종류 ====
#=== 문자 ===
if city = '' else :      Please enter your city
if city 'Seoul' :        You are in: Seoul
 
#=== 논리연산자 ===
and a > b and b > c:     True
or a > b or b > c:       True
not not a > b:   False
not not b > c:   False
not True :       False
not False :      True
3 + 12 > 7 + 3 :         True
5 + 10 * 3 > 7 + 3 * 20 :        False
5 + 10 > 3 and 7 + 3 == 10 :     True
5 + 10 > 0 and not 7 + 3 == 10 :         False
 
#=== 복수의 조건 ===
if score1 >= 90 and score2 == 'A' :      합격하셨습니다.
if id1 == gold or id2 == admin :         관리자 로그인 성공
if id2 == admin and grade == super :     최고 관리자 로그인 성공
if not is_work :         is work!
 
#=== 다중 조건 ===
if num >= 70 :   num ?  90
 
#=== 중첩 조건 ===
 :       A지망 지원 가능
 
1 in q :         True
6 in w :         False
12 not in r :    False
name in e :      True
seoul in e.values() :    True

Tip

/var/services/web/dokuwiki/data/pages/wiki/ai/python/조건문.txt · 마지막으로 수정됨: 2023/01/13 18:44 (바깥 편집)