Fundamentals 6 min read

Master Python’s Built‑in Numeric Types: From Integers to Complex Numbers

This article explains Python’s built‑in numeric objects—including integers, longs, floats, complex numbers, booleans, and fractions—covers constant formats, version‑specific division behavior, and demonstrates how to use the Fraction class for precise rational arithmetic.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Python’s Built‑in Numeric Types: From Integers to Complex Numbers

Python is an object‑oriented language where everything can be treated as an object. Built‑in objects are divided into simple types (numeric data) and container types (sequences, tuples, mappings, etc.).

Simple numeric types include int, long, float, complex, and bool. Container types include strings, lists, tuples, and dictionaries, while None represents the null object.

All objects have data attributes and built‑in methods, which are invoked using the dot operator.

This article focuses on Python’s simple numeric objects, illustrating their usage and differences between Python 2 and Python 3.

1. Constants

Python numeric constants support decimal, binary (0b), octal (0o), and hexadecimal (0x) formats, as well as scientific notation (e.g., 1.2e‑3). Complex numbers and fractions are also allowed, though fraction handling requires attention.

Fraction operations in Python 2:

Fraction operations in Python 3:

In Python 2, integer division truncates toward zero, while Python 3 performs true division, converting operands to float when necessary. The floor division operator // returns the largest integer less than or equal to the quotient, and the modulo operator % yields the remainder.

2. Fractions

The Fraction(x, y) function from the fractions module creates a rational number representing x/y. After importing the module, fractions can be used directly in expressions, or floating‑point strings can be converted to fractions.

3. Boolean

Boolean objects have two values: True and False. Empty data types evaluate to False. Booleans are commonly used in conditional statements and loop tests.

4. Complex Numbers

Complex numbers consist of a real part and an imaginary part, the latter indicated by the suffix j or J. They support the usual arithmetic operations, and results of operations between complex numbers remain complex.

Numeric types are fundamental to any programming language. Mastering them, especially the differences in fraction handling between Python 2 and Python 3, is essential for interviews and real‑world development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonbooleanConstantsnumeric typescomplex numbersfractions
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.