Back to Study Material Python

Numbers

Python integers have arbitrary precision — they can be as large as your available memory allows. There is no overflow as in C or Java. You can write inte...

Numbers

Integer Type (int)

Python integers have arbitrary precision — they can be as large as your available memory allows. There is no overflow as in C or Java. You can write integer literals in decimal (42), binary (0b101010), octal (0o52), or hexadecimal (0x2A) — all of these represent the same value. Underscores can be used as visual separators in large literals: 1_000_000 is the same as 1000000 and is easier to read.

Float Type (float)

Python floats are IEEE 754 double-precision numbers, which gives about 15-17 significant decimal digits. Be aware that some decimal values cannot be represented exactly in binary floating point: 0.1 + 0.2 does not equal 0.3 in Python — it gives 0.30000000000000004. For financial calculations requiring exact decimal arithmetic, use the decimal module (from decimal import Decimal). For scientific work, floats are usually precise enough.

Complex Type (complex)

Python has native complex number support. Write a complex literal as a + bj where a is the real part and b is the imaginary part: c = 3 + 4j. Access the parts with c.real (gives 3.0) and c.imag (gives 4.0). Get the conjugate with c.conjugate(). The built-in abs() function returns the magnitude of a complex number. Complex numbers support all arithmetic operators. This built-in support is useful in signal processing and certain mathematical algorithms.

Useful Numeric Functions

Python's built-in math-related functions include: abs(x) returns absolute value, round(x, n) rounds to n decimal places, pow(x, y) raises x to the power y (equivalent to x**y), divmod(x, y) returns a tuple (quotient, remainder). The math module adds functions like math.sqrt(), math.floor(), math.ceil(), math.factorial(), math.log(), and constants math.pi and math.e. Import with import math before using these.

Worked Example

Calculate the hypotenuse of a right triangle with legs 3 and 4. Using built-in pow: result = pow(3, 2) + pow(4, 2); hyp = result ** 0.5, giving 5.0. Using the math module: import math; hyp = math.hypot(3, 4), which also gives 5.0. math.hypot is more accurate for very large or small values because it avoids intermediate overflow and underflow. Always prefer specialised library functions when they exist.

Exam Tip

Key numeric facts for exams: round() uses banker's rounding (round half to even) in Python 3, so round(0.5) is 0, not 1, and round(2.5) is 2. This surprises many students. Also know that int() truncates toward zero (int(-2.9) is -2, not -3), while math.floor(-2.9) is -3. The difference between truncation and floor matters for negative numbers. These edge cases are frequently tested in certification and interview questions.