Python Numbers

Numbers are a fundamental part of programming, and Python provides robust support for different types of numerical data. In this blog post, we'll explore Python's number types, including integers, floats, and complex numbers. We'll also cover various operations and functions you can perform with numbers in Python, along with practical examples.


Types of Numbers in Python

Python supports three main types of numerical data:

  1. Integers (int)
  2. Floating-Point Numbers (float)
  3. Complex Numbers (complex)

1. Integers (int)

Integers are whole numbers without a decimal point. They can be positive, negative, or zero.

Example:

a = 10

b = -5

c = 0

print(a, b, c)  # Output: 10 -5 0

 

 

2. Floating-Point Numbers (float)

Floats are numbers that contain a decimal point. They are used to represent real numbers.

Example:

x = 3.14

y = -2.5

z = 0.0

print(x, y, z)  # Output: 3.14 -2.5 0.0

 

 

3. Complex Numbers (complex)

Complex numbers are numbers with a real and an imaginary part. They are represented as a + bj, where a is the real part and b is the imaginary part.

Example:

c1 = 2 + 3j

c2 = -1 + 4j

print(c1, c2)  # Output: (2+3j) (-1+4j)

 

 

Numerical Operations

Python supports various operations on numbers, including arithmetic operations, comparisons, and more.

Arithmetic Operations

Example:

# Addition

result = 10 + 5

print(result)  # Output: 15

 

# Subtraction

result = 10 - 5

print(result)  # Output: 5

 

# Multiplication

result = 10 * 5

print(result)  # Output: 50

 

# Division

result = 10 / 5

print(result)  # Output: 2.0

 

# Floor Division

result = 10 // 3

print(result)  # Output: 3

 

# Modulus

result = 10 % 3

print(result)  # Output: 1

 

# Exponentiation

result = 10 ** 2

print(result)  # Output: 100

 

 

Comparison Operations

Example:

a = 10

b = 5

 

print(a == b)  # Output: False

print(a != b)  # Output: True

print(a > b)   # Output: True

print(a < b)   # Output: False

print(a >= b)  # Output: True

print(a <= b)  # Output: False

 

 

Mathematical Functions

Python's math module provides many useful mathematical functions.

Example:

import math

 

# Square root

result = math.sqrt(16)

print(result)  # Output: 4.0

 

# Power

result = math.pow(2, 3)

print(result)  # Output: 8.0

 

# Logarithm

result = math.log(100)

print(result)  # Output: 4.605170185988092

 

# Sine

result = math.sin(math.pi / 2)

print(result)  # Output: 1.0

 

# Cosine

result = math.cos(0)

print(result)  # Output: 1.0

 

# Factorial

result = math.factorial(5)

print(result)  # Output: 120

 

 

Type Conversion

You can convert between different numerical types using built-in functions.

Example:

# Convert int to float

a = 10

b = float(a)

print(b)  # Output: 10.0

 

# Convert float to int

x = 3.14

y = int(x)

print(y)  # Output: 3

 

# Convert int to complex

c = complex(a)

print(c)  # Output: (10+0j)

 

 

Working with Complex Numbers

Python provides functions to work with the real and imaginary parts of complex numbers.

Example:

c = 3 + 4j

 

# Real part

print(c.real)  # Output: 3.0

 

# Imaginary part

print(c.imag)  # Output: 4.0

 

# Conjugate

print(c.conjugate())  # Output: (3-4j)

 

 

Final Remarks

Understanding and working with numbers is essential for any Python programmer. Whether you're performing simple arithmetic operations or complex mathematical calculations, Python provides robust support for numerical data types. Experiment with the examples provided to deepen your understanding and enhance your programming skills.

 

No comments:

Post a Comment

MS Excel Logical Functions

Logical functions in Excel are powerful tools that help you make decisions based on conditions. Whether you're comparing values or testi...

Post Count

Loading...