Archive | June, 2011

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

Image

Which Python book to follow?

11 Jun

If you’ve read my previous post Why learn Python? and you are really impressed to see what python can do for you,you might be wanting to try your hands on python.For this you’ll definitely need a good book of python language which can guide you during the times when you’re stuck somewhere in between.Believe me,a good book can be a great companion and an asset to the programmer.So,I’m gonna review 3 Python books for you to choose from.

Book #1 : Head First Python

hfp

Head First Python Cover

If you haven’t programmed before or you don’t want to put too much effort in learning Python,then this book is for you. But it is to be noted that this book is very lengthy and you’ll find many things in the book are redundant.Also,If you are an experienced programmer in Python then this book is not for you.The area where this book scores maximum is that it teaches the user how to build realistic applications using modern technologies rather than explaining Python’s Standard API(Application Programming Interface).

If you use this book,you will be able to:-

  • Write CGI scripts using Python
  • Create apps with Google App Engine which can be used with different social networks
  • Create Android apps
  • Database Management using Python
Book #2 : Programming with Python 3
pwp3
Programming with Python 3
Cover
Huff!Believe me this book is python newbie’s nightmare.If you are a newbie and don’t have any knowledge about python then this book is not for you.On the other hand if you’re an experienced python programmer who wants to increase his/her command over the language,then this book can prove beneficial to you.To tell the truth,I picked up this book in the first place for learning python but the book didn’t really help me.Besides regular stuffs this book will provide you some great topics.
Salient features of this book are as follows:-
  • The chapter on Regular Expressions is really nice
  • The book tells you about Networking using Python
  • The book teaches advance techniques like extracting meta data from files
  • The book teaches about debugging,testing and profiling
Book #3 : Python for Unix and Linux System Administrators
pfual
Python for Unix and Linux 
System Administrators Cover
To be frank with you,I’ve not read this book yet but I’ve had a look at it.So I can at least tell you what this book contains.As the name say it all,this book is exclusively for users who use Linux and Unix.
This book has chapters on following topic:-
  • IPython
  • SNMP
  • OS Soup
  • Package Management
  • Using easy_install
  • Processes and Concurrency
Attention : It is to be noted that all of the above books don’t have good chapters on “Building GUIs”.So,if you want to write GUI applications in Python then you have to use other libraries like PyGTK,PyQT etc.Just keep in touch,I’ll be reviewing GUI toolkits for Python soon

What’s Ubuntu?Is there any need to know Ubuntu?

10 Jun

If you have been using Linux for past few days or months,you might have heard the name of Ubuntu.If you come from Windows or Mac background,i’d like to tell you that Ubuntu is the most popular Linux distribution(flavour) on planet Earth.
You should at least give Ubuntu a try in order to know what this OS(Operating System) has to offer.
If you are in love with your Windows or Mac OS X environment,I can still give you enough reasons to try Ubuntu out.

So let me try to convince you to use Ubuntu:)

Reason #1 : You don’t like viruses and malwares,do you?
Solution : Try Ubuntu,it’s virus free

Reason #2 : You don’t like to wait for your OS to start so that you can update your Facebook Status.
Solution : Try Ubuntu,it boots in less than 20 seconds on new systems.
Now,If you argue that your windows system already boots in 15 seconds,I’d say that Ubuntu will boot in 10 seconds only,on your system;)

Reason #3 : You use a lot of pirated softwares and don’t want to pay for softwares in future too.
Solution : Then why not try out the OS which has all the softwares needed for day-to-day computing and all of them are free.
You could ask,”Ok,you say that all of the softwares are free but are they updated too?”.I’d say,”Yes,they are and all of the updates are free too:)”.

Reason #4 : You can still say that your CD/DVD drive is damaged or out of order or may be you own a netbook which doesn’t even has CD/DVD drive,so you are unable to try it out.
Solution : To tell you the truth,Ubuntu can boot from USB drive too.

So,you can try out Ubuntu without any fear.If I was successful in convincing you to use Ubuntu then why not download Ubuntu for your system.Here is the link: Download Ubuntu

In the next post I’ll be telling you about the ways to run Ubuntu on your System.

Why learn Python?

8 Jun

Whether you are a programmer or not,knowing any computer language is not a loss at all.

A good programming should has following qualities:-
-highly reliable
-fast enough
-easy to learn

Python has it all.It is designed to be easy and fast.Also the programs written in Python can be easily understood by human beings.

If you learn this language,you’d be able to:-
-write programs which can control your operating systems,it is the core programming language on Ubuntu.
-write applications for Android platform.
-write CGI scripts.
-write utility programs like Image Viewer.
-develop applications for social platforms like Facebook.
-create database applications without even installing any dbms as Python has in-built SQLite database system.
-run your own webserver and show off your contents to the world

Also there is nothing which will stop you from using Python.There are python-bindings(libraries) for everything.For example:-
-want to develop high quality GUIs?Use Pyclutter
-want to embed a web browser into your app?Use gtkmozembed
-want to process XML?Use lxml

I think all of the above would be enough to convince you to use Python for your programming purposes.

Facebook Indicator

7 Jun

I’ve developed a simple python script which can show your fb notifications using Ubuntu’s in-built OSD system.

You can check it out at: https://github.com/raynesax/findicate

I’ve joined WordPress

6 Jun

They say WordPress is best.Now let me see that how efficiently this site can serve my blogging needs.