Mathematics And Python

So yesterday was interesting. Interesting but hectic. We had a live chat* on the topic Mathematics and Python. Here’s a very summary of what we discussed.

We started our discussion with basic Python operations :
Basic operations such as addition (+), subtraction (-), multiplication (*), division (/) and exponentiation (**) work (mostly) as expected:


Parenthesis can be used for grouping:

The only problem(its not really a problem) one could encounter while doing basic operations in Python is the issue of Integer Division.

newokThis phenomenon is known as integer division: because
we provide two integer numbers (15 and 6) to the division operator (/), the assumption that Python makes is that we seek a return value of type integer. The mathematically correct answer is (the floating point number) 2.5.

There are basically two ways of fixing this :

. Making use of Python’s future division: it has been decided that from Python 3.0 onwards the division operator will return a floating point number (complex, if required) even if the numerator
and denominator are of integer type. This feature can be activated in older (2.x) Python versions with the from future import division statement:from_future__ Note: The   from  __future__ import division module should always be the first line of your code.

2. You could ensure that at least one number (numerator or                         denominator) is of type oat (or complex), the division operator will return a floating point number. This can be done by writing 15. instead of 15, of by forcing conversion of the number to a oat, i.e. use float(15) instead of 15:


We then we on to exclusively discuss the python math module.

This is a Python module used to bring alive  mathematical functions
such as sin, cos, exp, log and many others are located in the mathematics module with name math.
We can make use of this as soon as we import the math module:import

Using the dir function, we can see the directory of objects available in the math module:

The help function can provide more information about the module (help(math)) on individual objects.helpThe mathematics module also recognizes constants like pi and e:method

That was basically all we discussed yesterday. Since this is really not a class, we would let the pictures speak for themselves now.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s