### Example 8.1, Page Number: 158

In [2]:
def sqr_it(x):
x=x*x
return x

#Variable decleration
t=10

#Result; function calling
print sqr_it(t),' ',t

100   10


### Example 8.2, Page Number: 159

In [1]:
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

Initial values of i and j:  10   20
Swapped values of i and j:  20   10


### Example 8.3, Page Number: 161

In [4]:
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

Old value for val:  1
New value for val:  10


### Example 8.4, Page Number: 162

In [2]:
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]

Initial values of i and j:  10   20
Swapped values of i and j:  20   10


### Example 8.5, Page Number: 164

In [4]:
#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

100.0
100.0
99.1


### Example 8.6, Page Number: 166

In [5]:
#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],

Here are the original values:  1.1 2.2 3.3 4.4 5.5
Here are the changed values:  1.1 5298.23 3.3 -98.8 5.5


### Example 8.7, Page Number: 167

In [6]:
#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)

10   20   30 Bounds Error!


### Example 8.8, Page Number: 169

In [7]:
#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]

10   10
121


### Example 8.9, Page Number: 170

In [6]:
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)

In f(int), i is  10
In f(int,int), i is  10 , j is  20
In f(double), k is  12.23


### Example 8.10, Page Number: 171

In [8]:

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)

Using integer myabs():  10
Using double myabs():  11.0
Using long myabs():  9


### Example 8.11, Page Number: 174

In [8]:
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


0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29



### Example 8.12, Page Number: 176

In [9]:
#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

This is a test01234
this is a test0123456789


### Example 8.13, Page Number: 177

In [2]:
def myfunc(i):
return i

print myfunc(10.1),
print myfunc(10)

10.1 10


### Example 8.14, Page Number: 178

In [5]:
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))

 d W


### Example 8.15, Page Number: 179

In [6]:
def myfunc(i,j=1):
if j==1:
return i*j
else:
return i

print myfunc(4,5),
print myfunc(10)

4 10

In [ ]: