A Brief Look at Python Built‑in Numeric Types (Including a Python 2 vs 3 Difference)
This article explains Python's built‑in object model, focusing on simple numeric types—int, long, float, complex, bool—and their literal forms, division behavior differences between Python 2 and Python 3, as well as fractions, boolean values, and complex number operations.
Python Built‑in Object Overview
Python treats every value as an object. Built‑in objects are divided into simple types (primarily numeric data) and container types (sequences, tuples, mappings). The special object None represents the empty value.
Simple type objects include int, long, float, complex, and bool. Container types include sequences such as string, list, tuple, and the mapping type dict. All objects expose attributes and methods accessed via the dot operator ( .).
Numeric Constants
Python numeric literals support several formats:
Decimal integers (e.g., 42)
Binary ( 0b1010), octal ( 0o52), and hexadecimal ( 0x2A) prefixes
Floating‑point numbers, including scientific notation ( 1.2e-3 or 1.2E-3)
Complex literals using a trailing j or J (e.g., 3+4j)
Fractional literals via the fractions.Fraction class (see the next section)
Division in Python 2 vs Python 3
In Python 2, the / operator performs integer division when both operands are integers, truncating the result (e.g., 5/2 == 2). When either operand is a float, the operation yields a float. Python 3 changes / to always perform true division, returning a float (e.g., 5/2 == 2.5). The floor‑division operator // returns the largest integer less than or equal to the true quotient, and % returns the remainder.
Fractions
The fractions.Fraction class represents rational numbers as a numerator and denominator. To use it, import the module and instantiate with two integers, e.g., Fraction(3, 4). Strings representing floating‑point numbers can also be converted to fractions.
Boolean Type
Boolean objects have two values: True and False. Empty data structures evaluate to False. Booleans are commonly used in conditional statements to control program flow.
Complex Numbers
A complex number consists of a real part and an imaginary part, the latter indicated by a trailing j or J. Complex numbers support the usual arithmetic operators, and the result of an operation between complex numbers remains a complex number.
Takeaway
Numeric types are fundamental to any programming language. Mastering Python's numeric literals, division semantics across versions, fractions, booleans, and complex numbers is essential, especially since differences between Python 2 and Python 3 frequently appear in interview questions.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
