Okay so finally I got the time to tell you about the coolest calculator in the world – “The Python Shell“.
You might not agree with me now, as you might be in the habit of working with those handy little gadgets you can put in your pocket and carry anywhere.
But let’s talk about Python and you will start liking it before you know it!
(Before reading the stuff below, make sure that you have python (preferably python3) installed on your computer).
Okay, so you have it! Good, lets proceed.
1. Fire up the Python shell…
How, you say?
Well, if you are on a linux machine, press alt-F2 and then type “idle”. Go ahead, do it!
And you will see something like this…
For a windows machine, just go to the start menu and type “idle” and press “enter”. And you will be all set to dive into the python world.
Sorry for the mac users, I am not as rich as you are, but there are loads of content on internet to guide you.
Anyway, the screen you see in front of you is the “python shell” or our cal-cool-lator.
One of the things to notice is the “>>>” sign. That is just your command prompt which indicates that “hey dude! you can type your command here”.
2. What does it do?
Well everything a calculator can…
Just type in “2 + 2” and see what happens…
“So python shell can add 2 numbers, big thing, huh? Can it multiply or divide or subtract?”, you say.
Well ofcourse it does! Just go ahead and type in some expressions you enter in your calculator and see the magic happen.
To get yourself started, you can use the following expressions :
1) 4 – 3
2) 8 * 3
3) 17 / 10
4) 5 – 100
5) 3 * -1
6) 123 / -123
Note down what results your shell gave you and match them with the image below:
To enlarge the image just click on it.
Okay now, did you check out a few interesting things so far?
You did? Well, cool! and if you didn’t don’t worry, but from next time be on the look out for peculiar things.
1. The text you type appears in color “black” but the answer appears in “blue”. LOL!!!
2. The important thing: Python shell can do floating point division automatically. Well, python3 can. In python2 if you type 17 / 10, the answer will be 1, because most programming languages out there, truncate the decimal part if you don’t explicitly specify it to be a floating point calculation.
To get the same result in python2, you should type 17.0 / 10.0, but in python3 all’s well!
You: “But my calculator can find a log! a square root!Can python do that?”
Me: “Go try that out! To find the square root, you have to use the sqrt() function. Go try it!
Do it first and only then read it.”
You: “When I try to do that in python shell, it gives an error message like this(see image) and it blows my brains out! What the hell is a NameError man! And what is traceback? I am not using it till you teach me how to do stuff like finding root of a number!“.
Well, my friend!
Python shell can do that too. All that’s needed is that you know how to do it.
Now getting square root or a log is a little advanced maths and so python hides it from you till you demand that from it. Pretty Economical, I will say!
Just type the following line in the shell and press “enter”.
Now type in math.sqrt(number)
Note: for number choose a value like 4 or 10 or 2.5.
Press enter and see the magic happen.
Similarly, you can calculate the log of a number.
Just type in math.log(number, base) and press enter. See the example below:
There are many more complex functions that python shell “The Calcoolator” can do, but those are for later times!
What I want to do is explain the error that you got when you tried to find the square root using the sqrt function.
The answer is that python has a rich set of built in library functions, but to use them first you gotta tell it that “hey Mr. python! I need to calculate the square root of a function. So I wanna use that function you have…what’s that called?…umm… yeah, the sqrt() one.”
But python says,” I know I have such a function, but my collection is so big, you know! You need to help me locate where that function you need is”.
And hence python gives the “NameError”
You do that by typing in “import math” which is the library containing the function sqrt() and then python gives you the green signal to go ahead and use it whichever way you want to, which of course, is to calculate the square root.
And about the traceback thing!
Python is an interpreted language, and it executes your code line by line, without checking if your code is completely right. So if there is an error, it does a ‘traceback’ and tells you which line it found the error in!!!
One final and VERY IMPORTANT thing!
Python is a language which uses indentation to identify blocks of code:
If you don’t know what that means, don’t worry, we will deal with it later. Just make sure, you don’t leave any space after the “>>>” (called ellipsis), otherwise you will get an error like this:
I hope things I explained are clear. If there are any queries, feel free to comment or mail me to ask your questions.
Next up, we will deal with comparisons, strings and text. But that’s for next week! Hah! Too much typing already!
So go ahead and have fun with PYTHON!