Python 정리/제어 플로와 코드 구조

논리 연산자 // IN 과 NOT의 쓰임 // 값이 들어있지 않다는 테크닉

아직미정임 2022. 3. 19. 23:20

논리 연산자

 

a = 1
b = 1

a == b 
# ture

a != b 
#false

#확인 필요
a < b

#확인 필요
a > b


a <= b

a >= b 

a > 0 and b > 0

#a > 0 and b > 0 의 다른 표현
if a > 0:
	if b > 0:
    	print('a and b are positive')
 
a > 0 or b > 0

#a > 0 or b > 0 의 다른 표현
if a > 0:
	print('a or b is positive')
elif b > b:
	print('a or b is positive')

 

 

IN 과 NOT의 쓰임

y = [1,2,3]
x = 1

#y에 x의 값이 들어 있나요?
if x in y:
	print('in)
    
 #in 출력
 
 
 #100이 y안에 들어 있나요?
 if 100 not in y:
 	print('not in')
    
 if not a == b:
 	print('Not equal')
    
#not 보단 주로 !를 쓴다.
if a != b:
	print('Not equal')
    
    
 # Not은 boolean 형을 구분 할 때 사용한다.
 is_ok = True

#js에서 if(! is_ok){}로 표현하는 것과 같다.
if not is_ok:
	print('hello')

 

값이 들어 있지 않다는 테크닉

 

is_ok = True # ok
is_ok = '아무 값' #ok
is_ok = '' # No!
if is_ok:
	print('ok')
else:
	print('No!')
    
    
#같은 표현 

if len(is_ok) > 0:
	print('ok')
else:
	print('No!')
    
  #faㅣse의 예시
  #False, 0, 0.0,'',[],(),{},set()