Saturday 5 April 2014

Python programing basic examples

#!/usr/local/bin/python2.7

# printing a string
print "Hello, World!"

#defining and invoking function
def hello (what):
 text = "Hello, " + what + "!"
 print text

hello("Dave")

#---------------------------------------------
# functions returning multiple values.
str = "foo"; lst = ["abra", 2038, "cadabra"]
for char in str:
  print char

def squareandcube (x):
 return x*x, x*x*x

a, b = squareandcube(3)
print a
print b

#---------------------------------------------
# functions used as an iterable object creating its own object runtime.
def cubeseries ():
 for i in range(5):
   yield (i*i*i)

for j in cubeseries():
 print j

No comments: