# Python Numbers

In Python, the term “Python numbers” typically refers to the numeric data types available in the language, namely integers, floating-point numbers, and complex numbers.

**Integers (int)**

In Python, integers (int) are a built-in numeric data type used to represent whole numbers without any fractional part. Integers can be positive, negative, or zero. They have unlimited precision, meaning they can represent arbitrarily large or small numbers without overflow or underflow issues.

You can create an integer in Python by simply assigning a whole number to a variable. Here are a few examples:

1 2 3 |
x = 42 y = -10 z = 0 |

In the examples above, `x`

is assigned the value 42, `y`

is assigned -10, and `z`

is assigned 0. All three variables are of the integer data type.

Python provides various operators and functions to perform arithmetic operations and manipulations with integers. Some common arithmetic operators include:

- Addition:
`+`

- Subtraction:
`-`

- Multiplication:
`*`

- Division:
`/`

- Integer division (floor division):
`//`

- Modulo (remainder):
`%`

- Exponentiation:
`**`

Here’s an example that demonstrates the use of these operators with integers:

1 2 3 4 5 6 7 8 9 10 |
a = 5 b = 2 print(a + b) # Output: 7 print(a - b) # Output: 3 print(a * b) # Output: 10 print(a / b) # Output: 2.5 print(a // b) # Output: 2 print(a % b) # Output: 1 print(a ** b) # Output: 25 |

In addition to basic arithmetic operations, Python provides built-in functions for working with integers. Some commonly used functions include:

`abs()`

: Returns the absolute value of an integer.`divmod()`

: Returns the quotient and remainder of integer division.`max()`

: Returns the largest value among given integers.`min()`

: Returns the smallest value among given integers.`pow()`

: Raises an integer to a specified power.`round()`

: Rounds a floating-point number to the nearest integer.

Here’s an example demonstrating the usage of some of these functions:

1 2 3 4 5 6 7 8 9 |
x = -7 y = 3 print(abs(x)) # Output: 7 print(divmod(x, y)) # Output: (-3, 2) print(max(x, y)) # Output: 3 print(min(x, y)) # Output: -7 print(pow(x, y)) # Output: -343 print(round(3.7)) # Output: 4 |

These are just a few examples of what you can do with integers in Python. Integers are widely used in various programming tasks, such as counting, indexing, looping, and more.

**Floating-point numbers (float)**

Floating-point numbers represent real numbers with a fractional part. They are written with a decimal point or in scientific notation. Examples of floating-point numbers include 3.14, -0.5, and 1.0.

You can create a float in Python by assigning a number with a decimal point to a variable. Here are a few examples:

1 2 3 |
x = 3.14 y = -0.5 z = 1.0 |

In the examples above, `x`

is assigned the value 3.14, `y`

is assigned -0.5, and `z`

is assigned 1.0. All three variables are of the float data type.

Python provides various operators and functions to perform arithmetic operations and manipulations with floats. These operators include the same arithmetic operators used with integers: addition (+), subtraction (-), multiplication (*), division (/), integer division (//), modulo (remainder) (%), and exponentiation (**).

Here’s an example that demonstrates the use of these operators with floats:

1 2 3 4 5 6 7 8 9 10 |
a = 2.5 b = 1.2 print(a + b) # Output: 3.7 print(a - b) # Output: 1.3 print(a * b) # Output: 3.0 print(a / b) # Output: 2.0833333333333335 print(a // b) # Output: 2.0 print(a % b) # Output: 0.1 print(a ** b) # Output: 2.8575900451433384 |

It’s important to note that floating-point numbers can introduce small rounding errors due to the nature of their representation. This can lead to unexpected results when comparing float values for equality. To mitigate this, it is often recommended to use tolerance-based comparisons when dealing with float values.

Python provides built-in functions for working with floats as well. Some commonly used functions include:

`abs()`

: Returns the absolute value of a float.`round()`

: Rounds a float to the nearest whole number or to a specified number of decimal places.`max()`

: Returns the largest value among given floats.`min()`

: Returns the smallest value among given floats.`math`

module: Provides a wide range of mathematical functions, such as trigonometric functions, logarithmic functions, etc.

Here’s an example demonstrating the usage of some of these functions:

1 2 3 4 5 6 7 |
x = -3.75 y = 2.6 print(abs(x)) # Output: 3.75 print(round(y)) # Output: 3 print(max(x, y)) # Output: 2.6 print(min(x, y)) # Output: -3.75 |

These are just a few examples of what you can do with floating-point numbers in Python. Floats are commonly used in various applications involving scientific calculations, numerical simulations, financial calculations, and more.

**Complex numbers (complex)**

Complex numbers consist of a real part and an imaginary part. They are written in the form `a + bj`

, where `a`

is the real part and `b`

is the imaginary part. For instance, `2 + 3j`

is a complex number with a real part of 2 and an imaginary part of 3.

You can create a complex number in Python by using the `j`

or `J`

suffix to indicate the imaginary part. Here are a few examples:

1 2 3 |
x = 2 + 3j y = -1j z = 4.5 + 2j |

In the examples above, `x`

is assigned the complex number with a real part of 2 and an imaginary part of 3, `y`

is assigned the complex number with a real part of 0 and an imaginary part of -1, and `z`

is assigned the complex number with a real part of 4.5 and an imaginary part of 2.

Python provides various operators and functions to perform arithmetic operations and manipulations with complex numbers. These operators include the same arithmetic operators used with integers and floats: addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (**).

Here’s an example that demonstrates the use of these operators with complex numbers:

1 2 3 4 5 6 7 8 |
a = 2 + 3j b = 1 - 2j print(a + b) # Output: (3 + 1j) print(a - b) # Output: (1 + 5j) print(a * b) # Output: (8 - 1j) print(a / b) # Output: (-0.4 + 1.6j) print(a ** 2) # Output: (-5 + 12j) |

In addition to basic arithmetic operations, Python provides built-in functions for working with complex numbers. Some commonly used functions include:

`abs()`

: Returns the magnitude (absolute value) of a complex number.`cmath.phase()`

: Returns the phase angle (argument) of a complex number.`cmath.conj()`

: Returns the complex conjugate of a complex number.`cmath.sqrt()`

: Returns the square root of a complex number.

Here’s an example demonstrating the usage of some of these functions:

1 2 3 4 5 6 |
x = 2 + 3j print(abs(x)) # Output: 3.605551275463989 print(cmath.phase(x)) # Output: 0.982793723247329 print(cmath.conj(x)) # Output: (2-3j) print(cmath.sqrt(x)) # Output: (1.6741492280355401+0.8959774761298381j) |

**Underscores in numbers**

When a number is large, it’ll become difficult to read. For example:

1 |
num= 10000000000 |

To make the long numbers more readable, you can group digits using underscores, like this:

1 |
num= 10_000_000_000 |

When using underscores in numeric literals, Python ignores the underscores and treats the number as if they were not present. This allows you to visually group digits together for improved readability without affecting the actual value of the number.

1 2 |
num= 10_000_000_000 print(num) |

**Output**

`10000000000`

**More Examples**

1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Without underscores a = 1000000 b = 31415926535 c = 0.00000001 # With underscores a_with_underscores = 1_000_000 b_with_underscores = 31_415_926_535 c_with_underscores = 0.000_000_01 print(a == a_with_underscores) # Output: True print(b == b_with_underscores) # Output: True print(c == c_with_underscores) # Output: True |

It’s important to note that underscores can only be placed between digits, not at the beginning or end of a number, and adjacent to a decimal point in a floating-point number.

1 2 3 4 |
# Invalid usage of underscores invalid_number = _100 # Error: Cannot start with an underscore invalid_number = 100_ # Error: Cannot end with an underscore invalid_number = 1._23 # Error: Cannot be placed adjacent to a decimal point |

This feature was introduced in Python 3.6 and is particularly useful when dealing with long numbers or numbers with many digits.