Hello dear readers! Welcome back to another edition of our tutorial on Python. In this tutorial guide, we are going to be discussing about the Python Numbers.
Number types are used in storing numeric values. They are fixed data types, meaning that changing the values of a number data type would result to a newly allocated object.
Number objects are created when you assign a value to them. For example -
Number types are used in storing numeric values. They are fixed data types, meaning that changing the values of a number data type would result to a newly allocated object.
Number objects are created when you assign a value to them. For example -
var1 = 5 var2 = 50
You can also delete the reference to a number object using the del statement. The syntax for using the del statement is as follows -
del var1[, var2[, var3[...., varN]]]]
The del statement can be used to delete a single object or multiple objects. For example -
del var del var_a, var_b
RECOMMENDED POST: Python Pass Statement
Python language supports four distinct numeric types -
- int (signed integers) - They are often called just integers or ints. They are positive or negative whole numbers with no decimal point.
- long (long integers) - They are also called long. These are integers with unlimited size, written like integers and followed by an uppercase or a lowercase L.
- float (floating point real values) - Also called floats, they represent real numbers and are written with decimal point that can be used for dividing the integer and the fractional parts. Floats may also be in scientific notation, with either E or e indicating the power of 10.
- complex (complex numbers) - They are often of the form a + bJ, where ab are floats and here J or (j) represents the square root of -1 (which is an imaginary number). a is the real part of the number, and b is the imaginary part. The complex numbers are not used much in Python.
Example
The following below 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 |
RECOMMENDED POST: A Guide to Python Break Statement
- Python allows you to make use of a lowercase L with the long integer, but it is advised that you make use of only an uppercase L so as to avoid confusion with the number 1. It displays long integers with an uppercase L.
- A complex number consists of an ordered pair of real floating point numbers that is denoted by a + bj, where a is the real section of the complex number and b is the imaginary part.
Number Type Conversion
Python program converts numbers internally in an expression which contains mixed data types to a common type for evaluations. But sometimes, you need to coerce a number explicitly from one type to another in order to meet up with the requirements of an operator or function parameter.
- Type int(x) to convert x to a plain integer.
- Type long(x) to convert x to a long integer.
- Type float(x) to convert x to a floating point number.
- Type complex(x) to convert x to a complex number with real part x and the imaginary part zero.
- Type complex(x, y) in order to convert both x and y to a complex number with x as the real part and then y as the imaginary part.
RECOMMENDED POST: For Loop Statements in Python
Mathematical Functions
Python includes the following functions that is used 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
|
In our subsequent tutorials, we are going to be discussing about the above listed functions.
RECOMMENDED: Python Nested If Statements
Random Number Functions
Random numbers in Python are used for games, privacy, security, testing and stimulation of apps. Python includes the following functions that are commonly used -
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
|
In our subsequent tutorials, we are going to be discussing about the above listed functions.
RECOMMENDED POST: Python if Statement Tutorial with examples
Trigonometric Functions
Python includes the following functions that are used to perform trigonometric functions.
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.
|
In our subsequent tutorials, we are going to be discussing about the above listed functions.
RECOMMENDED: Decision Making in Python
Mathematical Constants
Python defines two mathematical constants -
Sr.No. | Constants & Description |
---|---|
1 |
pi
The mathematical constant pi.
|
2 |
e
The mathematical constant e.
|
Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the Python Mathematical Functions.
Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.