Monday, 18 July 2011

Notes on Python

Its been a long time since I've started learning Python and I've gotten as far as functions but there is a lot for me to cover. I originally started off reading the tutorial for Python 2.7 but later on people on a certain forum started to recommend 3.2 - what a bother! but I moved on to reading 3.2. There weren't many drastic changes in 3.2 at-least as far as I've read, I just had to keep in mind that "print" is no longer a statement but a function and that all the strings can be used as unicode strings by default apart from that, there was nothing I had to worry about.
                                         I've noticed Python was far more faster when executing the code, I read that it was because it was interpreting instead of compiling, this made it fast and efficient. Apart from the interpreter available from the official site, there are several other 3rd party IDEs (Integrated Development Environment) - the most famous of the lot being ActiveState.
                                    (do note that comments are initialized with the "#" keyword) What differed Python from "C" was that it was a simplified version of "C" or "C++" or "Java" - I was surprised to see to what level it was simplified. For example, if I wanted to declare an integer - all  I was supposed to do was enter a literal and assign an integer value.
                         >>> a=1
       And voila! it will be declared an integer and defined with the number "1". The same applies when I want to define or declare a string, float and character.
But this is just the beginning - I can efficiently use Python's interpreter as a calculator.
   >>> 1+1
                      Printing was as simple as
           print("hello world!")
                    There you go! you are looking at the easiest hello world program on the planet!
 Strings are rather tricky, Unlike what you see in "C", they are immutable - meaning, you cannot add an individual character to a string. In order to access elements in a string, you need to be familiar with slicing. (Slicing is a long topic to discuss and I do not want go through the entire matter so I'm trying keep it as brief and understandable as possible) If you want to access individual elements or a particular set of elements in a string, splicing comes in handy. It is denoted by slice indices ":".

 >>> k='ahem'  #defining a letter k with the string "ahem"
>>> k
>>> k[1:]         #the integer at the left of the indices shows the size of the integer to be cut off 
>>> k[:3]         # the integer at the right of the indices shows the size of the integer to be considered
>>> k[4:]         # when the size of the string is specified on the right side of the indices the result is an empty sting
                 When you want to access individual elements of a string - it is the same as in "C".

>>> k='ahem'
>>> k[0]
                        Finally, we come across the lists - Lists are fairly similar to "Structures" in "C" except that they are far less complicated. List can store elements of various data types in it -

 >>> l=['ahem',82,'what',1.2]
>>> l
['ahem', 82, 'what', 1.2]
                        Unlike strings, they are mutable - meaning, individual elements can be added to the list in place of another element.
>>> l[0]='gaga'
>>> l
['gaga', 82, 'what', 1.2]
                                     I hope this serves as an informal introduction to Python for all of you. There is one other topic - "Unicode" which deserves an update dedicated entirely to it.

Happy Coding!                

No comments:

Post a Comment