First of all, you need to know that Math and Numbers in Python is quite easy to follow. This is because, you only need to learn the functions you need to use. Therefore, we are going to examine Math and Numbers in Python.
We would cover the following
- Python Number Types
- Numeric Type Conversion
- Math Functions in Python
- Random Number Functions
- Trig Functions in Python
- Watch the Video
For the reason that Python is an easy language to learn, the same goes for the Python Math as well. The first thing you need to remember is that you don’t need to declare a number variable before use.
You simply assign a number to a variable. Once you do this, a number object is created. Memory is allocated as well.
num1 = 34
num2 = 2.5
The del keyword
This keyword is used to delete the reference to a number object. So you can say
2. Python Number Types
Python provides number types that can be used to manipulate numbers. Although it has been discussed under Python Data Types, we would do a brief review of the four Python Number types.
int: This represents unsigned integers. They could be positive or negative. But the important thing to remember is that they contain no decimal point
Examples of ints: 300, 123, 34, 56
long: They are also integers. However they have unlimited size. Unlike integers that are 32 bit. The create a long, you write the number followed by the letter L.
Examples of long: 83948495L, 03335L, 0x3085L
float: These are numbers written with a decimal point. The decimal point separates the integer part and the decimal part.
Examples of floats: 55.09, -45., 56.1e10, -45e20
complex: You may not use much of this though. It represents a number having two parts: a complex part and an imaginary part
Examples of complex numbers: 1 + 2j, 3 +4j, 10 + 12j
3. Numeric Type Conversion
You can convert from one number type to another. To do that, you can use any of the following functions:
int (a): this function converts the number a to an int
long(a): you use this to convert the number a to a long
float(a): use this function to convert the number a to a float
complex (a): use this function to convert the number a, to a complex number
complex(a, b): this function creates a complex number using the parameters a and b
bin(a): converts a number to a binary representation
4. Mathematical Functions
Python provides you with many of the useful functions needed to perform mathematical operations. They are listed in the table below:
|SN.||Function and brief description|
The absolute value of x: that is the (positive) distance between zero and x.
The ceiling of x: that is the smallest integer not less than x
Returns -1 if x < y, 0 if x == y, or 1 if x > y
Returns the exponential of x: ex
Returns the absolute value of x.
The floor of x: that is the largest integer not greater than x
Returns the natural logarithm of x, for x> 0
Returns the base-10 logarithm of x for x> 0.
Returns the largest of its arguments: the value closest to positive infinity
Returns the smallest of its arguments: the value closest to negative infinity
Returns 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.
Returns the value of x**y. That is x raise to power y
x rounded to n decimal digits. Python rounds away from zero: so round(0.5) is 1.0 while round(-0.5) is -1.0.
Returns the square root of x for x > 0
5. Random Number Function
These are other set of functions you need to know. They are used to either generate random numbers or select randomly from a list of numbers. It is applied in games, cryptography/encryption, simulation, software testing and other applications.
|SN||Function and brief Description|
A random item from a list, tuple, or string.
|2||randrange ([start,] stop [,step])|
Returns a randomly selected element from range(start, stop, step)
A random float r, such that 0 is less than or equal to r and r is less than 1
Sets the integer start value used for generating random numbers. You call this function before calling any other random module function.
Randomizes the items of a list in place. Returns None.
A random float s between x and y. such that x is less than or equal to s and s is less than y
6. Trig Functions
This is another set of functions you may not be using too often. But it’s necessary you know them.
|SN||Function and brief description|
Return the arc cosine of x, in radians.
Return the arc sine of x, in radians.
Return the arc tangent of x, in radians.
Return atan(y / x), in radians.
Return the cosine of x radians.
Return the Euclidean norm, sqrt(x*x + y*y).
Return the sine of x radians.
Return the tangent of x radians.
Converts angle x from radians to degrees.
Converts angle x from degrees to radians.