파이썬의 연산자는 다음의 7개 카테고리로 나눌 수 있다.
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
[Code Examples] 🤪:
Arithmetic operators : to perform common mathmatical operations
# Arithmetic operators : to perform common mathematical operations
x=13;
y=2;
print(x+y); #15
print(x-y); #11
print(x*y); #26
print(x/y); #6.5
#Modulus 나머지
print(x%y); #1
#Exponentiation 제곱
print(x**y); #169
#Floor division : rounds the result down to the nearest whole number
print(x//y); #6
Assignment operators : to assign values to variables
# Assignment operators : to assign values to variables
x=6;
x+=1;
print(x); #7
x-=1;
print(x); #6
x*=2;
print(x); #12
x/=2;
print(x); #6.0
x%=4;
print(x); #2.0
x//=2;
print(x); #1.0
x**=2;
print(x); #1.0
#Bitwise operators
y=3; #11
y&=2; #10
print(y); #2
y|=2; #
print(y); #2
y^=2;
print(y); #0
z=4;
z>>=2;
print(z);#1
z<<=2;
print(z);#4
Comparison operators : to compare two values
# Comparison operators : to compare two values
x=2;
y=3;
print(x==y);
print(x!=y);
print(x>y);
print(x<y);
print(x<=y);
print(x>=y);
Logical operators : used to combine conditional statements
# Logical operators : used to combine conditional statements
x=1;
y=5;
print(x>y and x<10); #False
print(x<y or x>10); #True
print(not(x<5 and x<10)); #False
Identity operators : to compare the objects, not if they are equal but if they are actually the same object, with the same memory location
# Identity operators : to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location
x=["a","b","c"]
y=["a","b","c"]
x=z
print(x is z); #True
print(x is y); #False because x is not the same object as y, even if they have the same content
print(x is not y); #True
Membership operators : to test if a sequence is presented in an object
#Membership operators : to test if a sequence is presented in an object
s=set([1,2,3])
print(3 in s); #True
print(4 in s); #False
Bitwise operators : to compare (binary) numbers
# Bitwise operators : to compare (binary) numbers
x=4; #0100
y=2; #0010
print(x&y); #0 0000
print(x|y); #6 0110
print(x^y); #6 0110
print(~x); #-5 1011 0100+1=0101(음수 결과값을 알 기 위해선 보수의 보수를 구하자)
print(~y); #-3 1101 0010+1=0011
print(x<<2); #16 10000
print(y>>1); #1 001
'Python🍍' 카테고리의 다른 글
Python Lambda🍋 (0) | 2022.06.27 |
---|---|
Python Function✨ (0) | 2022.06.27 |
Python 반복문 while, for⚡ (0) | 2022.06.27 |
Python 조건문 If🙄 (0) | 2022.06.27 |
Python Data Types💥 (0) | 2022.06.25 |