파이썬 연산자
파이썬 연산자
연산자는 변수와 값에 대한 연산을 수행하는 데 사용됩니다.
아래 예에서는 +
연산자를 사용하여 두 값을 더합니다.
예시
print(10 + 5)
Python은 연산자를 다음 그룹으로 나눕니다.
- 산술 연산자
- 할당 연산자
- 비교 연산자
- 논리 연산자
- ID 연산자
- 회원 운영자
- 비트 연산자
파이썬 산술 연산자
산술 연산자는 일반적인 수학 연산을 수행하기 위해 숫자 값과 함께 사용됩니다.
Operator | Name | Example | Try it |
---|---|---|---|
+ | Addition | x + y | |
- | Subtraction | x - y | |
* | Multiplication | x * y | |
/ | Division | x / y | |
% | Modulus | x % y | |
** | Exponentiation | x ** y | |
// | Floor division | x // y |
파이썬 할당 연산자
할당 연산자는 변수에 값을 할당하는 데 사용됩니다.
Operator | Example | Same As | Try it |
---|---|---|---|
= | x = 5 | x = 5 | |
+= | x += 3 | x = x + 3 | |
-= | x -= 3 | x = x - 3 | |
*= | x *= 3 | x = x * 3 | |
/= | x /= 3 | x = x / 3 | |
%= | x %= 3 | x = x % 3 | |
//= | x //= 3 | x = x // 3 | |
**= | x **= 3 | x = x ** 3 | |
&= | x &= 3 | x = x & 3 | |
|= | x |= 3 | x = x | 3 | |
^= | x ^= 3 | x = x ^ 3 | |
>>= | x >>= 3 | x = x >> 3 | |
<<= | x <<= 3 | x = x << 3 |
파이썬 비교 연산자
비교 연산자는 두 값을 비교하는 데 사용됩니다.
Operator | Name | Example | Try it |
---|---|---|---|
== | Equal | x == y | |
!= | Not equal | x != y | |
> | Greater than | x > y | |
< | Less than | x < y | |
>= | Greater than or equal to | x >= y | |
<= | Less than or equal to | x <= y |
파이썬 논리 연산자
논리 연산자는 조건문을 결합하는 데 사용됩니다.
Operator | Description | Example | Try it |
---|---|---|---|
and | Returns True if both statements are true | x < 5 and x < 10 | |
or | Returns True if one of the statements is true | x < 5 or x < 4 | |
not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
파이썬 아이덴티티 연산자
동일성 연산자는 개체를 비교하는 데 사용됩니다. 개체가 동일한 경우가 아니라 실제로 동일한 개체이고 메모리 위치가 동일한 경우입니다.
Operator | Description | Example | Try it |
---|---|---|---|
is | Returns True if both variables are the same object | x is y | |
is not | Returns True if both variables are not the same object | x is not y |
Python 멤버십 연산자
멤버쉽 연산자는 시퀀스가 개체에 표시되는지 테스트하는 데 사용됩니다.
Operator | Description | Example | Try it |
---|---|---|---|
in | Returns True if a sequence with the specified value is present in the object | x in y | |
not in | Returns True if a sequence with the specified value is not present in the object | x not in y |
파이썬 비트 연산자
비트 연산자는 (이진) 숫자를 비교하는 데 사용됩니다.
Operator | Name | Description |
---|---|---|
& | AND | Sets each bit to 1 if both bits are 1 |
| | OR | Sets each bit to 1 if one of two bits is 1 |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off |
>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |