파이썬 기초를 복습하는 내용입니다.
1. 산술연산자(arithmetic operator)
+ | 사칙연산자 더하기 | a + b = 27 (a=18, b=2) |
- | 사칙연산자 빼기 | a - b = 9 |
* | 사칙연산자 곱하기 | a * b = 162 |
/ | 사칙연산자 나누기 | a / b = 2 |
** | 제곱 | a ** b = 8 (a=2, b=3) |
// | 왼쪽 항을 오른쪽 항으로 나눈 정수몫 | a // b = 7 (a=30, b=4) |
% | 왼쪽 항을 오른쪽 항으로 나눈 나머지 | a % b = 2 |
2. 대입연산자(assignment operators)
연산과 할당을 합쳐놓은 연산자입니다. 변수에 연산한 값을 대입할 때 간결하게 사용이 가능합니다.
= | 왼쪽 항에 오른쪽 항을 대입한다 |
+= | 왼쪽 항에 오른쪽 항의 값을 더하고, 왼쪽 항에 대입한다 |
-= | 왼쪽 항에 오른쪽 항의 값을 빼고, 왼쪽 항에 대입한다. |
*= | 왼쪽 항에 오른쪽 항의 값을 곱하고, 왼쪽 항에 대입한다. |
/= | 왼쪽 항에 오른쪽 항의 값을 나누고, 왼쪽 항에 대입한다. |
%= | 왼쪽 항을 오른쪽 항의 값으로 나눈 나머지를 왼쪽 항에 대입한다. |
**= | 왼쪽 항에 오른쪽 항의 값만큼 제곱하고, 왼쪽 항에 대입한다 |
//= | 왼쪽 항을 오른쪽 항의 값으로 나눈 몫을 왼쪽 항에 대입한다. |
3. 비교 연산자(comparison operator)
관계 연산자(Relational Operators) 또는 비교 연산자라 불리며, 대소 관계와 상등 관계를 나타내는 연산자입니다. 파이썬의 경우 참일 경우 True, 거짓일 경우 False로 결과값이 논리값('bool', 'boolean')으로 반환됩니다
== | 왼쪽 항과 오른쪽 항의 값이 같을 경우 참 | 같다 |
!= | 왼쪽 항과 오른쪽 항의 값이 다를 경우 참 | 같지 않다 |
> | 왼쪽 항이 오른쪽 항보다 큰 경우 참 | 크다 |
< | 왼쪽 항이 오른쪽 항보다 작은 경우 참 | 작다 |
>= | 왼쪽 항이 오른쪽 항보다 크거나 같을 경우 참 | 크거나 같다 |
<= | 왼쪽 항이 오른쪽 항보다 작거나 같을 경우 참 | 작거나 같다 |
4. 논리 연산자(logical operator)
주어진 식의 논리 값(True, False)을 판단해주는 연산자 입니다.
a | b | a and b | a or b | not(a) |
T | T | T | T | F |
T | F | F | T | F |
F | T | F | T | T |
F | F | F | F | T |
5. 비트 연산자(bitwise operator)
비트단위 논리 연산을 진행할 때 사용한다.
& | 두항의 비트를 비교 같은 자리의 비트가 모두 1일 경우 1을 반환 | 비트 AND |
| | 두항의 비트를 비교 같은 자리의 비트중 1이 존재할 경우 1을 반환 | 비트 OR |
^ | 두항의 같은 자리의 비트가 서로 다를 경우 1을 반환 | 비트 XOR |
~ | 비트가 1일 경우 0으로, 0일 경우 1로 반환 | 비트 NOT |
<< | 지정한 수만큼 비트를 왼쪽으로 이동 | left shift |
>> | 지정한 수만큼 비트를 오른쪽으로 이동 | right shift |
6. 멤버 연산자(membership operator)
멤버 연산자의 경우 list안에 확인하고자 하는 값의 포함 여부를 확인할 때 사용한다.
in | 왼쪽항이 오른쪽 리스트 안에 들어 있으면 참 |
not in | 왼쪽항이 오른쪽 리스트 안에 안들어 있으면 참 |
7.식별 연산자(identity operator)
식별 연산자는 두 항의 값이나 메모리의 위치(주소)를 지녔는지 비교하여 참 거짓을 반환한다.
is | 개체메모리 위치나 값이 같다면 참 |
is not | 개체메모리 위치나 값이 같지 않다면 참 |
'Language > Python' 카테고리의 다른 글
[백준 문제 풀이] 입출력과 사칙연산 (1) | 2023.03.21 |
---|