Python Numeric Data Types: int, float, and complex

Python provides three built-in numeric types for handling different kinds of numerical values: itnegers, floating-point numbers, and complex numbers.

Integer (int)

Integer values represent whole numbers without any decimal component. They can be positive or negative and have arbitrary precision, meaning there's no practical limit to their size.

a = 42
b = -9876543210
c = 123456789012345678901234567890

print(a, b, c)
print(type(a))

Output:

42 -9876543210 123456789012345678901234567890
<class 'int'>

Floating-Point (float)

Floating-point numbers represent real numbers that include a decimal fraction. They can also be expressed using scientific notation with the letter "e" to denote powers of 10.

a = 3.14159
b = -0.5
c = 2.5e-3
d = 1.2E10

e = 7e4

print(a, b, c, d)
print(e, type(e))

Output:

3.14159 -0.5 0.0025 12000000000.0
70000.0 <class 'float'>

Complex Numbers (complex)

Complex numbers have a real part and an imaginary part, where the imaginary unit is represented by "j" in Python (rather than "i" as used in mathematics).

a = 4 + 5j
b = -3j
c = 2j

print(a, b, c)
print(type(a))

Output:

(4+5j) (-0-3j) 2j
<class 'complex'>

Type Verification

Use the type() function to determine the data type of any variable:

number1 = 100
number2 = 7.5
number3 = 3 + 4j

print(type(number1))
print(type(number2))
print(type(number3))

Output:

<class 'int'>
<class 'float'>
<class 'complex'>

Type Conversion

Python allows explicit conversion between numeric types using constructor functions:

original_int = 25
original_float = 12.7

# Convert integer to floating-point
converted_float = float(original_int)

# Convert floating-point to integer (truncates decimal portion)
converted_int = int(original_float)


# Convert integer to complex
converted_complex = complex(original_int)

print(converted_float, type(converted_float))
print(converted_int, type(converted_int))
print(converted_complex, type(converted_complex))

Output:

25.0 <class 'int'>
12 <class 'int'>
(25+0j) <class 'complex'>

Note: Complex numbers cannot be converted to int or float types.

Arithmetic Operators

Division Operator (/)

The division operator always returns a float result, regardless of whether the operands are evenly divisible.

result1 = 20 / 4
result2 = 15 / 4

print(result1, result2)

Output:

5.0 3.75

Floor Division Operator (//)

Floor divisoin returns the integer portion by discarding the fractional part, effectively rounding down.

result1 = 20 // 4
result2 = 15 // 4

print(result1, result2)

Output:

5 3

Modulo Operator (%)

The modulo operator returns the remainder after division.

result1 = 17 % 5
result2 = 20 % 4

print(result1, result2)

Output:

2 0

Exponentiation Operator (**)

The exponentiation operator raises a number to a specified power.

result1 = 5 ** 3
result2 = 2 ** 10

print(result1, result2)

Output:

125 1024

The Underscore Variable

In Python's interactive interpreter, the special underscore _ variable automatically stores the result of the last evaluated expression. This allows convenient reuse of previous values without explicitly storing them.

>>> 15 + 27
42
>>> _ + 8
50
>>> _ * 2
100

This feature is particularly useful during interactive debugging and exploratory programming.

Tags: python numeric types int float Complex

Posted on Fri, 06 Nov 2026 16:20:50 +0000 by noppie