# Numbers

Number data type stores numeric values. They are immutable data types i.e changing the value of a number data type results in a newly allocated object.

Number objects are created when you assign a value to them. For example

val1 = 1 val2 = 15

To delete a reference to a number object, you use the del statement as follows:

`del val1`

Python supports the following numerical types:

**Int (signed integer)** – they are positive or negative whole numbers without a decimal point.

**Float – **these represent real numbers with decimal points eg 109.50

**Complex numbers** – these use the j or J suffix to indicate the imaginary part (e.g. 3+5j).

**Number Type Conversion**

We use type conversion to convert one type to another in order to satisfy certain requirements of an operator or a function

Type **int(num)** to convert a number to a plain integer

Type** float(num) **to convert a number to a decimal or floating-point number

Type **complex(num)** to convert a num to a complex number with real part num and an imaginary part 0

### Mathematical Function

Python supports various Maths functions. Here are some of the common functions:

**max(num1, num2) ** – finds the largest of given arguments

**min(num1, num2**) – finds the smallest of the given arguments

**pow(num, exp)** – returns the power of a number i.e result of num**exp

**sqrt(num)** – returns the square-root of a number

You can check out more maths functions supported by Python in the official documentation.

**Random Number Function**

Python defines a set of functions that are used to generate or manipulate random numbers. This particular type of functions are used in a lot of games, lotteries or any application requiring random number generation.

The following are some of the random functions:

**choice(seq) **– returns a random number from a list, tuple or strings

**randomrange([start,], stop, [,step]) **A randomly selected element from range(start, stop, step).

**random() ** – This number is used to generate a** float random number less than 1 and greater or equal to 0.**

**seed() –** Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None.

**shuffle(lst)** – Randomizes the items of a list in place. Returns None.

**uniform(a,b) **– This function is used to generate a **floating-point random number between the numbers** mentioned in its arguments.** It takes two arguments, lower limit(included in generation) and upper limit(not included in generation).**

Example to illutstrate random number functions

```
python"># Python code to demonstrate the working of random functions and maths functions
# importing "random" for random operations
import random
# Initializing list
li = [1, 4, 5, 10, 2]
# Examples of math functions, max, min, pow
print min(2,4,5,8)
print max(4,2,5,1)
print pow(2,2)
# Printing list before shuffling
print ("The list before shuffling is : ")
print li
# using shuffle() to shuffle the list
random.shuffle(li)
# Printing list after shuffling
print ("The list after shuffling is : ")
print li
# using uniform() to generate random floating number in range
# prints number between 5 and 10
print ("The random floating point number between 5 and 10 is : ")
print (random.uniform(5,10))
# using random() to generate a random number
# between 0 and 1
print ("A random number between 0 and 1 is : ")
print (random.random())
# using seed() to seed a random number
random.seed(5)
# printing mapped random number
print ("The mapped random number with 5 is : ")
print (random.random())
# using choice() to generate a random number from a given list of numbers.
print ("A random number from list is : ")
print (random.choice([1, 4, 8, 10, 3]))
# using randrange() to generate in range from 20 to 50. The last parameter 3 is step size to skip
# three numbers when selecting.
print ("A random number from range is : ")
print (random.randrange(20, 50, 3))
```

The output of the above when executed will be as follows. Note the result will be different each time you run it.

The list before shuffling is : [1, 4, 5, 10, 2] The list after shuffling is : [5, 1, 4, 10, 2] The random floating point number between 5 and 10 is : 6.47318352507 A random number between 0 and 1 is : 0.630486433207 The mapped random number with 5 is : 0.62290169489 A random number from list is : 10 A random number from range is : 41