a = 100
b = 200
c = 300
print "Address: %x contains value: %i" %(a, a)
print "Address: %x contains value: %i" %(b, b)
print "Address: %x contains value: %i" %(c, c)
i = 7
f = 10.5
c = 'A'
print "%d is stored at address %u" %(i, id(i))
print "%f is stored at address %u" %(f, id(f))
print "%c is stored at address %u" %(c, id(c))
var1 = 10
var2 = 20
iptr = id(var1)
print "Address and contents of var1 is: %d & %d" %(iptr, var1)
iptr = id(var2)
print "Address and contents of var1 is: %d & %d" %(iptr, var2)
#note: we have used var1 instead of iptr because there is no point of pointer in python. Hence, instead of *iptr
#we have to refer to the same variable
var1 = 0
iptr = id(var1)
var1 = 25
var1 += 10
print "Variable var1 contains %i " %var1
var2 = var1
print "Variable var2 contains: %i" %var2
iptr = id(var2)
var2 += 20
print "Variable var2 now contains: %i" %var2
a = 5
b = 10
ptr = id(a)
print "Initial values of a & b"
print "a = %d" %a
print "b = %d" %b
b = a
print "Changed values of a & b"
print "a = %d" %a
print "b = %d" %b
print "Address of variable is ", id(a)
print "Address of variable is ", id(b)
print "Address assigned to pointer ptr is ", id(ptr)
print "Value pointer ptr access is ", a
def swap(d1, d2):
return d2, d1
print "Enter 2 real numbers:"
data1 = float(raw_input("num1: "))
data2 = float(raw_input("num2: "))
print "Data1 contains: ", data1
print "Data2 contains: ", data2
data1, data2 = swap(data1, data2)
print "After swapping....."
print "Data1 contains: ", data1
print "Data2 contains: ", data2
i1 = 100
f1 = 200.5
vptr = id(i1)
print "i1 contains ", i1
vptr = id(f1)
print "f1 contians ", f1
#the way it is given in the textbook is not possible as there is no concept of pointers in Python
a = []
print "Size of array ?"
n = int(raw_input())
print "Array elements ?"
for i in range(n):
a.append(int(raw_input()))
small = min(a)
print "The smallest element is ", small
a = 5
b = 10
ptr1 = id(a)
ptr2 = id(b)
c = a + b
d = a/b*7
print "Addresses of variables 'a' and 'b': "
print "a: ", ptr1
print "b: ", ptr2
print "Addresses of pointers 'ptr1' and 'ptr2' hold: "
print "ptr1: ", ptr1
print "ptr2: ", ptr2
print "values of 'a', 'b', 'c', 'd': "
print "a = %2d b = %2d c = %2d d = %2d" %(a,b,c,d)
print "Values accessed by pointers 'ptr1' and 'ptr2': "
print "ptr1: ", ptr1
print "ptr2: ", ptr2
# not possible in Python
data = 0
iptr = id(data)
ptriptr = id(iptr)
data = 100
print "Variable 'data' contains: ", data
ptriptr = 200
print "Variable 'data' contains: ", data
#value of data won't change as there is no pointer
data = 300
print "ptriptr is pointing to: ", data
iarray = [1, 2, 3, 4, 5]
for i in range(5):
print id(iarray[i]), iarray[i]
def sort_it(b):
return sorted(b)
a = []
print "Eneter the number of elements in the array (lessthan 21): "
n = int(raw_input())
print "Enter the elements: "
for i in range(n):
a.append(int(raw_input()))
arr = sort_it(a)
print "The sorted array: "
for ele in arr:
print ele,
def SortByPtrExchange(person):
return sorted(person)
choice = 'y'
person = []
while choice == 'y':
print "Enter Name: "
person.append(raw_input())
print "Enter another ? (y/n)"
choice = raw_input()
print "Unsorted list: "
for per in person:
print per
person = SortByPtrExchange(person)
print "Sorted List: "
for per in person:
print per
def Add10(studentmarks):
studentmarks += 10
return studentmarks
Marks = 25
print "Value of Marks = %d" %Marks
Marks = Add10(Marks)
print "Value of Marks after execution = %d" %Marks
Max = 5
def Add10(studentmarks):
global Max
for i in range(Max):
studentmarks[i] += 10
return studentmarks
Marks = [25, 43, 70, 80, 76]
Marks = Add10(Marks)
for j in range(Max):
print "Marks[%d] = %d" %(j, Marks[j])
def exchange(x, y):
return y, x
print "Program swaps values of the variables 'a' and 'b'"
print "Values of a & b: "
a = int(raw_input("a: "))
b = int(raw_input("b: "))
print "a: %5d \t b = %5d" %(a, b)
x, y = exchange(a, b)
print "After swapping: "
print "a = %5d \t b = %5d" %(x, y)
#not possible in Python. Hence, skipping
arr = [1, 2, 3]
ptr = id(arr)
for i in range(3):
print "%d %d" %(id(i), arr[i])
arr = [1, 2, 3, 4, 5]
for ele in arr[::-1]:
print ele,
arr = [-1, -2, -3, -4, -5]
p = arr
arr = arr[::-1]
p = p[::-1]
for i in range(len(arr)):
print arr[i], "\t", p[i]
ia = [2, 5, 9]
ptr = ia
for ele in ptr:
print ele,
def FindBig(x, y):
return x if x > y else y
print "Enter two integers: "
a = int(raw_input("int1: "))
b = int(raw_input("int2: "))
big = FindBig(a, b)
print "Variable name \t address of the variable \t value"
print "a \t\t %d \t\t\t %d"%(id(a), a)
print "The bigger of the two integers"
print "big \t\t %d \t\t\t %d" %(id(big), big)
print "The value as botained form pointer: ", big
city = "Bangalore"
i = 0
print "Address \t\t\t\t Contents"
for i in range(len(city)):
print id(city[i]), "\t\t\t ", city[i], city[i], city[i]
chArr = "Pointer and Strings"
chPtr = chArr
print "Contents pointed by the pointer chPtr is: ", chPtr
def usrstrlen(ptr):
ln = 0
for ele in ptr:
ln += 1
return ln
s1 = "Good"
print "Length of string using standard library function",
print len(s1)
length = usrstrlen(s1)
print "Length of string using user defined function ", length
print "Enger the string to find it's length"
string = raw_input()
print "String is: ", string
print "It's length is : ", len(string)
def usrstrcpy(s):
s2 = []
for ele in s:
s2.append(ele)
s2 = ''.join(s2)
return s2
s1 = "Good"
s2 = s1
print "Copied string using normal assignment is ", s2
s2 = usrstrcpy(s1)
print "Copied string using user defined function is ", s2
def usrstrcat(s2, s1):
return s1 + s2
s1 = "Good"
s2 = " Morning"
s3 = s1+s2
print "Concatenation using normal Python addition",
print s3
s1 = usrstrcat(s2, s1)
print "concatenation using user defined function is ", s1
#only user defined way is possible in Python. Normal way would return either True or False only
s1 = "Good"
s2 = " Morning"
if s1 < s2:
r = -1
elif s1 > s2:
r = 1
else:
r = 0
print "Comparison of strings using user defined way is ", r
pet = ["lion", "cat"]
for i in range(len(pet)):
print id(pet[i]), pet[i]
st = "hello"
for i in range(len(st)):
print st[i], "\t", st[i], "\t", st[i], "\t", st[i]
st = "Good Morning"
print st[3:]
#not possible in Python. Skipping
string = raw_input("Enter a string: ")
c = raw_input("Enter character to be searched: ")
try:
i = list(string).index(c)
print "%c was found in position %d" %(c, i)
except:
pass
string1 = "0123456789"
string2 = "A47DCF8"
c = 0
for ele in string2:
if ele in string1:
index = string1.index(ele)
break
string1 = "0123456789"
string2 = "A32DC014"
c = 0
for ele in string1:
for ele1 in string2:
if ele == ele1:
c += 1
print c
import sys
print "Input two Strings: "
str1 = raw_input("Str1: ")
str2 = raw_input("Str2: ")
for ele in str1:
if ele in str2:
print "Character found: ", ele
sys.exit()
print "Character not found"
import sys
print "Input two Strings: "
str1 = raw_input("Str1: ")
str2 = raw_input("Str2: ")
if str2 in str1:
str1.index(str2)
print "String 2 is found at position 6 in string1"
my_str = raw_input("Input a string, parts separated by , or ;")
print "The tokens found were: "
token = ''
for i in range(len(my_str)):
if my_str[i] == ',' or my_str[i] == ';':
if len(token) > 0:
print token
token = ''
else:
token = token + my_str[i]
print token
def func1(i, f):
print i, f
def func2(s):
print s
i = 5
f = 5.375
s = "string"
func1(i, f)
func2(s)
def fact(m):
if m == 1:
return 1
else:
return m*fact(m-1)
print "Enter the integer whose factorial is to be found: ",
n = int(raw_input())
ans = fact(n)
print "Factorial of ", n, " is ", ans
def large(b):
return max(b)
def small(b):
return min(b)
print "Enter the number of integers: ",
n = int(raw_input())
print "Enter the set of integers: "
a = []
for i in range(n):
a.append(int(raw_input()))
print "The largest integer is: ", large(a)
print "The smallest integer is: ", small(a)
MAX = 20
def print_array(a):
print "The elements of the array are: "
for ele in a:
print ele,
print "Enter the number of elements: "
n = int(raw_input())
print "Enter the elements: "
a = []
for i in range(n):
a.append(int(raw_input()))
if n < 0 or n > MAX:
print "Invalid number of elements..."
else:
print_array(a)
def upper(instr):
return instr.upper()
def lower(instr):
return instr.lower()
instr = raw_input("Input a string: ")
print "To upper or lower (u/l): "
inchar = raw_input()
if inchar == 'l':
print "The converted string is \n", lower(instr)
elif inchar == 'u':
print "The conveerted string is \n", upper(instr)
else:
print "Ivalid input..."