Chapter 11: Pointers

Example 1, Page Number : PTR-5

In [1]:
a = int(raw_input("\nEnter two integers : "))
b = int(raw_input(""))

xptr = id(a)
yptr = id(b)

#There is no pointer concept in python
s = a + b

print "\nSum = ",s
Enter two integers : 5
3

Sum =  8

Example 2, Page Number : PTR-6

In [2]:
a = int(raw_input("\nEnter two integers : "))
b = int(raw_input(""))

xptr = id(a)
yptr = id(b)

#There is no pointer concept in python
if a > b:
    big = a
else:
    big = b
    
print "\nBiggest number is ",big
Enter two integers : 15
25

Biggest number is  25

Example 3, Page Number : PTR-7

In [3]:
def add10(x,y):
    #There is no pointer concept in python
    x = x + 10
    y = y + 10
    print "\n\nInside the function a = ",x," b = ",y
    return x,y
    
a = 25
b = 10

print "\nBefore function call a = ",a," b = ",b
a,b = add10(a,b)
print "\nAfter function call a = ",a," b = ",b
Before function call a =  25  b =  10


Inside the function a =  35  b =  20

After function call a =  35  b =  20

Example 4, Page Number : PTR-11

In [5]:
def swap(x,y):
    #There is no pointer concept in python
    temp = x
    x = y
    y = temp
    return x,y

a = int(raw_input("\nEnter two integers : "))
b = int(raw_input(""))

print "\nBefore function call a = ",a," b = ",b

a,b = swap(a,b)

print "\n\nAfter function call, values are exchanged as \n a = ",a," b = ",b
Enter two integers : 25
500

Before function call a =  25  b =  500


After function call, values are exchanged as 
 a =  500  b =  25

Example 5, Page Number : PTR-14

In [1]:
import sys

x = [0.0 for i in range(0,10)]
n = int(raw_input("\nHow many values ? "))

print "\nEnter all values in the list\n"

for i in range(0,n):
    x[i] = float(raw_input(""))

sum1 = 0.0
#Since sum is a predefined function in python, sum1 is used
for i in range(0,n):
    sum1 = sum1 + x[i]
    #There is no pointer concept in python
    
mean = sum1/n

sys.stdout.write("\nArithmetic mean = %0.3f"%(mean))
How many values ? 4

Enter all values in the list

3.48
3.54
3.26
2.98

Arithmetic mean = 3.315

Example 6, Page Number : PTR-19

In [3]:
import sys

xp = [0 for i in range(0,10)]
n = int(raw_input("\nHow many values ? "))

#There is no pointer concept in python
print "\nEnter all values\n"

for i in range(0,n):
    xp[i] = int(raw_input(""))

print "\nThe reversed list is\n"
for i in range(n-1,0-1,-1):
    sys.stdout.write("%6d"%(xp[i]))
How many values ? 6

Enter all values

11
12
13
14
15
16

The reversed list is

    16    15    14    13    12    11

Example 7, Page Number : PTR-21

In [5]:
import sys

a = [[0 for i in range(0,10)]for i in range(0,10)]
b = [[0 for i in range(0,10)]for i in range(0,10)]

c = [[0 for i in range(0,5)]for i in range(0,5)]
m = int(raw_input("\nFor A matrix \nHow many rows and columns ? "))
n = int(raw_input(""))

print "\nEnter A matrix values\n"
for i in range(0,m):
    for j in range(0,n):
        a[i][j] = int(raw_input(""))
        
n = int(raw_input("\nFor B matrix\nHow many rows and columns ? "))
l = int(raw_input(""))

for i in range(0,n):
    for j in range(0,l):
        b[i][j] = int(raw_input(""))

for i in range(0,m):
    for j in range(0,l):
        c[i][j] = 0
        for k in range(0,l):
            c[i][j] = c[i][j] + (a[i][k] * b[k][j])
            
print "\nResultant matrix is \n"
for i in range(0,m):
    for j in range(0,l):
        sys.stdout.write("%6d"%(c[i][j]))
    sys.stdout.write("\n")
For A matrix 
How many rows and columns ? 3
3

Enter A matrix values

3
2
1
-2
0
4
3
2
-1

For B matrix
How many rows and columns ? 3
3
6
4
-1
2
1
2
4
-5
4

Resultant matrix is 

    26     9     5
     4   -28    18
    18    19    -3

Example 8, Page Number : PTR-24

In [32]:
count = 0

st = raw_input("\nEnter a string : ")
sptr = st
i = 0

while i < len(st):
    if st[i].upper() == 'A' or st[i].upper() == 'E' or st[i].upper() == 'I' or st[i].upper() == 'O' or st[i].upper() == 'U':
        count = count + 1
    i = i + 1
        
print "\nNumber of vowels = ",count
Enter a string : GOOD LUCK to all

Number of vowels =  5

Example 9, Page Number : PTR-25

In [2]:
def sort_names(sn,n):
    for i in range(0,n):
        for j in range(i+1,n):
            if sn[i] > sn[j]:
                temp = sn[i]
                sn[i] = sn[j]
                sn[j] = temp
                

n = int(raw_input("\nHow many names ?"))

snames = ["" for i in range(0,10)]

print "\nEnter all names \n"
for i in range(0,n):
    snames[i] = raw_input("")
    
sort_names(snames,n)

print "\nThe name list in alphabetical order"
for i in range(0,n):
    print snames[i]
How many names ?4

Enter all names 

DEEPAK
SHERIN
SONIKA
ARUN

The name list in alphabetical order
ARUN
DEEPAK
SHERIN
SONIKA

Example 10, Page Number : PTR-27

In [4]:
print "\nEnter a string : "
st = raw_input("")
rst = ['' for i in range(0,80)]

rst = st[::-1]
    
if st == rst:
    print st," is a palindrome string"
else:
    print st," is not a palindrome string"
Enter a string : 
LIRIL
LIRIL  is a palindrome string

Example 11, Page Number : PTR-28

In [5]:
print "\nEnter a string : "
st = raw_input("")

#There is no pointer concept in python
city = st

print "\nThe copied string : ",city
Enter a string : 
NEW DELHI

The copied string :  NEW DELHI

Example 12, Page Number : PTR-30

In [9]:
#class is used instead of structure in python

class student():
        rno = 0
        sname = ''
        tot = 0

sptr = student()
sptr.rno = int(raw_input("\nEnter register No.:"))
sptr.sname = raw_input("\nEnter student name : ")
sptr.tot = int(raw_input("\nEnter total marks : "))

print "\n\tDetails entered are \n"
print "\nReg.No,  : ",sptr.rno
print "\nName     : ",sptr.sname
print "\nTotal    : ",sptr.tot
Enter register No.:101

Enter student name : Ashwin

Enter total marks : 89

	Details entered are 


Reg.No,  :  101

Name     :  Ashwin

Total    :  89

Example 13, Page Number : PTR-32

In [10]:
#There is no pointer concept in python
def add(x,y):
    return x+y

def multiply(x,y):
    return x*y

a = int(raw_input("\nEnter two integers : "))
b = int(raw_input(""))

sum1 = add(a,b)

print "Sum a + b = ",sum1

product = multiply(a,b)
print "\nProduct a * b = ",product
Enter two integers : 5
3
Sum a + b =  8

Product a * b =  15
In [ ]: