-
삼항 연산자(Ternary operator)개발/Python 2022. 7. 18. 22:48
1. 3항 연산자
if-else와 같지만 한 줄로 표현
2. 3항 연산자 Syntax
기존의 if else 문을 통하면 아래와 같습니다.
a, b = 100, 200 if a > b: max_num = a else: max_num = b
Ternary operator를 사용하여 구현하면 조금 더 직관적으로 구현이 됩니다.
# [true] if [expression] else [false] a, b = 100, 200 max_num = a if a > b else b
Reference
https://www.geeksforgeeks.org/data-types/
Ternary Operator in Python - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
'개발 > Python' 카테고리의 다른 글
[Python] - Powerful Function (0) 2022.07.27 [Python] - Set (0) 2022.07.27 [Python] - Dictionary (0) 2022.07.27 [Python] - Tuple (0) 2022.07.27 [Python] - List (0) 2022.07.27