…little BITS of our lives.

Archive for the ‘Python tutorials’ Category

Shell Basics 1 – The Python Calcoolator

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…

The Python Shell

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.

The Python Shell

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!“.

Traceback Error

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”.

import math

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:

Square root working properly

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!

Whoa!!! Is that what we are talking about?

Snakes!!!

Am I nuts trying to get you something which “Discovery” hasn’t got covered yet?

No, No! Not at all! Snakes sure are scary creatures, but our Python is one of the friendliest out there, totally harmless and always a delight to talk to. I will let you see, what our python looks like.

Although it seems that there are two snakes, one blue and the other yellow, Python is neither of them. It is a programming language.
Read more about its history here: http://en.wikipedia.org/wiki/Python_language

So? Did you read that? Really?

I bet most of you decided to skip over that. After all, if Wikipedia has all the content, what’s this tutorial gonna do!
Well, we will talk about Python here as well, but it is a good idea to be sure that the thing is really worth giving some time, ’cause if it happens to be a snake that’s being talked about, well, not truly my taste…!
Anyway, even if you skipped the content at wikipedia, here’s the basic thing you need to know before starting out with Python.

Python is an interpreted programming language that has become the language of choice at various places (Google for example) because of its simplicity and the power which allows it to do almost anything.

Interested?
Well, Lets get started then!

We will be talking about Python 3. Yes, the number 3 stands for the version. In the computing world, whenever a software is released, it is enhanced over time, bugs are removed, interfaces are improved and lots of other modifications are done to better suit the needs of the user. The software is released again and that number there helps identify the newer version from the older ones.
The latest version is Python 3.2.1.

Someone out there: Wow! So, Python is actually not a snake, but a software living inside my computer. Now I
trust you,tell me more.

Yes, Python is a software, a programming language. (that’s what I had been saying for so long!) But it does not live inside your computer already, unless you are using LINUX or MAC OS. So, let’s give Python a home.

    Go to the website: http://python.org and then click on their downloads section.
    Depending upon your OS, choose your installer of Python. (remember version 3.2.1)
    Download and install it.

That should have been simple.
So what have we got by doing that?
More on that later. I gotta run now! Also I think it would be better if I made a video about the stuff I wanna teach. Really? teach?