Installing python and your first program

Time has come to teach your computer to speak “Python”

Firstly, the choice of Operating system:

So what do you use?

Windows, Linux?

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

cd ../Downloads

chmod +x “***.sh”

sudo ./***.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)

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.

In [2]:
print "Hello World"
Hello World

Yay , my computer responded back with "Hello World". Lets test if it can sum my grocery bill

In [3]:
print 212+190+40+15+64
521

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)

In [4]:
def HW(string):
    print string
In [5]:
HW("Hello World")
Hello World

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!

In [6]:
sen = "AbCd123"
In [7]:
print sen # call sen by its name and see what is there # is a comment
AbCd123

  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:
In [8]:
sen[2] # notice for 3rd character we write 2, this is because the numbering in this language starts from 0
Out[8]:
'C'

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

In [9]:
LList = ["A","b","C","d","1","2","3"]
In [12]:
print LList[2]," ",LList[0]
C   A

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

In [14]:
sen[2:] #return everything else from 2nd position
Out[14]:
'Cd123'
In [16]:
sen[:2]#return everything else till 2nd position
Out[16]:
'Ab'
In [18]:
sen[::2]#return everything with a gap of 2, i.e. 1st letter then 3rd letter then 5th and so on
Out[18]:
'AC13'

In general,

In [20]:
sen[2:-1:2]#start from pos 2 and till the last, and jump by 2 steps in between
Out[20]:
'C1'

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'

In []:

Learning to speak to computers!

Good to be back! Forgive me of my sins for I was occupied with interests of my own. 🙂

So apparently my girlfriend is now studying Linguistics and programming can become handy for large dataset analysis and automating some stuffs, so she (who never programmed anything ever) asked me to teach “programming”. I thought for a while and thought it would be fun to teach someone programming from scratch, think about it, it is a “language” too, albeit a “written” one, which is quite similar to the set of instructions that we are accustomed to process.

Someone writes to you,

Find the vowels in the sentence:   “Programming is simple conversation with the machine!”

The word “Find” is a trigger your brain processes to “narrow” down your “attention” to your prior knowledge of vowels, namely “AEIOU”. You can do the same thing to a computer, using its language! Now as humans have developed different language, you can talk to computer using various “languages” which it can understand.

BTW computers mother toungue is just True(1) and Flase(0).

I have been a great fan of the language “Python”, because of its simplicity and user friendliness, so I will stick to that. The main aim of these set of blog tutorials will be 2 fold. Recently I have been interested in NLP, which is Natural Language Processing, and its applications, these blog posts will apart from doing the basics of programming, move on to some basic language processing tools, which will be my own study journal. I might think to introduce some maths later on in this course for understanding some statistical properties of language.

So are you ready to talk to computer and explore the fascinating world?

Lets begin with the analogies, so that it isnt too “unfamiliar”. Your friend who speaks only German says to you :

Schreib “back”

Now I cant understand what Schreib means! because I dont know the language! Similarly a computer would not know any language, but you can teach him by installing a “software” ! I will write about Installing python in your computer in the next post, but till then hang on. So the basic thing to know speak to computer is to know its language. Specifically forget the adjectives there are only Names(nouns) and Commands(verbs) :p

BTW did you know ?

Guido van Rossum, the creator of the Python language, named the language after the BBC show “Monty Python’s Flying Circus”. He doesn’t particularly like snakes that kill animals for food by winding their long bodies around them and crushing them.

Now it is important that you learn a language by practicing it! So it applies same here, the next set of tutorials will be on my view about how one should see programming, well since i have said that, I believe that we are fast evolving into a world where humans and technology will be inseparable, and programming will be an intrinsic part of everyone’s life, there will be pet cyborgs and humanoids to make your life easier, so you better learn how to control them.

Ok, enough digression, so lets get back to our second example, Schreib in german means write, to do the same thing to a computer you can just say

print “HelloWorld”

and as you might have imagined, it does exactly as it is told 🙂 We will get to these details later. To have a formal understanding on how programming works, you must be really clear on what you want to do, and how exactly you want to do it!

This is the first and last thing one must worry about. Like if I said, “you Good  are ” (well its a odd fact that you could still make out the meaning, but believe me “computers” take things quite “Literally”), this doesnt make sense because  what exactly I want to ask is “Are you Good?” So structure is important in program too. Again in the first example, how would you count all vowels in a sentence?

1. recall what vowels are! {A,E,I,O,U} , wow !

2. Read the sentence “Programming is simple conversation with the machine!” letter by letter and note down vowels from it!

3.Naievely, you can also take a copy and write 1+1+1….. as soon as you encounter a vowel in the sentence.

4. To be sane enough, start reading from left to right!

Simple isnt it? You see how I broke a “Task” to smaller steps of what needs to be done. This is an important concept.

Now first time my teacher would teach me how to find vowels like this, but the next time she says me to find vowels, its a “Task” we should already know. This is the concept of a function. It is always a good idea to learn to define new short “verbs” for actions which you do often, say you work in a brick Kline, and the owner asks you “to collect mud, shape it and then bake it”  , would it be easier for him to “invent” a single word  for the whole process say “comisab” or something! and everyone just understands what the owner is saying.

So I will end this topic by summarizing 2 things what i think is important for writing a program:

1. Manually first do what you want to instruct to your computer in pen and paper. Its like a translation, first your need to know your mother language!

Did you Know? Programme comes from word Pro meaning before, and graphe meaning write, which probably sums up,what you need to do before writing your first code!Think about it before writing!

2.Try to break the process to as simple as possible and group them as small “tasks”.

Next Tutorial:

Installing python in Linux machine/Windows. Editors