Python Numbers with Examples and Output

Python provides three types of numeric data types: integers (int), floating-point numbers (float), and complex numbers (complex). These data types allow you to perform various mathematical operations with ease.

Types of Python Numbers

  1. Integer (int) – Whole numbers without decimal points (e.g., 5, -20, 1000)
  2. Float (float) – Numbers with decimal points (e.g., 3.14, -0.99, 100.0)
  3. Complex (complex) – Numbers with a real and an imaginary part (e.g., 2 + 3j)

1. Integer (int)

Integers represent whole numbers—both positive and negative, including zero.

Example: Integer in Python

# Integer values
x = 10
y = -25

# Performing addition
result = x + y
print("Sum of integers:", result)

Output:

Sum of integers: -15  

2. Float (float)

Float values represent real numbers with decimal points.

Example: Float in Python

# Float values
price = 49.99
discount = 10.5

# Calculating discounted price
final_price = price - discount
print("Final price:", final_price)

Output:

Final price: 39.49  

3. Complex Numbers (complex)

A complex number consists of a real and an imaginary part, written as a + bj.

Example: Complex Numbers in Python

# Complex number
z = 3 + 4j

# Accessing real and imaginary parts
print("Real part:", z.real)
print("Imaginary part:", z.imag)

Output:

Real part: 3.0  
Imaginary part: 4.0  

4. Type Conversion

You can convert one numeric type to another using Python’s built-in functions: int(), float(), and complex().

Example: Type Conversion

x = 5  # Integer
y = float(x)  # Convert to float
z = complex(x)  # Convert to complex

print("Integer:", x)
print("Float:", y)
print("Complex:", z)

Output:

Integer: 5  
Float: 5.0  
Complex: (5+0j)  

5. Mathematical Operations on Numbers

Python supports basic arithmetic operations like addition, subtraction, multiplication, and division.

Example: Arithmetic Operations

a = 20
b = 3

# Performing operations
addition = a + b
division = a / b
power = a ** b

print("Addition:", addition)
print("Division:", division)
print("Power:", power)

Output:

Addition: 23  
Division: 6.666666666666667  
Power: 8000  

6. Using the math Module

Python’s math module provides advanced mathematical functions like square root, sine, cosine, etc.

Example: Using the math Module

import math

number = 16
sqrt_value = math.sqrt(number)
print("Square root of 16:", sqrt_value)

Output:

Square root of 16: 4.0  

Python numbers offer a wide range of capabilities, from simple arithmetic operations to complex calculations. Whether you are working on financial calculations or scientific computations, mastering Python’s numeric types will greatly enhance your coding skills.

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *