In this article, you will learn what numbers are used in Python, how to convert one data type to another and what mathematical operations are supported in Python.
Numeric data types store numeric values. They are immutable data types, which means that changing the value of a numeric data type results in a new object being assigned.
Numeric objects are created when they are assigned a value. For example:
a1 = 1
b2 = 10
You can also delete the reference to a numeric object with the del statement. The syntax of the del statement is –
del a1[,a2[,a3[....,aN]]]]
You can delete a single object or multiple objects with the del statement. For example:
del var
del var_a1, var_b1
Python supports four different numeric types, namely
int (signed integers) – Usually called simply integers or ints, these are positive or negative integers without a decimal point.
long (long integers) – Also called long, these are integers of unlimited size written as integers followed by an upper or lower case L.
float (floating point real values) – Also called floats, these represent real numbers and are written with a decimal point separating the integer and fractional parts. Floats can also be written in scientific notation, where E or e represents the power of 10 (2.5e2 = 2.5 x 102 = 250).
complexes (complex numbers) – have the form a + bJ, where a and b are floating point numbers and J (or j) is the square root of -1 (which is an imaginary number). The real part of the number is a and the imaginary part is b. Complex numbers are not commonly used in Python programming.
Examples
Here are some examples of numbers
int | long | float | complex |
---|---|---|---|
10 | 51924361L | 0.0 | 3.14j |
100 | -0x19323L | 15.20 | 45.j |
-786 | 0122L | -21.9 | 9.322e-36j |
080 | 0xDEFABCECBDAECBFBAEL | 32.3+e18 | .876j |
-0490 | 535633629843L | -90. | -.6545+0J |
-0x260 | -052318172735L | -32.54e100 | 3e+26J |
0x69 | -4721885298529L | 70.2-E12 | 4.53e-7j |
Python allows the use of a small L with long, but it is recommended to use only a large L to avoid confusion with the number 1. Python displays long integers with a capital L.
A complex number consists of an ordered pair of floating-point real numbers denoted a + bj, where a is the real part and b is the imaginary part of the complex number.
Number type conversion
Python internally converts the numbers in an expression that contains mixed types to a common type for evaluation. But sometimes it is necessary to explicitly force a number from one type to another to meet the requirements of an operator or function parameter.
Type int(x) to convert x to a simple integer.
Type long(x) to convert x to a long integer.
Type float(x) to convert x to a floating point number.
Write complex(x) to convert x to a complex number with real part x and imaginary part zero.
Write complex(x, y) to convert x and y into a complex number with real part x and imaginary part y. x and y are numerical expressions.
Mathematical functions
Python contains the following functions to perform mathematical calculations.
Sr.No. | Function & Returns ( description ) |
---|---|
1 | abs(x) The absolute value of x: the (positive) distance between x and zero. |
2 | ceil(x) The ceiling of x: the smallest integer not less than x |
3 | cmp(x, y) -1 if x < y, 0 if x == y, or 1 if x > y |
4 | exp(x) The exponential of x: ex |
5 | fabs(x) The absolute value of x. |
6 | floor(x) The floor of x: the largest integer not greater than x |
7 | log(x) The natural logarithm of x, for x> 0 |
8 | log10(x) The base-10 logarithm of x for x> 0. |
9 | max(x1, x2,…) The largest of its arguments: the value closest to positive infinity |
10 | min(x1, x2,…) The smallest of its arguments: the value closest to negative infinity |
11 | modf(x) The fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The integer part is returned as a float. |
12 | pow(x, y) The value of x**y. |
13 | round(x [,n]) x rounded to n digits from the decimal point. Python rounds away from zero as a tie-breaker: round(0.5) is 1.0 and round(-0.5) is -1.0. |
14 | sqrt(x) The square root of x for x > 0 |
Random number functions
Random numbers are used for games, simulations, tests, and security and privacy applications. Python contains the following commonly used functions.
Sr.No. | Function & Description |
---|---|
1 | choice(seq) A random item from a list, tuple, or string. |
2 | randrange ([start,] stop [,step]) A randomly selected element from range(start, stop, step) |
3 | random() A random float r, such that 0 is less than or equal to r and r is less than 1 |
4 | seed([x]) Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None. |
5 | shuffle(lst) Randomizes the items of a list in place. Returns None. |
6 | uniform(x, y) A random float r, such that x is less than or equal to r and r is less than y |
Trigonometric functions
Python contains the following functions that perform trigonometric calculations.
Sr.No. | Function & Description |
---|---|
1 | acos(x) Return the arc cosine of x, in radians. |
2 | asin(x) Return the arc sine of x, in radians. |
3 | atan(x) Return the arc tangent of x, in radians. |
4 | atan2(y, x) Return atan(y / x), in radians. |
5 | cos(x) Return the cosine of x radians. |
6 | hypot(x, y) Return the Euclidean norm, sqrt(x*x + y*y). |
7 | sin(x) Return the sine of x radians. |
8 | tan(x) Return the tangent of x radians. |
9 | degrees(x) Converts angle x from radians to degrees. |
10 | radians(x) Converts angle x from degrees to radians. |
Mathematical constants
The module also defines two mathematical constants -.
Sr.No. | Constants & Description |
---|---|
1 | pi The mathematical constant pi. |
2 | e The mathematical constant e. |
Python provides support for various types of numbers, including integers, floating-point numbers, and complex numbers. Understanding how to work with these numeric data types is fundamental to writing effective Python code.
Integers
Integers are whole numbers, positive or negative, without any decimal point. In Python, you can work with integers just like you do in mathematics.
# Examples of integers x = 10 y = -5 z = 0 print(x) # Output: 10 print(y) # Output: -5 print(z) # Output: 0
Floating-Point Numbers
Floating-point numbers, or floats, are numbers that have a decimal point or use an exponential (scientific) notation. Python supports operations with floating-point numbers, including arithmetic operations.
# Examples of floating-point numbers a = 3.14 b = -0.5 c = 2.0e3 # 2.0 * 10^3 print(a) # Output: 3.14 print(b) # Output: -0.5 print(c) # Output: 2000.0
Complex Numbers
Complex numbers have a real part and an imaginary part, represented as a + bj
, where a
is the real part and b
is the imaginary part. In Python, you can create complex numbers and perform operations with them.
# Examples of complex numbers p = 2 + 3j q = -1j r = complex(1, -2) print(p) # Output: (2+3j) print(q) # Output: -1j print(r) # Output: (1-2j)
Arithmetic Operations
You can perform arithmetic operations on numeric data types in Python. These include addition (+
), subtraction (-
), multiplication (*
), division (/
), exponentiation (**
), and modulo (%
).
# Arithmetic operations a = 10 b = 3 print(a + b) # Addition, Output: 13 print(a - b) # Subtraction, Output: 7 print(a * b) # Multiplication, Output: 30 print(a / b) # Division, Output: 3.3333333333333335 print(a ** b) # Exponentiation, Output: 1000 print(a % b) # Modulo, Output: 1
Understanding Python’s numeric data types and how to perform arithmetic operations with them is crucial for developing Python programs. Whether you’re working with integers, floating-point numbers, or complex numbers, Python provides robust support for numerical computations.