반응형
ython provides several built-in numeric data types, each serving different purposes depending on the precision and size of the numbers you need to work with. The primary numeric data types in Python are:
1. Integer (int)
- Description: Represents whole numbers without a fractional component. Integers in Python have unlimited precision, meaning they can grow as large as the available memory allows.
- Example Usage:
x = 42
y = -15
z = 123456789123456789123456789
- Operations: Addition (+), subtraction (-), multiplication (*), integer division (//), modulo (%), exponentiation (**), and more.
2. Floating-Point (float)
- Description: Represents numbers with a decimal point. Floats have a finite precision based on the IEEE 754 double-precision binary floating-point format, typically providing around 15–17 significant decimal digits.
- Example Usage:
a = 3.14159
b = -0.001
c = 2.0
- Operations: Same as integers, with the addition of true division (/), which always returns a float even when dividing two integers.
3. Complex Number (complex)
- Description: Represents complex numbers, which have a real part and an imaginary part. The imaginary part is denoted with a j or J.
- Example Usage:
c1 = 2 + 3j
c2 = 1 - 1j
- Operations: Addition, subtraction, multiplication, division, and other arithmetic operations are supported, respecting the rules of complex numbers.
Using Numeric Data Types
1.Basic Arithmetic:
sum = 5 + 3 # Addition
difference = 5 - 3 # Subtraction
product = 5 * 3 # Multiplication
quotient = 5 / 2 # Division (float result)
floored_quotient = 5 // 2 # Integer division
remainder = 5 % 2 # Modulo operation
power = 2 ** 3 # Exponentiation
2.Complex Numbers:
complex_sum = (2 + 3j) + (1 - 1j)
complex_product = (2 + 3j) * (1 - 1j)
3.Type Conversion:
- Convert between types using int(), float(), and complex().
x = 10 # int
y = float(x) # Convert to float
z = complex(x, y) # Convert to complex with imaginary part y
4.Mathematical Functions:
- Python's math module provides a wide range of mathematical functions such as trigonometric functions, logarithms, etc.
import math
result = math.sqrt(16) # Square root
angle = math.sin(math.pi / 2) # Sine of 90 degrees
5.Rounding and Precision:
- Use round() to round to a specified number of decimal places.
rounded_value = round(3.14159, 2) # Rounds to 3.14
6.Decimal Module:
- For precise decimal arithmetic, especially for financial calculations, Python provides the decimal module.
from decimal import Decimal
price = Decimal('19.99')
total = price * 2
Python's numeric data types provide the flexibility to work with different kinds of numbers. The choice of data type depends on the precision requirements and the nature of the calculations being performed. Python also offers various tools and modules to work with these types effectively.
반응형
'Python > Data type' 카테고리의 다른 글
Python Data type - set (0) | 2024.08.03 |
---|---|
Python Data type - dictionary (0) | 2024.08.03 |
Python Data type - tuple (0) | 2024.08.03 |
Python Data type - list (0) | 2024.07.30 |
Python data type - String (0) | 2024.07.30 |