본문 바로가기
Python/기본

[Python] 조건문 if, match-case

by 카피마스터 2024. 9. 1.

조건문은 상황에 따라 분기 처리를 하고 싶은 경우 사용

스코프는 C와 다르게 중괄호를 사용하지 않고 들여쓰기로 설정

if

# command에 설정된 값에 따른 분기 처리
command = 1

if command == 1:
    print("play - 1")   # 1일 경우 처리
    print("같은 들여쓰기 위치까지 실행")
elif command == 2:
    print("play - 2")   # 2일 경우 처리
else:
    print("??")         # 그 외의 경우 처리

 

 

match - case

C의 switch처럼 사용

# command에 설정된 값에 따른 분기 처리
command = 1

match command:
    case 1:
        print("play - 1")     # 1일 경우 처리
    case 2:
        print("play - 2")     # 2일 경우 처라
    case _:
        print("??")         # 그 외의 경우 처리

 

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

[Python] 클래스  (0) 2024.09.07
[Python] 함수 정의 및 호출  (0) 2024.09.07
[Python] 변수  (0) 2024.08.31
[Python] VSCode에서 Python 사용(Mac)  (1) 2024.08.19
[Python] 기본 사용  (0) 2024.08.19