def sqr_it(x):
x=x*x
return x
#Variable decleration
t=10
#Result; function calling
print sqr_it(t),' ',t
def swap(x,y):
temp=x
x=y
y=temp
return x, y
#Variable decleration
i=10
j=20
#Initial values
print "Initial values of i and j: ",
print i,' ',j
#Calling function to swap
i, j=swap(i,j)
#Result
print "Swapped values of i and j: ",
print i,' ',j
def f(i):
i=10
return i #Returning the value since the function cannot access the variables in the calling scope.
#Variable Decleration
val=1
print "Old value for val: ",val
val=f(val) #Function call
#Result
print "New value for val: ",val
def swap(i,j):
temp=i[0]
i[0]=j[0]
j[0]=temp
return i, j
#Variable decleration
i=[]
j=[]
i.append(10)
j.append(20)
#Initial values
print "Initial values of i and j: ",
print i[0],' ',j[0]
#Calling function to swap
i, j=swap(i,j)
#Result
print "Swapped values of i and j: ",
print i[0],' ',j[0]
#Variable Decleration
val = 100.0
def f():
global val
return val
#Result
print val
newval=f() #function call
print newval
val=99.1 #change val's value
print f() #print val's new value
#Function definition
def change_it(i,n):
global vals
vals[i]=n
#Variable Decleration
vals=[1.1,2.2,3.3,4.4,5.5]
print "Here are the original values: ",
for i in range(5):
print vals[i],
print
#Function call
change_it(1,5298.23)
change_it(3,-98.8)
#Result
print "Here are the changed values: ",
for i in range(5):
print vals[i],
#Variable Decleration
vals=[None]*10
error=-1
#put values into the array
def put(i,n):
global vals
if i>=0 and i<10:
vals[i]=n
else:
print "Bounds Error!"
error=n
#obtain a value from the array
def get(i):
if i>=0 and i<10:
return vals[i]
else:
print "Bounds error!"
return -1
#put values into the array
put(0,10)
put(1,20)
put(9,30)
#Result
print get(0),' ',get(1),' ',get(9),
#now, intentionally generate an errer
put(12,1)
#Variable Decleration
i=[]
j=i #independent reference
j.append(10) #Here i and j are just references to [10]
print j[0]," ",i[0]
k=121
i[0]=k #copies k's value into j[0]
#Result
print j[0]
def f(i,j=None):
if j==None:
if isinstance(i,int): #for 1st function
print "In f(int), i is ",i
else: #for 3rd function
print "In f(double), k is ",i
else: #for 2nd arguments
print "In f(int,int), i is ",i,", j is ",j
#calling function
f(10)
f(10,20)
f(12.23)
def myabs(i):
if isinstance(i,int): #first instance
print "Using integer myabs(): ",
if i<0:
return -i
else:
return i
elif isinstance(i,float): #second instance
print "Using double myabs(): ",
if(i<0.0):
return -i
else:
return i
elif isinstance(i,long): #third instance
print "Using long myabs(): ",
if i<0:
return -i
else:
return i
#Result; calling the function
print myabs(-10)
print myabs(-11.0)
print myabs(-9L)
import os
def clrscr(size=25):
while(size):
print ""
size-=1
for i in range(30):
print i
clrscr() #clears 25 lines
for i in range(30):
print i
clrscr(10) #clears 10 lines
#Variable Decleration
str1 = "This is a test"
str2 = "0123456789"
#function for concatenation
def mystrcat(s1,s2,l=-1):
if l==-1:
l=len(str2)
s2=s2[:l] #truncates s2
s1=s1+s2 #concatenates the 2 strings
return s1
str1=mystrcat(str1,str2,5) #concatenate 5 chars
print str1
str1 = "this is a test" #reset str1
str1=mystrcat(str1, str2) #concatenate entire string
print str1
def myfunc(i):
return i
print myfunc(10.1),
print myfunc(10)
from ctypes import *
def myfunc(ch):
if isinstance(ch,c_int):
return chr(ch.value+1)
elif isinstance(ch,c_uint):
return chr(ch.value-1)
print myfunc(c_int(ord('c'))),
print myfunc(c_uint(88))
def myfunc(i,j=1):
if j==1:
return i*j
else:
return i
print myfunc(4,5),
print myfunc(10)