Sunday, 3 January 2016

Python shell

Well, now I expect that you have set up your environment so that you can learn python on you own. May be you have run the or script and verified the output, may be you have changed something in the script and tried to run it again to experience a different output or you may have done nothing.

Whatever you have did, if you are here, then at least I can expect you can tun python on your system.

Python is good when you want to learn it. May be it looses some of the features of other languages, may be it has some extra features it adds to the set or may be it is similar to the other programming language you may have already used, we are not going into that. We are not here to judge any language rather we are here to learn new things that python can do for us.

And seriously one of the things I like the most about python is that it can work pretty perfect as a calculator. May be you are in hurry and you need to perform a very simple calculation or may be you need to just check what "58*7+98*765-9.5+80" looks like or may be in your program you have come with a condition and want to know what it actually evaluates to in the run-time, you don't have to do anything apart from opening a new terminal and typing python and finally typing your query.

That's all you have to do to perform very simple to very complex mathematical expression evaluation.

So, if you have python installed in your system, you don't need a calculator and the shell will work for you in the fastest way possible.

Enough description, now let's try with the example I started.

Open terminal and type python.

It will show up the details of the version and some other texts. Now simply key in mathematical expression and you will get the answer within a second. Let me actually show you how it looks like in the terminal,

Pretty cool right ?
Well, you can do more than this. In fact you need not write a program for learning the basics of the language.

Amazed ?
I was too.

What about standard mathematical functions ?
Can you use them ?

Well, yes, you can.

Let me show you some of them...
palash@ubuntu:~$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.log(2)
>>> math.log10(10)
>>> math.log(15, 15)
>>> math.sin(math.pi)
>>> math.sin(0)
>>> math.sin(math.pi/2)
>>> math.cos(math.pi)
>>> math.tan(math.pi/2)
>>> math.tan(math.pi/4)
>>> math.cos(90*math.pi/180)
>>> math.ceil(4.32)
>>> math.floor(4.32)
>>> math.factorial(4)
>>> math.factorial(25)
>>> math.factorial(90)
>>> math.factorial(900)
>>> math.isinf(math.tan(math.pi/2))
>>> math.exp(2)
>>> math.pow(100,100000000000000000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: math range error
>>> math.pow(100,100000000000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: math range error
>>> math.pow(100,1000000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: math range error
>>> math.pow(100,100)
>>> math.sqrt(25)
>>> math.sqrt(25567)
>>> math.radians(30)
>>> math.radians(90)
>>> math.tan(math.radians(90))

Pretty cool !!!

Once you get the hang of it, you'll surely look for python to perform some calculations. If you look carefully, you'd find that, to perform calculations like changing degree to radian or vice versa or to calculate a factorial or to get logarithm of any base, you simply need no function or method to write. All these are built in and quite fast as well, you can find that I've also performed factorial of 900, which I got in less than a second and the result seems to be correct (although I have not verified it digit by digit).

However, if you are debugging your application and you want to know the result of some calculation, just open the shell and perform the calculation straightaway.

Pretty much useful I've found.

Question arises here, what is the first line,

palash@ubuntu:~$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import math

Well, this is an import statement, which imports the standard math library of python. Without this library, we could not perform calculations which requires the library function (like math.cos, math.ceil, math.exp etc.)

Now, the question arises, we can do so many things in python shell, then can we write a whole program in it ?
Of course you can. Let me show you.
>>> def addTwoNumbers(a, b):
...  return a+b;
>>> addTwoNumbers(4,3);

So, we've defined our own function which takes two number input and returns the sum of these two numbers. After defining the function, we've simply called the function from shell with the input and this returns the result and prints it out in the shell itself.

So,  you can see that you can do almost everything in python shell. Although it is acceptable to write your whole code in the shell but it is not recommended practice. So, the better way to put your logic is in a file and run it in the shell.

Now, you may want to get out of the python shell and want to resume your console. If you want to do so, here is how you can do it,

>>> quit()
palash@Home-Computer ~ $ 

In our subsequent discussions, we'll look into the programming aspects and different coding blocks of python. Till that enjoy using python to perform calculations or pretty small stuffs.

No comments:

Post a Comment