KR_Number - somaz94/python-study GitHub Wiki
μ«μνμ μ«μ ννλ‘ μ΄λ£¨μ΄μ§ μλ£νμ΄λ€. μ°λ¦¬κ° νν μ¬μ©νλ 123κ³Ό κ°μ μ μ, 12.34μ κ°μ μ€μ, 8μ§μλ 16μ§μκ° ν¬ν¨λλ€.
μ’ λ₯ | Python μ¬μ© μ | μ€λͺ |
---|---|---|
μ μ | 123, -345, 0 | μμ μ μ, μμ μ μ, 0 (ν¬κΈ° μ ν μμ) |
μ€μ | 123.45, -1234.5, 3.4e10 | μμμ μ΄ ν¬ν¨λ μ«μ |
2μ§μ | 0b1010, 0b111 | 0b μ λμ΄λ‘ μμνλ 2μ§μ |
8μ§μ | 0o34, 0o25 | 0o μ λμ΄λ‘ μμνλ 8μ§μ |
16μ§μ | 0x2A, 0xFF | 0x μ λμ΄λ‘ μμνλ 16μ§μ |
볡μμ | 3+4j | μ€μλΆμ νμλΆλ‘ ꡬμ±λ 볡μμ |
νΉμκ° | float('inf'), float('nan') | 무νλ, μ«μκ° μλ κ° |
μ μν(integer)μ λ§ κ·Έλλ‘ μ μλ₯Ό λ»νλ μλ£νμ΄λ€. Python3μμλ μ μμ ν¬κΈ°μ μ νμ΄ μλ€.
a = 123 # μμ μ μ
a = -178 # μμ μ μ
a = 0 # 0
a = 999999999999999999 # ν° μλ μ²λ¦¬ κ°λ₯
# μ μμ λ©λͺ¨λ¦¬ μ¬μ©λ νμΈ
import sys
print(sys.getsizeof(0)) # μΌλ°μ μΌλ‘ 24 λ°μ΄νΈ
print(sys.getsizeof(999999999999)) # λ ν° μ«μλ λ λ§μ λ©λͺ¨λ¦¬ μ¬μ©
β Tip:
- Python2μμλ
int
μlong
μ΄ κ΅¬λΆλμμ§λ§, Python3μμλ ν΅ν©λ¨ - λ©λͺ¨λ¦¬κ° νμ©νλ ν 무νλμ μ μλ₯Ό μ μ₯ν μ μμ
- ν° μ μμ κ²½μ° νμμ λ°λΌ λ©λͺ¨λ¦¬κ° λμ μΌλ‘ ν λΉλ¨
μ€μν(floating-point)μ μμμ μ΄ ν¬ν¨λ μ«μλ₯Ό λ§νλ€.
a = 1.2 # μΌλ°μ μΈ μ€μ
a = -3.45 # μμ μ€μ
a = 0.0 # 0.0
# μ»΄ν¨ν°μ μ§μ νν
a = 4.24E10 # 42400000000.0
a = 4.24e-10 # 0.000000000424
# μ€μνμ μ€μ°¨
print(0.1 + 0.2 == 0.3) # False
print(round(0.1 + 0.2, 1) == 0.3) # True
# νΉμν μ€μ κ°
pos_inf = float('inf') # μμ 무νλ
neg_inf = float('-inf') # μμ 무νλ
not_a_num = float('nan') # NaN (Not a Number)
print(1 < pos_inf) # True - λͺ¨λ μ«μλ 무νλλ³΄λ€ μλ€
print(neg_inf < -999999) # True - μμ 무νλλ λͺ¨λ μλ³΄λ€ μλ€
print(not_a_num == not_a_num) # False - NaNμ μκΈ° μμ κ³Όλ κ°μ§ μλ€
# NaN νμΈ
import math
print(math.isnan(not_a_num)) # True
print(math.isinf(pos_inf)) # True
β Tip:
- μ€μνμ λΆλμμμ λ°©μμ μ¬μ©νλ―λ‘ κ³μ° μ μ€μ°¨κ° λ°μν μ μμ
- μ νν κ³μ°μ΄ νμν λλ
decimal
λͺ¨λ μ¬μ© κΆμ₯ - λΆλμμμ μ«μλ IEEE 754 νμ€μ λ°λ₯΄λ©°, νΉμκ°(inf, nan)λ νν κ°λ₯
# 2μ§μ (0bλ‘ μμ)
a = 0b1010 # 10
a = 0b1111 # 15
# 8μ§μ (0oλ‘ μμ)
a = 0o177 # 127 (1*8^2 + 7*8^1 + 7*8^0 = 127)
# 16μ§μ (0xλ‘ μμ)
a = 0x8ff # 2303
b = 0xABC # 2748 (A:10, B:11, C:12)
# μ§μ λ³ν ν¨μ
print(bin(42)) # '0b101010' (2μ§μ)
print(oct(42)) # '0o52' (8μ§μ)
print(hex(42)) # '0x2a' (16μ§μ)
# λ€μν μ§λ²μΌλ‘ μ μ μμ±
print(int('101010', 2)) # 42 (2μ§μ λ¬Έμμ΄μ 10μ§μλ‘)
print(int('52', 8)) # 42 (8μ§μ λ¬Έμμ΄μ 10μ§μλ‘)
print(int('2a', 16)) # 42 (16μ§μ λ¬Έμμ΄μ 10μ§μλ‘)
print(int('42', 10)) # 42 (10μ§μ λ¬Έμμ΄μ 10μ§μλ‘)
# μμνλ μΆλ ₯ (μ λμ΄ μμ΄)
print(format(42, 'b')) # '101010' (μ λμ΄ μλ 2μ§μ)
print(format(42, 'o')) # '52' (μ λμ΄ μλ 8μ§μ)
print(format(42, 'x')) # '2a' (μ λμ΄ μλ 16μ§μ)
print(format(42, 'X')) # '2A' (λλ¬Έμ 16μ§μ)
a = 3 + 4j # μ€μλΆ 3, νμλΆ 4μΈ λ³΅μμ
b = complex(3, 4) # μμ λμΌ
print(a.real) # 3.0 (μ€μλΆ)
print(a.imag) # 4.0 (νμλΆ)
print(abs(a)) # 5.0 (볡μμμ ν¬κΈ°)
print(a.conjugate()) # (3-4j) (μΌ€λ 볡μμ)
# 볡μμ μ°μ°
c = 1 + 2j
d = 2 + 3j
print(c + d) # (3+5j)
print(c * d) # (-4+7j)
print(c / d) # (0.46153846153846156+0.07692307692307693j)
a = 3
b = 4
print(a + b) # 7 (λ§μ
)
print(a - b) # -1 (λΊμ
)
print(a * b) # 12 (κ³±μ
)
print(a / b) # 0.75 (λλμ
)
# μ¬λ¬ μ°μ°μ ν¨κ» μ¬μ©
result = (a + b) * 3 - 2 # μ°μ°μ μ°μ μμ μ μ©
a = 3
b = 4
print(a ** b) # 81 (3μ 4μ κ³±)
print(2 ** 3) # 8 (2μ 3μ κ³±)
print(9 ** 0.5) # 3.0 (μ κ³±κ·Ό)
print(7 % 3) # 1 (λλ¨Έμ§)
print(3 % 7) # 3 (λλ¨Έμ§)
print(7 / 4) # 1.75 (μΌλ° λλμ
)
print(7 // 4) # 1 (λͺ«)
print(-7 // 4) # -2 (μμ λλμ
μ λͺ«)
a = 60 # 0b111100
b = 13 # 0b1101
# λΉνΈ AND μ°μ°
print(a & b) # 12 (0b1100)
# λΉνΈ OR μ°μ°
print(a | b) # 61 (0b111101)
# λΉνΈ XOR μ°μ°
print(a ^ b) # 49 (0b110001)
# λΉνΈ NOT μ°μ°
print(~a) # -61
# λΉνΈ μννΈ μ°μ°
print(a << 2) # 240 (0b11110000) - μΌμͺ½μΌλ‘ 2λΉνΈ μ΄λ
print(a >> 2) # 15 (0b1111) - μ€λ₯Έμͺ½μΌλ‘ 2λΉνΈ μ΄λ
# λΉνΈ μ°μ° νμ©
def is_even(num):
return num & 1 == 0 # λΉνΈ AND μ°μ°μΌλ‘ νμ§ νλ¨
print(is_even(42)) # True
print(is_even(43)) # False
λ³΅ν© μ°μ°μλ μ°μ μ°μ°μμ λμ μ°μ°μ(=)λ₯Ό ν©μ³ λμ κ²μ΄λ€.
a = 1
# κΈ°λ³Έμ μΈ μ¦κ°
a = a + 1 # 2
# λ³΅ν© μ°μ°μ μ¬μ©
a += 1 # a = a + 1 κ³Ό λμΌ
a -= 1 # a = a - 1 κ³Ό λμΌ
a *= 2 # a = a * 2 μ λμΌ
a /= 2 # a = a / 2 μ λμΌ
a //= 2 # a = a // 2 μ λμΌ
a %= 2 # a = a % 2 μ λμΌ
a **= 2 # a = a ** 2 μ λμΌ
# λΉνΈ μ°μ° λ³΅ν© μ°μ°μ
a &= b # a = a & b μ λμΌ
a |= b # a = a | b μ λμΌ
a ^= b # a = a ^ b μ λμΌ
a <<= 2 # a = a << 2 μ λμΌ
a >>= 2 # a = a >> 2 μ λμΌ
# μ λκ°
print(abs(-3)) # 3
# λ°μ¬λ¦Ό, μ¬λ¦Ό, λ΄λ¦Ό
print(round(3.14)) # 3
print(round(3.14, 1)) # 3.1
import math
print(math.ceil(3.14)) # 4 (μ¬λ¦Ό)
print(math.floor(3.14)) # 3 (λ΄λ¦Ό)
print(math.trunc(3.14)) # 3 (μμμ λ²λ¦Ό)
# μ΅λκ°, μ΅μκ°
print(max(1, 2, 3)) # 3
print(min(1, 2, 3)) # 1
# ν©κ³, κ³±
print(sum([1, 2, 3])) # 6
import math
print(math.prod([1, 2, 3, 4])) # 24 (Python 3.8 μ΄μ)
# κ±°λμ κ³±
print(pow(2, 3)) # 8 (2μ 3μ κ³±)
print(pow(2, 3, 5)) # 3 (2μ 3μ κ³±μ 5λ‘ λλ λλ¨Έμ§)
β Tip:
- μν κ΄λ ¨ κ³ κΈ ν¨μλ
math
λͺ¨λ μ¬μ© - μ λ°ν μμ§ μ°μ°μ΄ νμν λλ
decimal
λͺ¨λ μ¬μ© - 볡μ‘ν μν κ³μ°μ
numpy
λΌμ΄λΈλ¬λ¦¬ κΆμ₯
decimal
λͺ¨λμ λΆλμμμ μ€μ°¨ μμ΄ μ νν μμ§ μ°μ°μ μ 곡νλ€.
from decimal import Decimal, getcontext
# μΌλ° λΆλμμμ κ³μ° (μ€μ°¨ λ°μ)
print(0.1 + 0.2) # 0.30000000000000004
# Decimal μ¬μ© (μ νν κ³μ°)
print(Decimal('0.1') + Decimal('0.2')) # 0.3
# μ λ°λ μ€μ
getcontext().prec = 28 # 28μ리 μ λ°λ
print(Decimal(1) / Decimal(7)) # 0.1428571428571428571428571429
# λ°μ¬λ¦Ό λͺ¨λ μ€μ
getcontext().rounding = 'ROUND_HALF_UP'
print(Decimal('1.5').quantize(Decimal('1'))) # 2
# κΈμ΅ κ³μ°
price = Decimal('19.99')
tax_rate = Decimal('0.075')
tax = price * tax_rate
total = price + tax
print(f"μΈκΈ: {tax}, μ΄μ‘: {total}") # μ νν κΈμ΅ κ³μ°
fractions
λͺ¨λμ λΆμ μ°μ°μ μ§μνλ€.
from fractions import Fraction
# λΆμ μμ±
f1 = Fraction(1, 3) # 1/3
f2 = Fraction('2.5') # 5/2
f3 = Fraction('0.25') # 1/4
print(f1) # 1/3
print(f2) # 5/2
print(f3) # 1/4
# λΆμ μ°μ°
print(f1 + f2) # 17/6
print(f1 * f3) # 1/12
print(f2 / f3) # 10
# λΆμ, λΆλͺ¨ μ κ·Ό
print(f1.numerator) # 1 (λΆμ)
print(f1.denominator) # 3 (λΆλͺ¨)
# μ€μ λ³ν
print(float(f1)) # 0.3333333333333333
math
λͺ¨λμ κΈ°λ³Έμ μΈ μν ν¨μλ€μ μ 곡νλ€.
import math
# μμ
print(math.pi) # 3.141592653589793 (νμ΄)
print(math.e) # 2.718281828459045 (μμ° μμ)
print(math.inf) # 무νλ
print(math.nan) # Not a Number
# μΌκ°ν¨μ (λΌλμ λ¨μ)
print(math.sin(math.pi/2)) # 1.0
print(math.cos(math.pi)) # -1.0
print(math.tan(math.pi/4)) # 0.9999999999999999
# κ°λ λ³ν
print(math.degrees(math.pi)) # 180.0 (λΌλμ -> κ°λ)
print(math.radians(180)) # 3.141592653589793 (κ°λ -> λΌλμ)
# λ‘κ·Έ ν¨μ
print(math.log(10)) # 2.302585092994046 (μμ° λ‘κ·Έ)
print(math.log10(100)) # 2.0 (λ°μ΄ 10μΈ λ‘κ·Έ)
print(math.log2(8)) # 3.0 (λ°μ΄ 2μΈ λ‘κ·Έ)
print(math.exp(2)) # 7.38905609893065 (e^2)
# μ κ³±κ·Όκ³Ό μ§μ
print(math.sqrt(16)) # 4.0 (μ κ³±κ·Ό)
print(math.pow(2, 3)) # 8.0 (κ±°λμ κ³±)
# κΈ°ν ν¨μ
print(math.factorial(5)) # 120 (ν©ν 리μΌ)
print(math.gcd(12, 18)) # 6 (μ΅λ곡μ½μ)
print(math.isclose(0.1 + 0.2, 0.3, rel_tol=1e-9)) # False
numpy
λ μμΉ κ³μ°μ μν κ°λ ₯ν λΌμ΄λΈλ¬λ¦¬λ€. (λ³λ μ€μΉ νμ: pip install numpy
)
import numpy as np
# λ°°μ΄ μμ±
arr = np.array([1, 2, 3, 4, 5])
print(arr) # [1 2 3 4 5]
# 벑ν°ν μ°μ°
print(arr * 2) # [2 4 6 8 10]
print(arr ** 2) # [1 4 9 16 25]
print(np.sqrt(arr)) # [1. 1.41421356 1.73205081 2. 2.23606798]
# ν΅κ³ ν¨μ
print(np.mean(arr)) # 3.0 (νκ· )
print(np.median(arr)) # 3.0 (μ€μκ°)
print(np.std(arr)) # 1.4142135623730951 (νμ€νΈμ°¨)
# μ νλμ
matrix = np.array([[1, 2], [3, 4]])
print(np.linalg.det(matrix)) # -2.0 (νλ ¬μ)
print(np.linalg.inv(matrix)) # [[-2. 1. ] [ 1.5 -0.5]] (μνλ ¬)
# μΌκ°ν¨μ (벑ν°ν)
angles = np.array([0, np.pi/4, np.pi/2])
print(np.sin(angles)) # [0. 0.70710678 1. ]
# λμ μμ±
print(np.random.rand(3)) # 3κ°μ 0~1 μ¬μ΄ κ· λ±λΆν¬ λμ
print(np.random.randn(3)) # 3κ°μ νμ€μ κ·λΆν¬ λμ
print(np.random.randint(1, 10, 5)) # 1~9 μ¬μ΄ 5κ° μ μ λμ
# IEEE 754 λΆλμμμ νν μ΄ν΄νκΈ°
import struct
def float_to_bin(num):
"""float κ°μ μ΄μ§ ννμΌλ‘ λ³ν"""
# IEEE 754 νμ€μ λ°λΌ floatλ₯Ό λΉνΈ ν¨ν΄μΌλ‘ λ³ν
bits = struct.unpack('!I', struct.pack('!f', num))[0]
return bin(bits)[2:].zfill(32)
# 0.1μ μ΄μ§ νν
print(float_to_bin(0.1)) # IEEE 754 λΆλμμμ μ΄μ§ νν
print(0.1.hex()) # 0x1.999999999999ap-4 (16μ§μ νν)
# λΆλμμμ μ λ°λ νμΈ
print(sys.float_info.dig) # μΌλ°μ μΌλ‘ 15 (10μ§ μ λ°λ)
print(sys.float_info.epsilon) # 2.220446049250313e-16 (κΈ°κ³ μ‘μ€λ‘ )
# μ μ νν λ²μ
print(sys.maxsize) # μΌλ°μ μΌλ‘ 9223372036854775807 (64λΉνΈ μμ€ν
)
β μ«μνμ λ©λͺ¨λ¦¬ κ΄λ ¨ νΉμ§:
- μ μ: Pythonμ μμ μ λ°λ μ μλ₯Ό μ¬μ©νμ¬ λ©λͺ¨λ¦¬ μ ν μμ΄ ν° μ μ μ²λ¦¬ κ°λ₯
- μ€μ: IEEE 754 λ°°μ λ°λ λΆλμμμ μ¬μ© (μΌλ°μ μΌλ‘ 64λΉνΈ)
- μ€μ ννμ νκ³λ‘ μΈν΄ μ λ°ν κ³μ°μλ
decimal
λͺ¨λ μ¬μ© κΆμ₯ - 볡μμλ μ€μλΆμ νμλΆμ κ°κ° λΆλμμμ μ¬μ©