Python Numbers
$count++; if($count == 1) { include "../mobilemenu.php"; } ?> if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
Python Numbers in Detail
Numbers in Python are essential data types used for performing mathematical operations, comparisons, and representing data. Python supports three primary numeric types:1. Integer (`int`)
2. Float (`float`)
3. Complex (`complex`)
1. Integer (`int`)
Integers represent whole numbers, either positive or negative, without any decimal or fractional part. Python’s integer type is unbounded, meaning it can store very large or very small numbers, limited only by available memory.# Basic integer example
num = 10
print("Integer:", num)
# Large integer
large_num = 12345678901234567890
print("Large Integer:", large_num)
# Negative integer
negative_num = -42
print("Negative Integer:", negative_num)
# Arithmetic operations with integers
sum_result = num + 5
print("Sum:", sum_result)
difference = num - 3
print("Difference:", difference)
product = num * 2
print("Product:", product)
# Power of an integer
power = num ** 3
print("Power:", power)
Output:
Integer: 10
Large Integer: 12345678901234567890
Negative Integer: -42
Sum: 15
Difference: 7
Product: 20
Power: 1000
2. Float (`float`)
Floats, or floating-point numbers, represent real numbers with decimal points. They can be used for numbers with fractional parts and support scientific notation.# Basic float example
pi = 3.14159
print("Pi:", pi)
# Float with a large number of decimal places
precise_num = 0.00012345
print("Precise Float:", precise_num)
# Negative float
negative_float = -45.67
print("Negative Float:", negative_float)
# Arithmetic operations with floats
sum_result = pi + 2.5
print("Sum:", sum_result)
difference = pi - 1.1
print("Difference:", difference)
product = pi * 2
print("Product:", product)
# Division
division_result = 7.5 / 2.5
print("Division:", division_result)
# Scientific notation
sci_notation = 1.23e4
print("Scientific Notation:", sci_notation)
Output:
Pi: 3.14159
Precise Float: 0.00012345
Negative Float: -45.67
Sum: 5.64159
Difference: 2.04159
Product: 6.28318
Division: 3.0
Scientific Notation: 12300.0
3. Complex (`complex`)
Complex numbers have both a real and an imaginary component, represented as `a + bj`, where `a` is the real part and `b` is the imaginary part. They are especially useful in scientific and engineering calculations.# Basic complex number
z = 3 + 4j
print("Complex Number:", z)
# Accessing real and imaginary parts
print("Real Part:", z.real)
print("Imaginary Part:", z.imag)
# Arithmetic with complex numbers
z2 = 1 - 2j
sum_complex = z + z2
print("Sum of Complex Numbers:", sum_complex)
product_complex = z * z2
print("Product of Complex Numbers:", product_complex)
# Complex conjugate
conjugate = z.conjugate()
print("Complex Conjugate:", conjugate)
Output:
Complex Number: (3+4j)
Real Part: 3.0
Imaginary Part: 4.0
Sum of Complex Numbers: (4+2j)
Product of Complex Numbers: (11-2j)
Complex Conjugate: (3-4j)
Type Conversions Between Numeric Types
Python allows type conversion between integers, floats, and complex numbers, which is helpful when you need to perform calculations with mixed numeric types.# Integer to float
int_num = 10
float_num = float(int_num)
print("Integer to Float:", float_num)
# Float to integer
float_num = 9.8
int_num = int(float_num)
print("Float to Integer:", int_num)
# Integer to complex
complex_num = complex(int_num)
print("Integer to Complex:", complex_num)
# Float to complex
complex_num = complex(float_num)
print("Float to Complex:", complex_num)
Output:
Integer to Float: 10.0
Float to Integer: 9
Integer to Complex: (9+0j)
Float to Complex: (9.8+0j)
Mathematical Functions with Numbers
Python provides a `math` module for various mathematical operations, such as trigonometric functions, logarithms, and constants.import math
# Square root
sqrt_result = math.sqrt(16)
print("Square Root of 16:", sqrt_result)
# Power
power_result = math.pow(2, 3)
print("2 raised to the power of 3:", power_result)
# Trigonometric functions
cosine = math.cos(math.pi) # Cosine of pi radians
print("Cosine of pi:", cosine)
# Logarithm
log_result = math.log(10)
print("Natural Log of 10:", log_result)
# Constants
print("Value of pi:", math.pi)
print("Value of e:", math.e)
Output:
Square Root of 16: 4.0
2 raised to the power of 3: 8.0
Cosine of pi: -1.0
Natural Log of 10: 2.302585092994046
Value of pi: 3.141592653589793
Value of e: 2.718281828459045
Summary
Python’s numeric types allow a wide range of mathematical computations, from basic arithmetic to complex scientific calculations. Understanding how to work with integers, floats, and complex numbers provides a strong foundation for Python programming.