Python/Python기초

Python 4_ 연산자

Codezoy 2019. 11. 21. 22:34


연산자(Operator)
- 할당(assignment) :    =
- 산술연산(numerical operator): +, -, *, **(제곱), /, //(몫), %(나머지)
- 복합 할당: +=, -=, *=, /=, **=, //=, ...
- 비교 연산: >, >=, <=, <, ==(반드시 등호 두 개), !=
- 논리 연산: and, or, not
- identity 연산: is, is not

산술연산

     print(2**3) # 2*2*2 : 2의 3승
     print(3//2) # 몫
     print(13%3) # 나머지


실행결과
  8
  1
  1

복합할당

     x = 1
     print('x = ', x)

     x += 10 # x = x + 10
     print('x = ', x)


실행결과
x =  1
x =  11

비교 연산

     print(1==2)
     print(1!=2)


실행 결과
False
True

논리 연산

     x = 50
     print(x>0 and x<200)
     print(x<0 or x>30)
print(x>0andx<200

실행 결과
True
True

# Alt + Enter 
     print(x>0andx<200)                       

>>

     print(0 < x < 200)  # 코드가 변환됨


- identity 연산: is, is not
id 함수의 리턴값이 같은 지 다른 지 비교
 >> 나중에 설명