In Python, there are three types of numeric data types: integers, floating-point numbers, and complex numbers. Let’s take a look at each of these data types with examples:
- Integers – Integers are whole numbers, positive or negative, without a fractional component. They can be assigned to a variable directly without any special syntax.
a = 5
b = -10
c = 0
- Floating-point numbers – Floating-point numbers, or floats, are numbers with a fractional component. They can be created using a decimal point or scientific notation.
a = 3.14
b = 1.23e-4
c = -9.0
- Complex numbers – Complex numbers are numbers with a real and imaginary part. They can be created by adding a “j” or “J” after the imaginary part.
a = 2 + 3j
b = -1j
c = 4.5 - 6j
Python also provides built-in functions for working with numeric data types. Some of the commonly used ones are:
abs()
– Returns the absolute value of a numberint()
– Converts a string or float to an integerfloat()
– Converts a string or integer to a floatcomplex()
– Creates a complex number from a real and imaginary part
a = abs(-10)
b = int('15')
c = float(3)
d = complex(2, 3)
In addition to these built-in functions, Python also provides several libraries for more advanced mathematical operations, such as NumPy and SciPy.