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 []:

Advertisements

Any thoughts?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s