Algorithm/Python 문법
[Python] switch-case문 사용하기
갬미
2022. 1. 5. 10:57
파이썬에는 switch-case 문법이 없다
1학년때 C와 python을 다 배워서 그런지 당연히 있는 문법인데 내가 많이 안쓰는건줄 알았다
python에서 switch-case 문법을 구현하는 방법은 두가지이다
- if - else 활용하기
- Dictionary 이용하기
## if - else
num = 1
if num == 0:
print (0)
elif num == 1:
print (1)
elif num == 2:
print (2)
## dictionary
num = 1
def switch(key):
number = {0:'0', 1:'1', 2:'2'}.get(key, 'unknowen')
print (number)
switch(num)