# Python : Numbers

In Python, we have Number datatype that store numeric values, used to complete numeric operations through pythons codes. These are immutable datatype that could change during any operation.

>>> a=12 >>> b=24 >>> a+b 36 >>> a*b 288 >>> b-a 12

See above we did many common operations in this small code. If we have some serious code, we can use them to do some important development project.

We can delete or remove these variables assigned during code.

>>> a 12 >>> del a >>> a Traceback (most recent call last): File "", line 1, in NameError: name 'a' is not defined

As above code we delete or unassigned a variable name a which visible with error once its deleted.

As said these are immutable and we can change these anytime during operation.

>>> a=12 >>> id(a) 94690961708176 >>> a=36 >>> id(a) 94690961709592

After assigning new value , it shows another memory address show in id command on same variable name.

For during various operation we have many in build functions and classes in python that make our work easy. So let’s see some examples, where we could understand these scenarios.

For numeric operation we should use math and numpy module , these modules has various functions and methods which helps to do numeric operations.

Import numpy and math. Math is default module but numpy is not default we have to install it through python pip (pip install numpy)

### 1. Sum of list

we can easily sum of all number in list or array. Let’s see how

>>> lst=[12,2,356,24] >>> numpy.sum(lst) 394

### 2. Square root of number of List of numbers.

>>> a 4 >>> math.sqrt(a) 2.0 >>> numpy.sqrt(lst) array([ 3.46410162, 1.41421356, 18.86796226, 4.89897949])

### 3. Max and Min number in list of number.

>>> lst [12, 2, 356, 24] >>> numpy.min(lst) 2 >>> numpy.max(lst) 356

### 4. Sine, Cosine,tangent of any number or list of number

>>> numpy.cos(lst) array([ 0.84385396, -0.41614684, -0.54027694, 0.42417901]) >>> numpy.sin(lst) array([-0.53657292, 0.90929743, -0.84148727, -0.90557836]) >>> numpy.tan(lst) array([-0.63585993, -2.18503986, 1.55751099, -2.1348967 ])

### 5. Some important variable of math module.

>>> math.pi 3.141592653589793 >>> math.e 2.718281828459045

### 6. There is another module name random

which has also some method for numeric operations.

>>> import random >>> random.choice(lst) 24 >>> lst [356, 24, 2, 12] >>> random.shuffle(lst) >>> lst [356, 12, 2, 24]

In above examples, we can see , we cab select any random number and can also shuffle number list in sequences. We also have some other methods in same random modules that can do other operations. List of methods in random module.

>>> dir(random) ['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', 'WichmannHill', '_BuiltinMethodType', '_MethodType', '__all__', '__builtins__', '__doc__', '__f ile__', '__name__', '__package__', '_acos', '_ceil', '_cos', '_e', '_exp', '_hashlib', '_hexlify', '_inst', '_log', '_pi', '_random', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_ warn', 'betavariate', 'choice', 'division', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'jumpahead', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'rando m', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']

Although this is not complete list of modules and their methods. Its also not possible to complete them in one post, but my intention is only to make you understand that we have plenty of such modules and methods to work on numbers. So With all above examples now we know we have many modules and their methods to work on numeric operation in python which has fair enough potential to complete requirement.

## Leave a Reply