Python 연산자 - 빠른 참조
Python 연산자를 사용하면 변수에 대한 일반적인 처리를 수행할 수 있습니다. 예제와 연산자 우선 순위를 통해 다양한 유형의 연산자를 살펴보겠습니다. 하나 이상의 피연산자의 값을 조작할 수 있는 특수 기호입니다.
Python 연산자 목록
Python 연산자는 여러 범주로 분류할 수 있습니다.
- 할당 연산자
- 산술 연산자
- 논리 연산자
- 비교 연산자
- 비트 연산자
Python 할당 연산자
대입 연산자에는 기본 대입 연산자 등호(=)가 포함됩니다.
그러나 코드를 단순화하고 중복성을 줄이기 위해 Python에는 산술 할당 연산자도 포함되어 있습니다.
여기에는 더하기 할당에 사용되는 Python의 += 연산자, //= 바닥 나누기 할당 연산자 등이 포함됩니다.
다음은 파이썬의 모든 산술 할당 연산자 목록입니다.
Operator | Description |
---|---|
+= | a+=b is equivalent to a=a+b |
*= | a*=b is equivalent to a=a*b |
/= | a/=b is equivalent to a=a/b |
%= | a%=b is equivalent to a=a%b |
**= | a**=b is equivalent to a=a**b (exponent operator) |
//= | a//=b is equivalent to a=a//b (floor division) |
대입 연산자 사용
# take two variable, assign values with assignment operators
a=3
b=4
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a+b
a+=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a*b
a*=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a/b
a/=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a%b
a%=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a**b ( exponent operator)
a**=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a//b ( floor division)
a//=b
print("a: "+str(a))
print("b: "+str(b))
파이썬 산술 연산자
Operator | Description | Example |
---|---|---|
+ | used to add two numbers | sum = a + b |
– | used for subtraction | difference = a – b |
* | used to multiply two numbers. If a string and int is multiplied then the string is repeated the int times. | mul = a*b>>> “Hi”*5 |
‘HiHiHiHiHi’ | ||
/ | used to divide two numbers | div = b/a |
% | modulus operator, returns the remainder of division | mod = a%b |
** | exponent operator |
#create two variables
a=100
b=200
# addition (+) operator
print(a+b)
# subtraction (-) operator
print(a-b)
# multiplication (*) operator
print(a*b)
# division (/) operator
print(b/a)
# modulus (%) operator
print(a%b) # prints the remainder of a/b
# exponent (**) operator
print(a**b) #prints a^b
파이썬 비교 연산자
Operator | Description | Example |
---|---|---|
== | returns True if two operands are equal, otherwise False. | flag = a == b |
!= | returns True if two operands are not equal, otherwise False. | flag = a != b |
> | returns True if left operand is greater than the right operand, otherwise False. | flag = a > b |
< | returns True if left operand is smaller than the right operand, otherwise False. | flag = a < b |
>= | returns True if left operand is greater than or equal to the right operand, otherwise False. | flag = a > b |
<= | returns True if left operand is smaller than or equal to the right operand, otherwise False. | flag = a < b |
# create two variables
a=100
b=200
# (==) operator, checks if two operands are equal or not
print(a==b)
# (!=) operator, checks if two operands are not equal
print(a!=b)
# (>) operator, checks left operand is greater than right operand or not
print(a>b)
# (<) operator, checks left operand is less than right operand or not
print(a<b)
#(>=) operator, checks left operand is greater than or equal to right operand or not
print(a>=b)
# (<=) operator, checks left operand is less than or equal to right operand or not
print(a<=b)
파이썬 비트 연산자
Operator | Description | Example |
---|---|---|
& | Binary AND Operator | x = 10 & 7 = 2 |
Binary OR Operator | ||
^ | Binary XOR Operator | x = 10 ^ 7 = 13 |
~ | Binary ONEs Compliment Operator | x = ~10 = -11 |
<< | Binary Left Shift operator | x = 10<<1 = 20 |
>> | Binary Right Shift Operator | x = 10>>1 = 5 |
#create two variables
a=10 # binary 1010
b=7 # binary 0111
# Binary AND (&) operator, done binary AND operation
print(a&b)
# Binary OR (|) operator, done binary OR operation
print(a|b)
# Binary XOR (^) operator, done binary XOR operation
print(a^b)
# Binary ONEs Compliment (~) operator, done binary One's Compliment operation
print(~a)
# Binary Left Shift (<<) operator, done binary Left Shift operation
print(a<<1)
# Binary Right Shift (>>) operator, done binary Right Shift operation
print(a>>1)
파이썬 논리 연산자
Operator | Description | Example |
---|---|---|
and | Logical AND Operator | flag = exp1 and exp2 |
or | Logical OR Operator | flag = exp1 or exp2 |
not | Logical NOT Operator | flag = not(True) = False |
#take user input as int
a=int(input())
# logical AND operation
if a%4==0 and a%3==0:
print("divided by both 4 and 3")
# logical OR operation
if a%4==0 or a%3==0:
print("either divided by 4 or 3")
# logical NOT operation
if not(a%4==0 or a%3==0):
print("neither divided by 4 nor 3")
Python 연산자 우선 순위
이러한 연산자의 우선 순위는 연산자의 우선 순위 수준을 의미합니다. 이는 식에 여러 연산자가 있는 경우 매우 중요합니다. 예를 들어 다음 표현식을 고려하십시오.
>>> 2+3*4
자, 일련의 작업은 무엇이라고 생각하십니까? 2와 3을 더한 다음 결과에 4를 곱할 수 있습니다. 또한 먼저 3과 4를 곱한 다음 2를 더할 수 있습니다. 여기에서 연산자의 우선 순위가 중요하다는 것을 알 수 있습니다.
다음은 우선 순위 수준을 나타내는 연산자 목록입니다. 내림차순입니다. 즉, 상위 그룹이 하위 그룹보다 우선 순위가 높습니다.
- 괄호 –
()
- 승승법 –
**
- 칭찬, 단항 더하기 및 빼기 –
~
,+
,-
- 곱하기, 나누기, 모듈로 –
*
,/
,%
- 더하기 및 빼기 –
+
,-
- 오른쪽 및 왼쪽 시프트 –
>>
,<<
- 비트 AND –
&
- 비트 OR 및 XOR –
|
,^
- 비교 연산자 –
==
,!=
,>
,<
,>=
, <코드><= - 할당 연산자-
=