# Python – Basic Math & Numbers 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

#### 1. Introduction

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.

For example:

num1 = 34

num2 = 2.5

The del keyword

This keyword is used to delete  the reference to a number object. So you can say

del num1

del num2

#### 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
1 abs(x)

The absolute value of x: that is the (positive) distance between zero and x.

2 ceil(x)

The ceiling of x: that is the smallest integer not less than x

3 cmp(x, y)

Returns -1 if x < y, 0 if x == y, or 1 if x > y

4 exp(x)

Returns the exponential of x: ex

5 fabs(x)

Returns the absolute value of x.

6 floor(x)

The floor of x: that is the largest integer not greater than x

7 log(x)

Returns the natural logarithm of x, for x> 0

8 log10(x)

Returns the  base-10 logarithm of x for x> 0.

9 max(x1, x2,…)

Returns the largest of its arguments: the value closest to positive infinity

10 min(x1, x2,…)

Returns the smallest of its arguments: the value closest to negative infinity

11 modf(x)

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.

12 pow(x, y)

Returns the  value of x**y. That is x raise to power y

13 round(x [,n])

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.

14 sqrt(x)

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
1 choice(sequence)

A random item from a list, tuple, or string.

2 randrange ([start,] stop [,step])

Returns 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 start value used for generating random numbers. You call this function before calling any other random module function.

5 shuffle(lst)

Randomizes the items of a list in place. Returns None.

6 uniform(x, y)

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
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.