Python supports different types of numerical data, including integers, floating-point numbers, and complex numbers.
Here are some examples of how to use numbers in Python:
Integers are represented by the int data type in Python. You can perform arithmetic operations on integers using the standard mathematical operators (+, -, *, /, %) as shown below:
x = 10 y = 5 print(x + y) # Output: 15 print(x - y) # Output: 5 print(x * y) # Output: 50 print(x / y) # Output: 2.0 print(x % y) # Output: 0
Floating-point numbers are represented by the float data type in Python. You can perform arithmetic operations on floats using the same operators as for integers:
x = 3.14 y = 2.5 print(x + y) # Output: 5.64 print(x - y) # Output: 0.64 print(x * y) # Output: 7.85 print(x / y) # Output: 1.256 print(x % y) # Output: 0.64
Complex numbers are represented by the complex data type in Python.
You can create a complex number by specifying its real and imaginary components using the syntax a + bj
, where a
is the real part and b
is the imaginary part:
z = 2 + 3j print(z.real) # Output: 2.0 print(z.imag) # Output: 3.0 print(abs(z)) # Output: 3.605551275463989
You can perform arithmetic operations on complex numbers using the same operators as for integers and floats:
z1 = 2 + 3j z2 = 1 - 2j print(z1 + z2) # Output: (3+1j) print(z1 - z2) # Output: (1+5j) print(z1 * z2) # Output: (8-1j) print(z1 / z2) # Output: (-0.4+1.6j)
Python provides several built-in functions for working with numbers. For example, you can use the round()
function to round a floating-point number to a specified number of decimal places:
x = 3.14159265359 print(round(x, 2)) # Output: 3.14
You can use the abs()
function to get the absolute value of a number
x = -10 print(abs(x)) # Output: 10
You can use the pow()
function to raise a number to a power:
x = 2 y = 3 print(pow(x, y)) # Output: 8
You can use the min()
and max()
functions to find the minimum and maximum values in a set of numbers:
x = [1, 2, 3, 4, 5] print(min(x)) # Output: 1 print(max(x)) # Output: 5
You can convert a number from one data type to another using type conversion functions like int()
, float()
, and complex()
. For example:
x = 10 y = float(x) z = complex(x) print(y) # Output: 10.0 print(z) # Output: (10+0j)
The math
module provides a wide range of mathematical functions that can be used to perform complex calculations. To use the functions in the math
module, you need to import it first using the import
statement. For example:
import math x = 3.14 print(math.sin(x)) # Output: 0.0015926529164868282 print(math.exp(x)) # Output: 23.103866858722185 print(math.sqrt(25)) # Output: 5.0
Python supports arbitrary-precision integers, which means that you can work with extremely large numbers without worrying about overflow errors. For example:
x = 2**100 print(x) # Output: 1267650600228229401496703205376
Python has several external libraries that are specifically designed for numerical computation, such as NumPy and SciPy. These libraries provide highly optimized functions for working with arrays, matrices, and other numerical data structures. For example:
import numpy as np x = np.array([1, 2, 3, 4]) y = np.array([5, 6, 7, 8]) print(x + y) # Output: [ 6 8 10 12] print(x * y) # Output: [ 5 12 21 32]
These are just a few examples of how to use numbers in Python.
The language offers a wide range of features and functions for working with numerical data, making it a versatile tool for scientific computing and data analysis.
format()
method to format the output of numerical values. For example:
x = 3.14159 print("{:.2f}".format(x)) # Output: 3.14 print("{:.4f}".format(x)) # Output: 3.1416 print("{:.2e}".format(x)) # Output: 3.14e+00
Fraction
class in the fractions
module for working with rational numbers. For example:
from fractions import Fraction x = Fraction(3, 4) y = Fraction(1, 3) print(x + y) # Output: 13/12 print(x * y) # Output: 1/4
decimal
module provides a Decimal
class for working with decimal values. For example:
from decimal import Decimal x = Decimal('0.1') y = Decimal('0.2') z = x + y print(z) # Output: 0.3
&
(bitwise AND), |
(bitwise OR), ^
(bitwise XOR), ~
(bitwise NOT), <<
(bitwise left shift), and >>
(bitwise right shift). For example:
x = 0b1010 y = 0b1100 print(bin(x & y)) # Output: 0b1000 print(bin(x | y)) # Output: 0b1110 print(bin(x ^ y)) # Output: 0b0110 print(bin(~x)) # Output: -0b1011
These are just a few more examples of how to use numbers in Python. With its wide range of features and capabilities, Python is a powerful language for working with numerical data.
The random
module provides functions for generating random numbers. This can be useful for generating test data or for simulations.
For example:
import random x = random.random() # Generate a random number between 0 and 1 print(x) y = random.randint(1, 10) # Generate a random integer between 1 and 10 print(y) z = random.choice(['apple', 'banana', 'cherry']) # Choose a random element from a list print(z)
The statistics
module provides functions for calculating statistical values such as the mean, median, and standard deviation.
For example:
import statistics data = [1, 2, 3, 4, 5, 6, 7, 8, 9] mean = statistics.mean(data) median = statistics.median(data) stdev = statistics.stdev(data) print(mean) # Output: 5 print(median) # Output: 5 print(stdev) # Output: 2.7386127875258306
The math
module provides functions for performing trigonometric calculations.
For example:
import math x = math.sin(math.pi/2) y = math.cos(math.pi/4) z = math.tan(math.pi/3) print(x) # Output: 1.0 print(y) # Output: 0.7071067811865476 print(z) # Output: 1.7320508075688772
The math
module provides functions for performing logarithmic and exponential calculations.
For example:
import math x = math.log(10) y = math.log10(100) z = math.exp(2) print(x) # Output: 2.302585092994046 print(y) # Output: 2.0 print(z) # Output: 7.3890560989306495
These are just a few more examples of how to use numbers in Python. With its vast collection of built-in modules and third-party libraries, Python is a versatile language for working with numerical data.
The numpy
module provides powerful numerical computing tools for working with arrays and matrices.
For example:
import numpy as np x = np.array([1, 2, 3]) y = np.array([4, 5, 6]) z = np.dot(x, y) print(z) # Output: 32
The pandas
module provides tools for working with data in a tabular format.
This can be useful for cleaning and analyzing data sets.
For example:
import pandas as pd data = {'name': ['omar', 'Gogo', 'Adam'], 'age': [18,13, 5]} df = pd.DataFrame(data) print(df)
Output:
name age
0 omar 18
1 Gogo 13
2 Adam 5
sympy
module provides tools for performing symbolic mathematics. For example:
import sympy x = sympy.Symbol('x') y = sympy.Symbol('y') expr = (x + y)**2 simplified_expr = sympy.simplify(expr) print(simplified_expr)
Output:
x**2 + 2*x*y + y**2