Over a million developers have joined DZone.

Getting Started with Python – A simple example with all basic stuff

DZone's Guide to

Getting Started with Python – A simple example with all basic stuff

· Web Dev Zone ·
Free Resource

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

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):
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")

# 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

Source:  http://singztechmusings.in/getting-started-with-python-programming-a-simple-example-with-all-basic-stuff/

Take a look at an Indigo.Design sample application to learn more about how apps are created with design to code software.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}