KR_Operator - somaz94/python-study GitHub Wiki
๊ธฐ๋ณธ์ ์ธ ์ํ ์ฐ์ฐ์ ์ํํ๋ ์ฐ์ฐ์์ด๋ค.
# ๊ธฐ๋ณธ ์ฐ์ ์ฐ์ฐ์
x = 10
y = 3
print(x + y) # 13 (๋ง์
)
print(x - y) # 7 (๋บ์
)
print(x * y) # 30 (๊ณฑ์
)
print(x / y) # 3.333... (๋๋์
)
print(x // y) # 3 (๋ชซ)
print(x % y) # 1 (๋๋จธ์ง)
print(x ** y) # 1000 (๊ฑฐ๋ญ์ ๊ณฑ)
โ ํน์ง:
- ๊ธฐ๋ณธ์ ์ธ ์ํ ์ฐ์ฐ ์ง์
- ๋๋์ ๊ฒฐ๊ณผ๋ ํญ์ float
- ์ ์ ๋๋์ ์ // ์ฐ์ฐ์ ์ฌ์ฉ
๋ ๊ฐ์ ๋น๊ตํ์ฌ True/False๋ฅผ ๋ฐํํ๋ค.
x = 5
y = 3
print(x == y) # False (๊ฐ์)
print(x != y) # True (๋ค๋ฆ)
print(x > y) # True (ํฌ๋ค)
print(x < y) # False (์๋ค)
print(x >= y) # True (ํฌ๊ฑฐ๋ ๊ฐ๋ค)
print(x <= y) # False (์๊ฑฐ๋ ๊ฐ๋ค)
โ ํน์ง:
- ๋น๊ต ๊ฒฐ๊ณผ๋ ํญ์ ๋ถ๋ฆฌ์ธ
- ๋ชจ๋ ๋ฐ์ดํฐ ํ์ ์ ์ฌ์ฉ ๊ฐ๋ฅ
- ์ฒด์ธ ๋น๊ต ๊ฐ๋ฅ (์: a < b < c)
๋ถ๋ฆฌ์ธ ๊ฐ๋ค์ ์กฐํฉํ๋ ์ฐ์ฐ์์ด๋ค.
x = True
y = False
print(x and y) # False (๋
ผ๋ฆฌ๊ณฑ)
print(x or y) # True (๋
ผ๋ฆฌํฉ)
print(not x) # False (๋
ผ๋ฆฌ๋ถ์ )
โ ํน์ง:
- and, or, not ํค์๋ ์ฌ์ฉ
- ๋จ๋ฝ ํ๊ฐ(short-circuit) ์ง์
- ๋ถ๋ฆฌ์ธ์ด ์๋ ๊ฐ๋ ํ๊ฐ ๊ฐ๋ฅ
๋ณ์์ ๊ฐ์ ํ ๋นํ๋ ์ฐ์ฐ์์ด๋ค.
x = 10 # ๊ธฐ๋ณธ ํ ๋น
x += 5 # x = x + 5
x -= 3 # x = x - 3
x *= 2 # x = x * 2
x /= 4 # x = x / 4
x //= 2 # x = x // 2
x %= 3 # x = x % 3
x **= 2 # x = x ** 2
โ ํน์ง:
- ๋ณตํฉ ํ ๋น ์ฐ์ฐ์ ์ง์
- ๋ชจ๋ ์ฐ์ ์ฐ์ฐ์์ ๊ฒฐํฉ ๊ฐ๋ฅ
- ์ฐ์ฐ๊ณผ ํ ๋น์ ๋์์ ์ํ
๋นํธ ๋จ์์ ์ฐ์ฐ์ ์ํํ๋ค.
x = 60 # 0011 1100
y = 13 # 0000 1101
print(x & y) # 12 (AND)
print(x | y) # 61 (OR)
print(x ^ y) # 49 (XOR)
print(~x) # -61 (NOT)
print(x << 2) # 240 (์ผ์ชฝ ์ํํธ)
print(x >> 2) # 15 (์ค๋ฅธ์ชฝ ์ํํธ)
โ ํน์ง:
- ์ ์ํ์ ๋ํด ๋นํธ ๋จ์ ์ฐ์ฐ
- ๋นํธ ์ํํธ ์ฐ์ฐ ์ง์
- 2์ง์ ์ฒ๋ฆฌ์ ์ ์ฉ
์ํ์ค ๋ด์ ํฌํจ ์ฌ๋ถ๋ฅผ ํ์ธํ๋ค.
lst = [1, 2, 3, 4, 5]
print(3 in lst) # True
print(6 not in lst) # True
โ ํน์ง:
- in๊ณผ not in ์ฐ์ฐ์ ์ ๊ณต
- ๋ฆฌ์คํธ, ํํ, ๋ฌธ์์ด ๋ฑ์ ์ฌ์ฉ
- ๋์ ๋๋ฆฌ๋ ํค ๊ฒ์์ ์ฌ์ฉ
๊ฐ์ฒด์ ๋์ผ์ฑ์ ํ์ธํ๋ค.
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a is c) # True (๊ฐ์ ๊ฐ์ฒด)
print(a is b) # False (๋ค๋ฅธ ๊ฐ์ฒด)
print(a is not b) # True (๋ค๋ฅธ ๊ฐ์ฒด)
# ์ฃผ์: == vs is
print(a == b) # True (๊ฐ์ด ๊ฐ์)
print(a is b) # False (๋ค๋ฅธ ๊ฐ์ฒด)
โ ํน์ง:
- is์ is not ์ฐ์ฐ์ ์ ๊ณต
- ๊ฐ์ฒด์ ID๋ฅผ ๋น๊ต
- == ์ฐ์ฐ์์์ ์ฐจ์ด ์ดํด ํ์
- None ์ฒดํฌ์ ์์ฃผ ์ฌ์ฉ (x is None)
# ์ฐ์ ์์ ์์
result = 2 + 3 * 4 # 14 (๊ณฑ์
๋จผ์ )
โ ์ฐ์ ์์ ์์:
- ** (๊ฑฐ๋ญ์ ๊ณฑ)
- +x, -x (๋จํญ ์ฐ์ฐ์)
- *, /, //, % (๊ณฑ์ , ๋๋์ )
- +, - (๋ง์ , ๋บ์ )
- <<, >> (๋นํธ ์ํํธ)
- & (๋นํธ AND)
- ^ (๋นํธ XOR)
- | (๋นํธ OR)
- ๋น๊ต ์ฐ์ฐ์ (<, >, <=, >=, ==, !=)
- is, is not, in, not in (์์ด๋ดํฐํฐ, ๋ฉค๋ฒ์ญ)
- not (๋ ผ๋ฆฌ NOT)
- and (๋ ผ๋ฆฌ AND)
- or (๋ ผ๋ฆฌ OR)
๋ ผ๋ฆฌ ์ฐ์ฐ์์ ์ค์ํ ํน์ฑ์ธ ๋จ๋ฝ ํ๊ฐ์ ๋ํด ์์ธํ ์์๋ณด์.
# and ์ฐ์ฐ์์ ๋จ๋ฝ ํ๊ฐ
x = False
y = print("y๊ฐ ํ๊ฐ๋จ") or True # y๊ฐ ํ๊ฐ๋จ
result = x and y # y๋ ํ๊ฐ๋์ง ์์
print(result) # False
# or ์ฐ์ฐ์์ ๋จ๋ฝ ํ๊ฐ
x = True
y = print("y๊ฐ ํ๊ฐ๋จ") or True # y๊ฐ ํ๊ฐ๋์ง ์์
result = x or y # y๋ ํ๊ฐ๋์ง ์์
print(result) # True
# ์ค์ฉ์ ์ธ ํ์ฉ
def get_user():
print("์ฌ์ฉ์ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ")
return {"name": "ํ๊ธธ๋"}
# ๊ธฐ์กด ์ฌ์ฉ์๊ฐ ์์ผ๋ฉด ๊ฐ์ ธ์ค๊ณ , ์์ผ๋ฉด ์๋ก ์์ฑ
user = existing_user or get_user()
# ์์ ํ ์์ฑ ์ ๊ทผ
name = user and user.get('name')
# ๊ธฐ๋ณธ๊ฐ ์ค์
default_config = {"timeout": 30, "retries": 3}
config = user_config or default_config
โ ๋จ๋ฝ ํ๊ฐ์ ํน์ง:
-
and
์ฐ์ฐ์: ์ฒซ ๋ฒ์งธ ๊ฐ์ด ๊ฑฐ์ง์ด๋ฉด ๋ ๋ฒ์งธ ๊ฐ์ ํ๊ฐํ์ง ์์ -
or
์ฐ์ฐ์: ์ฒซ ๋ฒ์งธ ๊ฐ์ด ์ฐธ์ด๋ฉด ๋ ๋ฒ์งธ ๊ฐ์ ํ๊ฐํ์ง ์์ - ํจ์จ์ฑ ํฅ์ ๋ฐ ์ค๋ฅ ๋ฐฉ์ง์ ์ ์ฉ
- ์กฐ๊ฑด๋ถ ์คํ, ๊ธฐ๋ณธ๊ฐ ์ค์ ๋ฑ์ ํ์ฉ
ํ์ด์ฌ์ ํด๋์ค์์ ์ฐ์ฐ์์ ๋์์ ์ฌ์ ์ํ ์ ์๋ค.
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
# + ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
# - ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ
def __sub__(self, other):
return Vector(self.x - other.x, self.y - other.y)
# * ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ (์ค์นผ๋ผ ๊ณฑ)
def __mul__(self, scalar):
return Vector(self.x * scalar, self.y * scalar)
# == ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ
def __eq__(self, other):
return self.x == other.x and self.y == other.y
# ๋ฌธ์์ด ํํ
def __str__(self):
return f"Vector({self.x}, {self.y})"
# ์ฌ์ฉ ์์
v1 = Vector(1, 2)
v2 = Vector(3, 4)
v3 = v1 + v2 # Vector(4, 6)
v4 = v1 - v2 # Vector(-2, -2)
v5 = v1 * 3 # Vector(3, 6)
print(v3)
print(v4)
print(v5)
print(v1 == Vector(1, 2)) # True
โ ์ฃผ์ ํน์ ๋ฉ์๋:
-
__add__
: + ์ฐ์ฐ์ -
__sub__
: - ์ฐ์ฐ์ -
__mul__
: * ์ฐ์ฐ์ -
__truediv__
: / ์ฐ์ฐ์ -
__floordiv__
: // ์ฐ์ฐ์ -
__mod__
: % ์ฐ์ฐ์ -
__pow__
: ** ์ฐ์ฐ์ -
__eq__
: == ์ฐ์ฐ์ -
__lt__
: < ์ฐ์ฐ์ -
__le__
: <= ์ฐ์ฐ์ -
__gt__
: > ์ฐ์ฐ์ -
__ge__
: >= ์ฐ์ฐ์
# ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ค๋ฅธ ๊ฐ ํ ๋น
x = 10
result = "์ง์" if x % 2 == 0 else "ํ์"
print(result) # ์ง์
# ์ค์ฒฉ ์ผํญ ์ฐ์ฐ์
score = 85
grade = "A" if score >= 90 else "B" if score >= 80 else "C" if score >= 70 else "D"
print(grade) # B
# ๋ฆฌ์คํธ ํ๊ธฐ
a, b, c = [1, 2, 3]
print(a, b, c) # 1 2 3
# ๋๋จธ์ง ์์ ๋ชจ์ผ๊ธฐ (Python 3.x)
first, *middle, last = [1, 2, 3, 4, 5]
print(first) # 1
print(middle) # [2, 3, 4]
print(last) # 5
# ๋์
๋๋ฆฌ ํ๊ธฐ
person = {'name': 'ํ๊ธธ๋', 'age': 30}
print("{name}๋์ {age}์ธ์
๋๋ค.".format(**person)) # ํ๊ธธ๋๋์ 30์ธ์
๋๋ค.
# ๊ธฐ์กด ๋ฐฉ์
numbers = [1, 2, 3, 4, 5]
filtered = []
for num in numbers:
squared = num ** 2
if squared > 10:
filtered.append(squared)
print(filtered) # [16, 25]
# ์์ฆ ์ฐ์ฐ์ ์ฌ์ฉ (Python 3.8+)
numbers = [1, 2, 3, 4, 5]
filtered = [squared for num in numbers if (squared := num ** 2) > 10]
print(filtered) # [16, 25]
# while ๋ฃจํ์์ ํ์ฉ
while (line := input("์
๋ ฅํ์ธ์ (์ข
๋ฃํ๋ ค๋ฉด 'q'): ")) != 'q':
print(f"์
๋ ฅ๊ฐ: {line}")
# ๋นํธ ํ๋๊ทธ ์ฌ์ฉ
READ = 1 # 0001
WRITE = 2 # 0010
EXECUTE = 4 # 0100
DELETE = 8 # 1000
# ๊ถํ ๋ถ์ฌ
permission = READ | WRITE # 0011 (3)
# ๊ถํ ํ์ธ
has_read = permission & READ # 0001 (1) - ์์
has_execute = permission & EXECUTE # 0000 (0) - ์์
print(f"Read ๊ถํ: {bool(has_read)}") # True
print(f"Execute ๊ถํ: {bool(has_execute)}") # False
# ๊ถํ ์ถ๊ฐ
permission |= EXECUTE # 0111 (7)
print(f"Execute ๊ถํ ์ถ๊ฐ ํ: {bin(permission)}") # 0b111
# ๊ถํ ์ ๊ฑฐ
permission &= ~DELETE # 0111 & ~1000 = 0111 (7)
print(f"Delete ๊ถํ ์ ๊ฑฐ ํ์ธ: {bin(permission)}") # 0b111
# ํน์ ๋นํธ๋ง ํ ๊ธ(๋ณ๊ฒฝ)
permission ^= WRITE # 0111 ^ 0010 = 0101 (5)
print(f"Write ๊ถํ ํ ๊ธ ํ: {bin(permission)}") # 0b101
from functools import reduce
from operator import add, mul, itemgetter, attrgetter
# ๋ฆฌ์คํธ ํฉ๊ณ ๊ณ์ฐ
numbers = [1, 2, 3, 4, 5]
sum_result = reduce(add, numbers)
print(f"ํฉ๊ณ: {sum_result}") # 15
# ๋ฆฌ์คํธ ๊ณฑ ๊ณ์ฐ
product_result = reduce(mul, numbers, 1)
print(f"๊ณฑ: {product_result}") # 120
# ๊ฐ์ฒด ๋ฆฌ์คํธ ์ ๋ ฌ
people = [
{'name': 'ํ๊ธธ๋', 'age': 20},
{'name': '๊น์ฒ ์', 'age': 35},
{'name': '์ด์ํฌ', 'age': 25}
]
# ๋์ด์ ์ ๋ ฌ
age_sorted = sorted(people, key=itemgetter('age'))
print("๋์ด์ ์ ๋ ฌ:", [p['name'] for p in age_sorted]) # ['ํ๊ธธ๋', '์ด์ํฌ', '๊น์ฒ ์']
# ์ด๋ฆ์ ์ ๋ ฌ
name_sorted = sorted(people, key=itemgetter('name'))
print("์ด๋ฆ์ ์ ๋ ฌ:", [p['name'] for p in name_sorted]) # ['๊น์ฒ ์', '์ด์ํฌ', 'ํ๊ธธ๋']
# ํด๋์ค ์์ฑ์ผ๋ก ์ ๋ ฌ
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return f"Person('{self.name}', {self.age})"
people_objs = [
Person('ํ๊ธธ๋', 20),
Person('๊น์ฒ ์', 35),
Person('์ด์ํฌ', 25)
]
age_sorted_objs = sorted(people_objs, key=attrgetter('age'))
print("๋์ด์ ์ ๋ ฌ(๊ฐ์ฒด):", age_sorted_objs)
# with ๋ฌธ์ ๋ด๋ถ์ ์ผ๋ก __enter__์ __exit__ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉ
class Timer:
def __enter__(self):
import time
self.start = time.time()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
import time
self.end = time.time()
self.interval = self.end - self.start
print(f"์คํ ์๊ฐ: {self.interval:.6f}์ด")
return False # ์์ธ ์ ํ
# ์ฌ์ฉ ์์
with Timer() as timer:
# ์๊ฐ ์ธก์ ํ ์ฝ๋
total = sum(range(1000000))