In Python, numbers are a fundamental data type used to represent numeric values.
Python provides several built-in numeric types, including integers, floating-point numbers, and complex numbers.
Mainly they are
int
float
complex
Integers (int) :-
Integers are whole numbers, positive or negative, (...,-1, 0, 1,...)
without any decimal point.
In Python, integers have unlimited precision, meaning they can represent arbitrarily large numbers (limited only by available memory)
x = 42 y = -10 z = 0 print(x, type(x)) print(y, type(y)) print(z, type(z))
Floating-point numbers (float) :-
Floating-point numbers, or floats
, are numbers with a decimal point ( 1.0, 1.1, 0.2 )
.
They are used to represent real numbers and can approximate a wide range of values, including fractions and very large or very small numbers.
a = 3.14 b = -0.5 c = 2.71828 print(a) print(b) print(c)
Complex numbers (complex) :-
Complex numbers consist of a real part and an imaginary part, both represented as floating-point numbers.
They are written in the form a + bi
, where a
is the real part, b
is the imaginary part, and i
is the imaginary unit (the square root of -1
)
z1 = 2 + 3j z2 = -1j print(z1) print(z2)
Note - when working with integers, keep in mind that division (/)
between two integers results in a floating-point number, while the floor division (//)
operator returns an integer result by discarding any fractional part
# Division (/) between two integers results in a floating-point number result_float = 10 / 3 print(result_float) # Output: 3.3333333333333335 # Floor division (//) returns an integer result by discarding any fractional part result_floor = 10 // 3 print(result_floor) # Output: 3 # The result is rounded towards negative infinity result_floor_negative = -10 // 3 print(result_floor_negative) # Output: -4 # Remainder can be obtained using the modulus operator (%) remainder = 10 % 3 print(remainder) # Output: 1