Numbers in Python
Numbers are fundamental to programming, and Python provides robust support for working with various types of numbers.
Whether you need to perform basic arithmetic operations or complex mathematical calculations, Python offers a versatile set of tools.
In this module, we will explore the different types of numbers in Python, their properties, and learn through code examples to help you understand their usage.
Integer Numbers
Integers are whole numbers without any fractional or decimal parts. In Python, integer numbers are represented by the int data type. They can be positive, negative, or zero. Here's an example:
>>> x = 42
>>> y = -10
>>> z = 0
NOTE: Arithmetic operations such as addition, subtraction, multiplication, and division can be performed on integer numbers using the familiar operators +, -, *, and /.
Floating-Point Numbers
Floating-point numbers, commonly known as floats, represent numbers with a decimal point. Python uses the float data type to handle these numbers.
Here's an example:
>>> pi = 3.14
>>> radius = 2.5
NOTE: Floats allow you to perform both basic and advanced mathematical operations. Python also provides functions for rounding, finding the absolute value, and accessing mathematical constants like pi.
Complex Numbers
Complex numbers consist of a real part and an imaginary part. In Python, complex numbers are denoted by the complex data type.
The imaginary part is represented using the suffix j or J. Here's an example:
>>> z = 2 + 3j
NOTE: Python supports arithmetic operations on complex numbers, including addition, subtraction, multiplication, and division. Additionally, you can access the real and imaginary parts using the real and imag attributes, respectively.
Number Conversions
Python provides built-in functions to convert numbers between different types. These conversion functions are:
- int(): Converts a number or a string to an integer.
- float(): Converts a number or a string to a float.
- complex(): Converts a number or a string to a complex number.
Here are a few examples:
>>> x = int(3.14)
>>> # x will be 3
>>> y = float("42.5")
>>> # y will be 42.5
>>> z = complex(2, 3)
>>> # z will be 2 + 3j
Mathematical Functions and Modules
Python provides a rich set of mathematical functions and modules to perform complex mathematical operations.
Some commonly used modules include math and cmath for real and complex number operations, respectively. Refer to the example below:
>>> import math
>>> x = math.sqrt(16)
>>> # x will be 4.0
>>> y = math.sin(math.radians(45))
>>> # y will be 0.70710678118
>>> import cmath
>>> z = cmath.sqrt(-1)
>>> # z will be 1j
Random Numbers
Python's random module allows you to generate random numbers. You can generate random integers, floats, or even choose a random element from a list. An example for you:
>>> import random
>>> x = random.randint(1, 10)
>>> # x will be a random integer between 1 and 10
>>> y = random.random()
>>> # y will be a random float between 0 and 1
>>> z = random.choice([1, 2, 3, 4, 5])
>>> # z will be a random element from the list
Module Takeaways:
Understanding how to work with numbers in Python is essential for any programmer. In this module, we covered integer numbers, floating-point numbers, complex numbers, number conversions, mathematical functions, and modules, as well as generating random numbers.
By leveraging Python's built-in capabilities and libraries, you can efficiently handle various numerical operations in your programs.
Remember to experiment with code examples and explore additional resources to deepen your knowledge and proficiency with numbers in Python and keep following along on this module journey to master Python where the next stop is: Booleans!