Tag Archives: tutorial

The Impatient Developer’s Guide to Python

27 Jun

If you don’t want to go through those 200 pages for just learning the basics of Python but still want to learn Python,you’re at the right place.In this post I’ll be telling you about how to code in Python.I already assume that you at least know the basics of computer programming or have programmed before.So let’s dive in.

First of all,you should understand that Python doesn’t have ‘{‘ and ‘}’ to represent start and end respectively of any structural block so we use indentations to tell the python’s interpreter about start and end of any structural block like functions,if else statements,loops etc.To make things clear i’m gonna show you the example

Example 1: C’s syntax:-  

void sum(){

<programs body>

}

while in Python the above function will be written as follows

Python’s syntax:-

def sum():

<indentation><program body>

In the above code “<indentation>” denotes that you’ve to indent the program’s body,you don’t have to type the word “<indentation>” though,just press the tab key to indent the code.Now we can proceed further.I’d like to tell you about python’s shell.It’s an interactive place where you can write your commands and have them executed.The symbol “>>>” denotes the Python shell’s prompt.It’s “interactive” because it shows the result immediately after executing the instruction.If you type 2+3 at the “>>>” prompt then you’ll get 5 as output.

>>>2+3

5

>>>(2+5)/7

7

Diving In: In order to practice Python,you should start Python’s Interactive Shell.If you’re using Ubuntu or Mac OS X then you must be having Python installed already.If you’re using Windows you can easily get the installer from here.On Ubuntu simply type “python” in the Terminal without quotes.You will get something like this

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Showing output,printing to the console: For this use “print” command.

Example:

>>>print(“Hello World”)

Hello World

Defining Variables in Python: For defining simple variables to store integer,float and string like values you can write

>>>var=1

>>>var=”Rajat”

>>>var=1.0

There’s no need of doing something like “int var” or “float var” like other programming languages.

Introducing Dictionary,Lists and Tuples: These are unique data types and are very powerful.I’m gonna show you how you can use them,one by one.

Dictionary: You can define a dictionary type variable as follows:

>>>dictvar={“name”:”rajat”,”age”:23}

Now you can visualize it as one item list with associated values.In the above case if “name” is an item then “rajat” is it’s value.Now if you want to access value of any item,you can do it as follows

>>>dictvar[“name”]

‘rajat’

To add new entry,type:

>>>dictvar[“gender”]=”female”

List: You can define a list type variable in the following way:

>>>listvar=[1,2,”rajat”,3,”saxena”]

Now to access the elements from the above list you can do something like this

>>>listvar[0]

1

To add new element to the above list,type

>>>listvar.append(“newvalue”)

Tuple: You can define a tuple type variable as follows:

>>>t=(“1″,”2″,”rajat”)

Defining Functions: Creating new functions in Python is very easy.Following is the syntax of a function in Python

>>>def <function name>(<arguments list>):

…            <program’s body>

Here is the small example of a function:

>>>def sayhello():

…           print(“Hello”)

Now calling any function is very easy too:

>>>sayhello()

Hello

 Defining Class: As Python is an object oriented high level programming language you can obviously create classes in Python.Here’s how you can do this:

>>>class <classname>(<SuperClass,if any>):

…          def  __init__(self):

…                  <init’s body>

…          <rest to the body of this class>

Example of a class:

>>>class myclass:

…              def __init__(self):

…                     print(“Hello”)

Now to make the instance of above class,type:

>>>m=myclass()

Hello

Looping: Easiest way to doing this is as follows

>>>for i in range(5):

…          print(str(i))

0

1

2

3

4

In the above example you must have noticed two new things “str” and “range”,so here’s the explanation.The in built function str() converts integer value to string and the function range() generates a list containing the integer elements from 0 to argument-1 where argument is what we pass to the range() function.Have a look at it

>>>range(8)

[0,1,2,3,4,5,6,7]

I think if you put the above Python skills under your belt,then you can certainly make simple programs using Python.Under any difficulty feel free to write to me,I’ll try resolve your issue with Python

Advertisements