Python
[Python] 바다 코끼리에게 연산을 맡겨보자 Walrus operator :=
insub4067
2021. 11. 22. 18:28
바다 코끼리 연산자는 파이썬 3.8에 추가된 기능으로
정식 명칭은 "Assignment Expression" (대입 표현식) 이다.
# 기존 방식의 if문
myList = [1, 2, 3, 4, 5]
num = int(input("Choose a number between 0, 10"))
if num in myList:
print("Number is in the list")
--------------------------------
# Walrus 사용할 경우
myList = [1,2,3,4,5]
if (num := int(input("Choose a number between 0, 10"))) in myList:
print(f"Number is in the list")
# 기존 방식의 while문
password = 1234
while int(input("Password: ")) != password:
print("Password is encorrect")
--------------------------------
# Walrus Operator
password = 1234
while Input := int(input("Password: ")) != password:
print("Password is encorrect")
결론:
귀엽긴 한데 솔직히 왜 써야하는지 모르겠다.
좋은 사용 방법이나 왜 쓰는지 이유 아시는 법
댓글 부탁드립니다.