강의노트 파이썬 조건문 - match

조회수 821 • 댓글 0 • 수정 1개월 전 크게 보기  
  • 조건문
  • match

match 구문

match구문은 표현식과 값들을 비교하여 여러 case 블록중 맞는 것을 찾는다.

python 3.10 이후 버전에서만 사용가능하다.

def http_error(status):
    match status:
        case 400:
            return "Bad request"
        case 404:
            return "Not found"
        case 418:
            return "I'm a teapot"
        case _:
            return "Something's wrong with the internet"

마지막 "case _:"는 와일드 카드로 모든 것과 맞는다.

case와 맞는 것이 없으면 아무 블록도 실행되지 않는다.

var = 1
match var:
    case 0:
        print("var == 0")
    case 1:
        print(f"var = 1")
    case 2:
        print(f"var=2")
    case 3:
        print("var =3")
    case _:
        print("Some other value")
이전 글
다음 글
댓글
댓글로 소통하세요.