In this tutorial, you will learn how to write and work with numbers in Python. Python supports various numerical data types such as integers, floats, and complex numbers. You will also discover different ways to format and display numbers in a user-friendly manner. Let’s dive in!
Step 1: Understand Python number data types
Python supports three different numerical data types:
- Integers (int) – whole numbers (e.g., -3, 0, 42)
- Floating-point numbers (float) – real numbers with a decimal point (e.g., -3.0, 0.0, 3.14)
- Complex numbers (complex) – numbers with a real part and an imaginary part (e.g., 1 + 3j, -5 + 2j)
Here’s an example of how to assign numbers of each data type to variables:
1 2 3 |
x = 7 # Integer y = 3.14 # Float z = 2 + 3j # Complex |
Step 2: Perform basic arithmetic operations
Python allows you to perform basic arithmetic operations such as addition, subtraction, multiplication, and division of numbers. Here are some examples:
1 2 3 4 |
add_result = x + y subt_result = x - y mult_result = x * y div_result = x / y |
You can also use the double-asterisk (**
) to raise a number to a power and the modulus operator (%
) to find the remainder of a division:
1 2 |
power_result = x ** 2 modulus_result = x % 3 |
Step 3: Work with number-related functions
Python provides several built-in functions to perform common operations on numbers:
- abs() – Returns the absolute value of a number
- round() – Rounds a floating-point number to the nearest integer
- min() and
max()
– Return the smallest and largest numbers in a given sequence, respectively - sum() – Returns the sum of all items in a given sequence.
Here’s an example of how to use these functions:
1 2 3 4 5 |
abs_val = abs(-3.14) rounded_val = round(3.14159) min_val = min(3, 6, 9) max_val = max(1, 2, 3) sum_val = sum([1, 2, 3, 4, 5]) |
Step 4: Use the math module for advanced operations
For more advanced numerical operations, you can use Python’s built-in math
module. This module provides functions such as square root, logarithm, trigonometry functions, and constants like π.
First, you need to import the math module:
1 |
import math |
Then, you can use various functions from the math
module as follows:
1 2 3 4 |
sqrt_result = math.sqrt(64) log_result = math.log10(100) sin_result = math.sin(math.radians(30)) pi_value = math.pi |
Step 5: Format and display numbers
To display numbers in a formatted manner, you can use Python’s built-in string formatting. The format()
function allows you to easily format numbers as strings.
Here are some examples of using the format()
function to display numbers:
1 2 3 |
formatted_num1 = format(12345, ',') # 12,345 formatted_num2 = format(123.456, '.2f') # 123.46 formatted_num3 = format(0.6789, '.1%') # 67.9% |
For more advanced formatting options, see the Python documentation on format specification.
Conclusion
Now you know how to work with numbers in Python, from understanding different data types to performing basic arithmetic operations, using built-in functions and the math
module, and displaying numbers in a formatted way. Keep exploring and experimenting with Python to enhance your numerical skills!