Python Numbers

1. Integers (int):

Integers represent whole numbers without a fractional or decimal part.


x = 42
y = -7

2. Floating-Point Numbers (float):

Floating-point numbers represent real numbers with a decimal or fractional part. They use a decimal point to differentiate from integers.


a = 3.14
b = -0.5

3. Complex Numbers (complex):

Complex numbers consist of a real part and an imaginary part, denoted by "j" or "J."


z = 2 + 3j
w = -1 - 0.5j

4. Number Conversions:

You can convert between number types using constructors, such as int(), float(), and complex().


x = int(3.14)     # Convert to integer
y = float(42)     # Convert to float
z = complex(2)    # Convert to complex

5. Basic Number Operations:

Python supports standard arithmetic operations, including addition, subtraction, multiplication, division, and modulus.


a = 10
b = 3
addition = a + b
subtraction = a - b
multiplication = a * b
division = a / b
modulus = a % b

6. Exponentiation:

You can raise a number to a power using the ** operator.


squared = 5 ** 2
cubed = 2 ** 3

7. Math Functions:

Python's math module provides a wide range of mathematical functions for more advanced numerical calculations.


import math
sqrt_val = math.sqrt(16)

8. Number Comparisons:

You can compare numbers using comparison operators, such as ==, <, >, <=, and >=.


is_equal = 5 == 5
is_less_than = 3 < 5
is_greater_than = 7 > 9

9. Number Type Checking:

You can check the type of a variable using the isinstance() function.


num = 42
is_int = isinstance(num, int)

10. Type Conversion:

You can convert numbers to strings using the str() function.


num = 123
num_str = str(num)

11. Infinite and NaN:

Python provides special constants for positive and negative infinity, as well as for "Not-a-Number" (NaN).


pos_inf = float('inf')
neg_inf = float('-inf')
not_a_number = float('nan')

Python's support for various numeric types and operations makes it a versatile language for mathematical and scientific computing. You can perform a wide range of calculations, including basic arithmetic, advanced mathematical functions, and complex number operations using Python's built-in capabilities and libraries.