Fundamentals 7 min read

Python Numeric Types Explained: Integers, Floats, Fractions & Py2 vs Py3 Division Differences

This tutorial covers Python's built-in numeric types including integers, floats, complex numbers, booleans, and fractions, with detailed examples of division behavior differences between Python 2 and 3, floor division, modulo operations, and the fractions module for precise rational arithmetic.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Python Numeric Types Explained: Integers, Floats, Fractions & Py2 vs Py3 Division Differences

Python is an object-oriented language where everything is an object. Built-in objects are classified into simple types (numeric data) and container types (sequences, tuples, mappings like dict). This article focuses on simple numeric types.

Numeric Constants

Python supports multiple numeric constant formats:

Decimal integers and floats

Scientific notation (e.g., 1.2e-3 or 1.2E-3 for 1.2×10⁻³)

Binary ( 0b prefix), octal ( 0 prefix), hexadecimal ( 0x prefix)

Complex numbers and fractions

Division Differences: Python 2 vs Python 3

The article illustrates a key difference using interactive session screenshots:

Python 2: Integer division truncates toward zero (floor for positive). Example: 3/2 yields 1. If either operand is a float, Python coerces the other to float, so 3/2.0 yields 1.5.

Python 3: Division always returns a float. 3/2 yields 1.5. Both operands are implicitly converted to float before division.

Floor Division and Modulo

The // operator performs floor division (returns the largest integer ≤ quotient). The % operator returns the remainder. Examples shown: 5//2

2
5%2

1 Works consistently across Python 2 and 3.

Fractions with the fractions Module

Import Fraction from fractions to create exact rational numbers: Fraction(3, 4) represents 3/4. Fractions support arithmetic operations and can be constructed from floats or strings (e.g., Fraction('0.75')). The article demonstrates fraction creation and arithmetic via screenshots.

Boolean Type

Booleans have two values: True and False. Empty containers, zero, and None evaluate to False in boolean contexts. Booleans are primarily used as conditions in branching and loops.

Complex Numbers

Complex numbers consist of a real part (optional) and an imaginary part (required, suffixed with j or J). Example: 3+4j. They support standard arithmetic; results remain complex. Screenshots show addition, subtraction, multiplication, and division of complex numbers.

The article concludes by emphasizing that numeric types are fundamental in any language, and highlights the Python 2 vs 3 division difference as a common interview topic.

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.

Pythonfundamentalsnumeric typescomplex numbersdivisionfractionsPython 2 vs 3booleans
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.