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"
else:
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']
languages.append('Groovy')

# 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):
self.name = name
self.age = age

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

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

# 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
pickle.dump(languages,file)
print "done with pickling"

# close the file, and your pickling is complete
file.close()

# 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
file.close()

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

2 comments

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

  2. […] Getting started with Python programming – A simple example with all basic stuff (singztechmusings.wordpress.com) 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:

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

Follow

Get every new post delivered to your Inbox.

Join 27 other followers

%d bloggers like this: