Here’s a detailed explanation of Python Numbers for Python programming:


Python Numbers

What are Numbers in Python?

Numbers in Python are used to store numeric values. Python supports several types of numbers, including integers, floating-point numbers, and complex numbers. Each type serves a specific purpose depending on the kind of data being handled.


Types of Numbers in Python

  1. int (Integer)
    • Stores whole numbers (positive, negative, or zero) without any decimal point.
    • Example: x = 10 y = -5 z = 0 print(type(x)) # Output: <class 'int'>
  2. float (Floating-point)
    • Stores real numbers with a decimal point or in exponential (scientific) notation.
    • Example: x = 3.14 y = -0.99 z = 1.5e3 # Scientific notation (1.5 * 10^3 = 1500) print(type(x)) # Output: <class 'float'>
  3. complex (Complex Numbers)
    • Represents numbers in the form a + bj, where a is the real part and b is the imaginary part.
    • Example: num = 3 + 4j print(num.real) # Output: 3.0 print(num.imag) # Output: 4.0 print(type(num)) # Output: <class 'complex'>

Numeric Operations

Python supports a variety of arithmetic operations with numbers:

OperatorDescriptionExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division5 / 22.5
//Floor Division5 // 22
%Modulus (Remainder)5 % 21
**Exponentiation5 ** 3125

Examples:

# Basic Arithmetic
a = 10
b = 3

print(a + b)   # Output: 13
print(a - b)   # Output: 7
print(a * b)   # Output: 30
print(a / b)   # Output: 3.3333...
print(a // b)  # Output: 3 (Floor Division)
print(a % b)   # Output: 1 (Remainder)
print(a ** b)  # Output: 1000 (10 raised to the power of 3)

Type Conversion

Python provides several built-in functions to convert between numeric types:

FunctionDescriptionExample
int(x)Converts x to an integerint(3.5)3
float(x)Converts x to a floating pointfloat(5)5.0
complex(x)Converts x to a complex numbercomplex(3)3+0j

Examples:

x = 5.7
y = int(x)  # Converts float to int
print(y)    # Output: 5

z = float(y)  # Converts int back to float
print(z)    # Output: 5.0

c = complex(x)  # Converts to complex number
print(c)    # Output: (5.7+0j)

Mathematical Functions

Python includes a standard library called math that provides many mathematical operations:

Examples:

import math

# Square root
print(math.sqrt(16))   # Output: 4.0

# Power
print(math.pow(2, 3))  # Output: 8.0

# Rounding
print(round(3.14159, 2))  # Output: 3.14

# Absolute value
print(abs(-7))  # Output: 7

# Trigonometric functions
print(math.sin(math.pi / 2))  # Output: 1.0

Special Numeric Values

Python also supports special numeric constants like infinity and NaN (Not a Number).

Examples:

# Infinity
pos_inf = float('inf')
neg_inf = float('-inf')
print(pos_inf, neg_inf)  # Output: inf -inf

# NaN (Not a Number)
nan = float('nan')
print(nan)  # Output: nan

Practical Example

Here’s an example that combines different numeric types and operations:

# Calculate the area of a circle
import math

radius = 5
area = math.pi * (radius ** 2)
print(f"Area of the circle: {area}")  # Output: Area of the circle: 78.53981633974483

# Perform floor division and modulus
x = 15
y = 4
quotient = x // y
remainder = x % y
print(f"Quotient: {quotient}, Remainder: {remainder}")

Important Notes

  1. Dynamic Typing: You can reassign a variable to a different type: x = 10 # x is an int x = 10.5 # x is now a float x = 3 + 4j # x is now a complex number
  2. Immutable Numbers: Numbers in Python are immutable, meaning their value cannot change after being created. When you perform an operation, a new object is created.
  3. Precision of Floating-Point Numbers: Floats may not always provide exact results due to how they’re stored in memory: print(0.1 + 0.2) # Output: 0.30000000000000004 Use the decimal module for higher precision if needed: from decimal import Decimal print(Decimal('0.1') + Decimal('0.2')) # Output: 0.3

This explanation provides a clear overview of Python Numbers.

Here’s a detailed explanation of Python Numbers for Python programming:


Python Numbers

What are Numbers in Python?

Numbers in Python are used to store numeric values. Python supports several types of numbers, including integers, floating-point numbers, and complex numbers. Each type serves a specific purpose depending on the kind of data being handled.


Types of Numbers in Python

  1. int (Integer)
    • Stores whole numbers (positive, negative, or zero) without any decimal point.
    • Example: x = 10 y = -5 z = 0 print(type(x)) # Output: <class 'int'>
  2. float (Floating-point)
    • Stores real numbers with a decimal point or in exponential (scientific) notation.
    • Example: x = 3.14 y = -0.99 z = 1.5e3 # Scientific notation (1.5 * 10^3 = 1500) print(type(x)) # Output: <class 'float'>
  3. complex (Complex Numbers)
    • Represents numbers in the form a + bj, where a is the real part and b is the imaginary part.
    • Example: num = 3 + 4j print(num.real) # Output: 3.0 print(num.imag) # Output: 4.0 print(type(num)) # Output: <class 'complex'>

Numeric Operations

Python supports a variety of arithmetic operations with numbers:

OperatorDescriptionExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division5 / 22.5
//Floor Division5 // 22
%Modulus (Remainder)5 % 21
**Exponentiation5 ** 3125

Examples:

# Basic Arithmetic
a = 10
b = 3

print(a + b)   # Output: 13
print(a - b)   # Output: 7
print(a * b)   # Output: 30
print(a / b)   # Output: 3.3333...
print(a // b)  # Output: 3 (Floor Division)
print(a % b)   # Output: 1 (Remainder)
print(a ** b)  # Output: 1000 (10 raised to the power of 3)

Type Conversion

Python provides several built-in functions to convert between numeric types:

FunctionDescriptionExample
int(x)Converts x to an integerint(3.5)3
float(x)Converts x to a floating pointfloat(5)5.0
complex(x)Converts x to a complex numbercomplex(3)3+0j

Examples:

x = 5.7
y = int(x)  # Converts float to int
print(y)    # Output: 5

z = float(y)  # Converts int back to float
print(z)    # Output: 5.0

c = complex(x)  # Converts to complex number
print(c)    # Output: (5.7+0j)

Mathematical Functions

Python includes a standard library called math that provides many mathematical operations:

Examples:

import math

# Square root
print(math.sqrt(16))   # Output: 4.0

# Power
print(math.pow(2, 3))  # Output: 8.0

# Rounding
print(round(3.14159, 2))  # Output: 3.14

# Absolute value
print(abs(-7))  # Output: 7

# Trigonometric functions
print(math.sin(math.pi / 2))  # Output: 1.0

Special Numeric Values

Python also supports special numeric constants like infinity and NaN (Not a Number).

Examples:

# Infinity
pos_inf = float('inf')
neg_inf = float('-inf')
print(pos_inf, neg_inf)  # Output: inf -inf

# NaN (Not a Number)
nan = float('nan')
print(nan)  # Output: nan

Practical Example

Here’s an example that combines different numeric types and operations:

# Calculate the area of a circle
import math

radius = 5
area = math.pi * (radius ** 2)
print(f"Area of the circle: {area}")  # Output: Area of the circle: 78.53981633974483

# Perform floor division and modulus
x = 15
y = 4
quotient = x // y
remainder = x % y
print(f"Quotient: {quotient}, Remainder: {remainder}")

Important Notes

  1. Dynamic Typing: You can reassign a variable to a different type: x = 10 # x is an int x = 10.5 # x is now a float x = 3 + 4j # x is now a complex number
  2. Immutable Numbers: Numbers in Python are immutable, meaning their value cannot change after being created. When you perform an operation, a new object is created.
  3. Precision of Floating-Point Numbers: Floats may not always provide exact results due to how they’re stored in memory: print(0.1 + 0.2) # Output: 0.30000000000000004 Use the decimal module for higher precision if needed: from decimal import Decimal print(Decimal('0.1') + Decimal('0.2')) # Output: 0.3

This explanation provides a clear overview of Python Numbers.