>>> num = 1
>>> if num == 1 :
... 	print("num : 1")
... elif num == 2 :
... 	print("num : 2")
... else :
... 	print("out of range")
num : 1

# and, or, not
>>> True and True
True 
>>> True or False
True
>>> not True
False

# 숫자
>>> if 0 : print("out 1")
... else : print("out 2")
out 2
# 0 : False
# 0 이외의 수 : True

# bool(var) : var 가 Ture/False인지 반환
>>> bool(0)
False
>>> bool("")
False
>>> bool([])
False

 

[ 1줄 if/else ]

참_표현식  if  조건문  else  거짓_표현식

>>> if x == 1 :
>>>     cell = 'X'
>>> else :
>>>     cell = '0'
>>> 
>>> cell = 'X' if x==1 else '0' # c의 3항 연산자와 비슷하다.

'Python > 기초이론' 카테고리의 다른 글

Python - 튜플  (0) 2022.12.25
Python - for, while  (0) 2022.12.24
Python - 문자열  (0) 2022.12.10
Python - 리스트  (0) 2022.12.10
Python - 기본이론  (0) 2022.12.05

+ Recent posts