Python Numbers – Tutorial with Examples

In Python, numbers are used to perform mathematical operations and to store values. There are three types of numbers in Python: integers, floating-point numbers, and complex numbers. In this article, we will go over the different types of numbers in Python and their properties and methods.

Integers

Integers are whole numbers that can be positive, negative, or zero. They have no decimal part and are represented in Python by the int type. Integers can be represented in various bases, including binary, octal, and hexadecimal. The following are some examples of integers in Python:

x = 42 # decimal representation
y = 0b101010 # binary representation
z = 0o52 # octal representation
a = 0x2A # hexadecimal representation

Integers in Python are objects of the int type, and they have a number of methods available, including:

print(x.bit_length()) # returns the number of bits necessary to represent the integer in binary
print(x.to_bytes(4, 'big')) # returns the integer as a 4-byte string in big-endian order
print(x.to_bytes(4, 'little')) # returns the integer as a 4-byte string in little-endian order

In Python 3, integers have no limit to their size, as they can grow to the size of the available memory. This is different from Python 2, where integers are limited to a certain number of bits and can overflow.

Floating-Point Numbers

Floating-point numbers, also known as floats, are numbers with a decimal part. They are represented in Python by the float type. The following are some examples of floating-point numbers in Python:

x = 3.14
y = 1.0e3
z = -0.5

Floats have a limited precision in Python, which means that not all decimal values can be exactly represented. This can lead to inaccuracies in some calculations and the loss of precision. The following example demonstrates the limitations of floating-point precision:

x = 0.1 + 0.1 + 0.1
print(x) # Output: 0.30000000000000004

It’s important to keep this limitation in mind when using floating-point numbers in Python, especially when working with financial or scientific applications where accuracy is crucial.

Complex Numbers

Complex numbers are numbers with both a real and imaginary part. They are represented in Python by the complex type. The imaginary part is represented by the letter ‘j’ in Python. The following are some examples of complex numbers in Python:

x = 1 + 2j
y = 3 - 4j
z = -2j

Complex numbers can be used to represent quantities in the plane and to perform operations with them. They have a number of methods available, including:

print(x.real) # returns the real part of the complex number
print(x.imag) # returns the imaginary part of the complex number
print(x.conjugate()) # returns the conjugate of the complex number
print(abs(x)) # returns the magnitude of the complex number

Complex numbers are often used in physics, engineering, and mathematics, where they can represent quantities with both magnitude and direction.

Number Conversion

In Python, it’s possible to convert between different number types. For example, you can convert an integer to a float or a float to an integer. The following are some examples of number conversion in Python:

x = 42
y = float(x) # convert int to float
z = int(y) # convert float to int
a = complex(x) # convert int to complex
b = int(a) # convert complex to int (raises TypeError)

It’s important to keep in mind that converting from a float to an integer will truncate the decimal part and not round it. Additionally, converting from a complex number to an integer is not possible and will raise a TypeError.

Arithmetic Operations

In Python, you can perform arithmetic operations with numbers. The following are some examples of arithmetic operations in Python:

x = 3
y = 4
print(x + y) # addition
print(x - y) # subtraction
print(x * y) # multiplication
print(x / y) # division (returns float)
print(x // y) # floor division (returns int)
print(x % y) # modulo (returns int)
print(x ** y) # exponentiation

It’s important to keep in mind that division between two integers in Python 3 will return a float, whereas in Python 2 it will return an integer (floor division). If you want to perform floor division in Python 3, you can use the // operator. Additionally, the ** operator can be used for exponentiation.

Conclusion

In this article, we covered the different types of numbers in Python, including integers, floating-point numbers, and complex numbers. We also looked at their properties, methods, and the available arithmetic operations. Understanding numbers in Python is important for performing mathematical operations and storing values in your programs.

Leave a Reply

Your email address will not be published. Required fields are marked *