Numeric in Python

Overview

Python provides a robust set of numeric types and operations that cater to a wide range of computational needs. The primary numeric types in Python include:

  • Integers: These can be arbitrarily large, allowing for computations that exceed the typical limits found in other programming languages.
  • Floats: These have precision similar to the double types in C, making them suitable for most scientific and engineering calculations.
  • Complex Numbers: These involve the imaginary unit j and are used in fields that require complex number computations.
  • Booleans: These are a subclass of integers and can take the values True or False.

Python also provides a variety of built-in numeric functions and more advanced functions available in modules like math for real numbers and cmath for complex numbers.

Numeric Operations

Python supports a variety of arithmetic operations, which can be demonstrated with the following examples:

5 + 2 - 3 * 2
# [Result: 1](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/106)

5 / 2         # Floating-point result with normal division
# [Result: 2.5](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/106)

5 // 2        # Integer result with truncation when divided using '//'
# [Result: 2](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/106)

30000000000   # This would be too large to be an int in many languages
# [Result: 30000000000](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/106)

30000000000 * 3
# [Result: 90000000000](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/106)

30000000000 * 3.0
# [Result: 90000000000.0](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/106)

2.0e-8        # Scientific notation gives back a float
# [Result: 2e-08](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/106)

3000000 * 3000000 
# [Result: 9000000000000](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/106)

int(200.2)    # Convert float to int.
# [Result: 200](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/106)

int(2e2)      # Convert exponent to int.
# [Result: 200](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/106)

float(200)    # Convert an int to a float.
# [Result: 200.0](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/106)

Numeric Escape Sequences

Numeric escape sequences in Python allow you to represent ASCII characters in strings using octal or hexadecimal values. These sequences are particularly useful for characters that do not have a standard printable representation. Examples include:

  • '\012' for newline in octal
  • '\x0A' for newline in hexadecimal

Example Listing

Below is an example of a simple Python script located in a file named __init__.py within a directory structure mathproj/comp/numeric/. This script prints a message indicating its execution:

Listing 18.4 File mathproj/comp/numeric/init.py

print("Hello from numeric init")

This script can be part of a larger package or module, serving as an initialization script that runs when the package is imported.

FAQ (Frequently asked questions)

How large can integers be in Python?

What is the precision of floats in Python?

What is the imaginary unit used in Python complex numbers?

What are the Boolean values in Python?

What is the result of 5 + 2 - 3 * 2 in Python?

What is the result of multiplying a large integer by a float in Python?

What is an example of a numeric escape sequence for newline in octal?

What is an example of a numeric escape sequence for newline in hexadecimal?

What does the numeric init file in the mathproj package do?