Getting started with Python programming – A simple example with all basic stuff

I’ve tried out this python code in Eclipse Helios (v3.6) with PyDev plugin installed and using Python v2.7 interpreter. If you know programming in any language – C/C++/Java etc. – you’ll understand this easily.  And, you can always google if you aren’t sure of any term.

Created on Aug 24, 2011

@author: singaram subramanian

# My first day trying out python

# The pickle module implements a basic but powerful algorithm
# for 'pickling' (a.k.a. serializing, marshalling or flattening)
# nearly arbitrary Python objects
import pickle

# Write something to standard output
print "hello world!"

# Define a function
def addNum(num1, num2):
result = num1 + num2
print str(num1) + "+" + str(num2) + "=" + str(result)
return result

# Invoking the above function
print "Adding 2 and 3 gives " + str(addNum(2,3))

# while loop
a = 0
while a < 3:
a = a + 1
print a

# Conditional statements
aNum = 2
if ((aNum % 2) == 0):
print str(aNum) + " is even"
print str(aNum) + " is odd"

# List - a list of values. Each one of them is numbered,
# starting from zero - the first one is numbered zero, the second 1,
# the third 2, etc. You can remove values from the list, and add new values to the end.
languages = ['C','C++','Java', 'Python']

# Tuple - just like lists, but you can't change their values.
# The values that you give it first up, are the values that you are
# stuck with for the rest of the program
months = ('January','February','March','April','May','June',\
'July','August','September','October','November','  December')

# Dictionary - similar to what their name suggests - a dictionary. In a dictionary,
# you have an 'index' of words, and for each of them a definition.
# In python, the word is called a 'key', and the definition a 'value'.
capitalInfo = {'TamilNadu':'Chennai', 'Kerala':'Trivandrum'}
print capitalInfo['TamilNadu']

# Class
class Person:
def __init__(self, name, age): = name
self.age = age

def printInfo(self):
print "Name: " +
print "Age: " + self.age

# Creating an instance of Person
newEmp = Person("Sing","24")

# Gonna try out pickles - make sure to import pickle at the top

# pickling - let's save the languages info to the file
file = open('/home/singaram/python-files/pickletest.txt', 'w')

# now let's pickle languages list
print "done with pickling"

# close the file, and your pickling is complete

# unpickling - reading languages info from the pickled file

# open the pickled file for reading
file = open('/home/singaram/python-files/pickletest.txt', 'r')

# load languages list from the file
unpickledlist = pickle.load(file)

# close the file, just for safety

# print items in the newly constructed list - and an example for for loop too
for item in unpickledlist:
print item


  1. […] Getting started with Python programming – A simple example with all basic stuff ( […]

  2. […] Getting started with Python programming – A simple example with all basic stuff ( Share this:EmailPrintDiggStumbleUponFacebookTwitterRedditLinkedInLike this:LikeBe the first to like this post. […]

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: