Time has come to teach your computer to speak “Python”
Firstly, the choice of Operating system:
So what do you use?
I would personally prefer Linux, as it is open source and that is what I primarily use for my work. Although it really does not matter once you teach your computer the language, it can understand what you say as well on windows or linux.
But hold on! I will be committing a big mistake if I don’t refer to the huge support group and documentation available online. I will not be extensive over these set of tutorials, but there is a huge crowd of people who is ready to help you always at stackoverflow.com and the Python Docs
Next, I would prefer one to use the userfrienidly IPython, but is it it better to go to the shop once rather than going 1000 times, here comes the Enthought distribution , select the operating system and the 32/64 bit system( which you can find from system properties) and download the package ,and install it! (Just do next next next :p for windows). For linux its a bit tricky, you will download a “***.sh” file. save it somewhere, say downloads. Open terminal (I am assuming you have Ubuntu) with Ctrl+Alt+T( remember CAT!) , hopefully it will open in Desktop, change directory by command
chmod +x “***.sh”
[Give your password]
This should be starting the installation process, and would ask for where you want to install just type “Y” or do as it suggests 🙂
Once you are done installing , its time to start up the software,
In windows open “command” (start -> run->type “cmd”), a black box should pop up.
In Linux do the Ctrl+Alt+T thing again, and the lil box should show up.
Ok so there are few things you need to “know” to make things happen, its a repetitive thing, these are the few steps:
ok now type in:
ipython notebook –pylab [for a white good looking interface,opens up in browser] or
ipython –pylab [for nerdy looking ones]
I will probably follow notebook here, because its easier to blog in that 🙂
Once it fires up there is an option (New Notebook)
click on that, and here you have the first terminal,
Here is my first code, well as is the rule lets begin by "HelloWorld", its a computer speaking to human through human interface. Shift+Enter is the basic go to next line(Evaluate this line). So everything in this notebook is evaluated on the go, its like an advanced calculator.
print "Hello World"
Yay , my computer responded back with "Hello World". Lets test if it can sum my grocery bill
Oh good, you see how easy a calculator it is! I can just sum eveything at one go!
Ok now remeber I told you to break up simple tasks, well lets do the hello world with "functions" now,( this is a small digresson, if you find it complicated, skip it, you will get to know its advantages in future)
def HW(string): print string
This will require some explation so, every small task must have some "name", here "HW" is such a name, "def" stands for defination! so in natural language it is equivalent to saying "define a task HW, which takes in a sentence and prints it out!" And then call it!
Ok lets now begin with some basics of text. So firstly note that "text" and numbers are different! And computers mostly understand numbers. Firstly for everything there is a name! even for a sentence!
sen = "AbCd123"
print sen # call sen by its name and see what is there # is a comment
now a string is a collection of letters, so if say you wanted to know the 3rd letter of the sentence you would do something like:
sen # notice for 3rd character we write 2, this is because the numbering in this language starts from 0
This is the concept of an "list" , which is an collection of objects, here that is letters. In general an list is defined with brackets, like
LList = ["A","b","C","d","1","2","3"]
print LList," ",LList
one can also "slice" the array, that is tell the computer to return all letters after a particular letter, the syntax for that is simple like
sen[2:] #return everything else from 2nd position
sen[:2]#return everything else till 2nd position
sen[::2]#return everything with a gap of 2, i.e. 1st letter then 3rd letter then 5th and so on
sen[2:-1:2]#start from pos 2 and till the last, and jump by 2 steps in between
Exercise: 1. define a function which does the following
def stringP(string): # print alternate letters # print the leftout letters # example : if string = ABCDEF # output : ACE,BDF # Hints :string[0::2]
call the function with your favourite coute
Exercise 2: define a code to write the string in reverse, that is for input of Sen, we get '321dCbA'