syntactic sugar
코드를 간단하게 줄여준다.
1. 다중할당
여러 변수에 값을 한꺼번에 할당할 수 있다.
cat = dog = bird = "animal"
2. 변수값 바꾸기(Swap)
# 두 변수의 값을 교환하기 위해 빈공간에 넣었다가 치환해줌
temp = ''
food = "chair"
furniture = "banana"
print(food, furniture)
temp = food
food = furniture
furniture = food
print(food, furniture)
파이썬은 변수값을 바로 넘겨 줄 수 있음
temp = 'chair'
food = ''
furniture = 'banana'
print(f'temp: {temp}, food: {food}, furniture: {furniture}')
temp, food, furniture = food, furniture, temp
# 변수명 = 변수명이 갖게될 값
print(f'temp: {temp}, food: {food}, furniture: {furniture}')
3. FT
int(False) = 0, int(True) = 1을 갖는다.
print('Phone'[False]) # 출력값 P
print('Phone'[True]) # 출력값 h
...쓸 일이 있을까?
4. 비교 연산자 합치기
def get_out(age):
if 0 < age < 20:
print("집에 가라")
get_out(12)
5. 삼항 연산자
def catch(mouse):
mouse = [print("잡았다") if 0 < mouse < 3 else print("놓쳤다")]
# x = [ if_실행문 if if_조건문 esle else_실행문]
catch(5)
'Python' 카테고리의 다른 글
[Python] 넘파이(Numpy) (0) | 2023.04.16 |
---|---|
[Python] return과 print (1) | 2023.03.24 |
[Python] 데코레이터(Decorator) (0) | 2023.03.23 |
[Python] 클로저(Closure) (0) | 2023.03.23 |
[Python] 클래스 if __name__ =="__main__": (0) | 2023.03.23 |