파이썬 연산자를 사용하면 변수에 대한 일반적인 처리를 수행할 수 있습니다. 예제와 함께 다양한 유형의 연산자와 연산자 우선 순위를 살펴보겠습니다. 이들은 하나 이상의 피연산자의 값을 조작할 수 있는 특수 기호입니다.
파이썬 연산자 목록
파이썬 연산자는 여러 범주로 분류할 수 있습니다.
- 할당 연산자
- 산술 연산자
- 논리 연산자
- 비교 연산자
- 비트 연산자
파이썬 할당 연산자
할당 연산자에는 기본 할당 연산자인 등호(=)가 포함됩니다.
하지만 코드를 단순화하고 중복을 줄이기 위해 파이썬은 산술 할당 연산자도 포함합니다.
여기에는 덧셈 할당에 사용되는 += 연산자와 같은 것들이 포함됩니다. // = 바닥 나눗셈 할당 연산자 및 기타.
다음은 파이썬에서 사용되는 모든 산술 할당 연산자의 목록입니다.
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) |
할당 연산자 사용하기
# 두 변수를 가져와서 할당 연산자를 사용하여 값 할당하기
a=3
b=4
print("a: "+str(a))
print("b: "+str(b))
# 이것은 a=a+b와 동등합니다
a+=b
print("a: "+str(a))
print("b: "+str(b))
# 이것은 a=a*b와 동등합니다
a*=b
print("a: "+str(a))
print("b: "+str(b))
# 이것은 a=a/b와 동등합니다
a/=b
print("a: "+str(a))
print("b: "+str(b))
# 이것은 a=a%b와 동등합니다
a%=b
print("a: "+str(a))
print("b: "+str(b))
# 이것은 a=a**b (지수 연산자)와 동등합니다
a**=b
print("a: "+str(a))
print("b: "+str(b))
# 이것은 a=a//b (내림 나눗셈)와 동등합니다
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 |
# 두 변수를 만듭니다
a=100
b=200
# 덧셈 (+) 연산자
print(a+b)
# 뺄셈 (-) 연산자
print(a-b)
# 곱셈 (*) 연산자
print(a*b)
# 나눗셈 (/) 연산자
print(b/a)
# 나머지 (%) 연산자
print(a%b) # prints the remainder of a/b
# 지수 (**) 연산자
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 |
# (!=) 연산자는 두 피연산자가 동일하지 않은지를 확인합니다
a=int(input())
# (>) 연산자는 왼쪽 피연산자가 오른쪽 피연산자보다 큰지 확인합니다
if a%4==0 and a%3==0:
print("divided by both 4 and 3")
# (<) 연산자는 왼쪽 피연산자가 오른쪽 피연산자보다 작은지 확인합니다또는 아닙니다연산자가
if a%4==0 or a%3==0:
print("either divided by 4 or 3")
# (<=) 연산자는 왼쪽 피연산자가 오른쪽 피연산자보다 작거나 같은지를 확인합니다
if not(a%4==0 or a%3==0):
print("neither divided by 4 nor 3")
Python 비트 연산자
# 두 개의 변수를 생성합니다
>>> 2+3*4
# 이진 AND (&) 연산자, 이진 AND 연산 수행
# 이진 OR (|) 연산자, 이진 OR 연산 수행
- # 이진 XOR (^) 연산자, 이진 XOR 연산 수행
- # 이진 보수 (~) 연산자, 이진 보수 연산 수행
- # 이진 왼쪽 시프트 (<<) 연산자, 수행 이진 왼쪽 시프트 수행
- 파이썬 논리 연산자
- # 사용자 입력을 int로 받습니다
- # 논리 AND 연산
- # 논리 OR 연산
- # 논리 NOT 연산
- 파이썬 연산자 우선순위
- 연산자의 우선순위는 연산식에 여러 연산자가 포함되어 있을 때 중요합니다. 예를 들어 다음과 같은 식을 고려해보세요:
Source:
https://www.digitalocean.com/community/tutorials/python-operators