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 hello_world.py or hello_user.py 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)
0.6931471805599453
>>> math.log10(10)
1.0
>>> math.log(15, 15)
1.0
>>> math.sin(math.pi)
1.2246467991473532e-16
>>> math.sin(0)
0.0
>>> math.sin(math.pi/2)
1.0
>>> math.cos(math.pi)
-1.0
>>> math.tan(math.pi/2)
1.633123935319537e+16
>>> math.tan(math.pi/4)
0.9999999999999999
>>> math.cos(90*math.pi/180)
6.123233995736766e-17
>>> math.ceil(4.32)
5.0
>>> math.floor(4.32)
4.0
>>> math.factorial(4)
24
>>> math.factorial(25)
15511210043330985984000000L
>>> math.factorial(90)
1485715964481761497309522733620825737885569961284688766942216863704985393094065876545992131370884059645617234469978112000000000000000000000L
>>> math.factorial(900)
67526802209645841583879061361800814224269427869589384312198268703685091643180416969132446952698303794226010370578672908593198347699886928591906501031587651846976759681112609524787093848004428636186893395272784450630354080243217646658024696659065951793757223520229235577548653833681102170973893746054649126415909143150172860721156685810655759230011450132992176454983227538696340112610447029002337004887877266387704586077293585433151612518800147764461182680822867092786694982831838641800997499819339206579415325649748486265233918911087114592440896594062675914294925816719862178374679272092637524786939036290035924271782253738059886933923447877769583003016705363339031413069155837518524761078342052635475632113169618774549275701480106933362990003732589370593557325299434734459295866728988740794174654391479926000848846686708729736713207285203712732201272410830836913052635365082888725171636081587151603468291106754640398232146673627370895934090777828827549554232436190464827998683927179246029919443251026464452337939599198528297828591122689960620361238248313158071643395848405047261412680039877733761849874447323867911712630023171745968278465780558568067035013885275080292137360491875164947724464221693533755035300065350065137490832039523382963747026185653050331832380991844842560750923543775188582096487476950254418365198999674684417286265442786651594404781622946901879166382930714196908227460133027605817864877377712193142137625430353718448269390732615776645283198828602917680224041088993892610506802195917247838900106910698057030379190571057605849323113308634452008179881165616449767648354161225066967961297609698742737923389391615207441152319392845687673311899247085327703421862972871644495409572259985563215471482083325653231777113271326579970310755604973969708949477374254974480294652427022436705380184064008853457214518515270985563195412993145274057688634448812449445800617631162768243125606424844709372022149908463572254912654907763445758543980999149122998104378965626781898655221443263601405152073199706585080288735040205417371277253096243200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L
>>> math.isinf(math.tan(math.pi/2))
False
>>> math.exp(2)
7.38905609893065
>>> 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)
1e+200
>>> math.sqrt(25)
5.0
>>> math.sqrt(25567)
159.89684174492004
>>> math.radians(30)
0.5235987755982988
>>> math.radians(90)
1.5707963267948966
>>> math.tan(math.radians(90))
1.633123935319537e+16
>>> 

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);
7
>>> 

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