Chapter 3: The Basic Data Types

Example 3.1, Page Number: 35

In [1]:
def func():
    x=-199       #local to func
    print x      #displays -199 
    
x=10             #local to main

func()
print x          #displays 10
-199
10

Example 3.2, Page Number: 37

In [1]:
def func1():
    global count
    print "count: ",count     #access global count
    func2()

def func2():
    for count in range(3):    #this is local variable
        print '.',

 
count=None                    #global variables

for i in range(10):
    count=i*2
    func1()
count:  0
. . . count:  2
. . . count:  4
. . . count:  6
. . . count:  8
. . . count:  10
. . . count:  12
. . . count:  14
. . . count:  16
. . . count:  18
. . .

Example 3.3, Page Number: 40

In [5]:
from ctypes import *

#Variable declaration
j=c_uint(60000)
i=c_int(60000)

#Result
print i.value,j.value
60000 60000

Example 3.4, Page Number: 41

In [8]:
for letter in xrange(ord('Z'),ord('A')-1,-1):
    print chr(letter),
    
Z Y X W V U T S R Q P O N M L K J I H G F E D C B A

Example 3.5, Page Number: 44

In [9]:
 
print "\n\\\b"
\

Example 3.6, Page Number: 45

In [2]:
def total(x):
    sum=0
    for i in xrange(1,x+1):
        sum=sum+i
        for count in range(10):
            print '-',
        print "The current sum is",sum
        
print "Computing summation of 5."
total(5)

print "Computing summation of 6."
total(6)
Computing summation of 5.
- - - - - - - - - - The current sum is 1
- - - - - - - - - - The current sum is 3
- - - - - - - - - - The current sum is 6
- - - - - - - - - - The current sum is 10
- - - - - - - - - - The current sum is 15
Computing summation of 6.
- - - - - - - - - - The current sum is 1
- - - - - - - - - - The current sum is 3
- - - - - - - - - - The current sum is 6
- - - - - - - - - - The current sum is 10
- - - - - - - - - - The current sum is 15
- - - - - - - - - - The current sum is 21

Example 3.7, Page Number: 47

In [11]:
#Variable declaration
x=10
y=3

print x/y         #will display 3
print x%y         #will display1, the remainder

x=1
y=2

#Result
print x/y,x%y    #will display 0 1
3
1
0 1

Example 3.8, Page Number: 51

In [12]:
 
def xor(a,b):
    return (a or b)and(not(a and b))

#User-input
print "Enter P(0 or 1):"
p=1        
print "Enter Q(0 or 1):"
q=0

#Result
print "P AND Q:",(p and q)
print "P OR Q:",(p or q)
print "P XOR Q:",xor(p,q)
Enter P(0 or 1):
Enter Q(0 or 1):
P AND Q: 0
P OR Q: 1
P XOR Q: True

Example 3.9, Page Number: 54

In [3]:
for i in xrange(1,100+1):
    print i,"/ 2 is:",float(i)/2
1 / 2 is: 0.5
2 / 2 is: 1.0
3 / 2 is: 1.5
4 / 2 is: 2.0
5 / 2 is: 2.5
6 / 2 is: 3.0
7 / 2 is: 3.5
8 / 2 is: 4.0
9 / 2 is: 4.5
10 / 2 is: 5.0
11 / 2 is: 5.5
12 / 2 is: 6.0
13 / 2 is: 6.5
14 / 2 is: 7.0
15 / 2 is: 7.5
16 / 2 is: 8.0
17 / 2 is: 8.5
18 / 2 is: 9.0
19 / 2 is: 9.5
20 / 2 is: 10.0
21 / 2 is: 10.5
22 / 2 is: 11.0
23 / 2 is: 11.5
24 / 2 is: 12.0
25 / 2 is: 12.5
26 / 2 is: 13.0
27 / 2 is: 13.5
28 / 2 is: 14.0
29 / 2 is: 14.5
30 / 2 is: 15.0
31 / 2 is: 15.5
32 / 2 is: 16.0
33 / 2 is: 16.5
34 / 2 is: 17.0
35 / 2 is: 17.5
36 / 2 is: 18.0
37 / 2 is: 18.5
38 / 2 is: 19.0
39 / 2 is: 19.5
40 / 2 is: 20.0
41 / 2 is: 20.5
42 / 2 is: 21.0
43 / 2 is: 21.5
44 / 2 is: 22.0
45 / 2 is: 22.5
46 / 2 is: 23.0
47 / 2 is: 23.5
48 / 2 is: 24.0
49 / 2 is: 24.5
50 / 2 is: 25.0
51 / 2 is: 25.5
52 / 2 is: 26.0
53 / 2 is: 26.5
54 / 2 is: 27.0
55 / 2 is: 27.5
56 / 2 is: 28.0
57 / 2 is: 28.5
58 / 2 is: 29.0
59 / 2 is: 29.5
60 / 2 is: 30.0
61 / 2 is: 30.5
62 / 2 is: 31.0
63 / 2 is: 31.5
64 / 2 is: 32.0
65 / 2 is: 32.5
66 / 2 is: 33.0
67 / 2 is: 33.5
68 / 2 is: 34.0
69 / 2 is: 34.5
70 / 2 is: 35.0
71 / 2 is: 35.5
72 / 2 is: 36.0
73 / 2 is: 36.5
74 / 2 is: 37.0
75 / 2 is: 37.5
76 / 2 is: 38.0
77 / 2 is: 38.5
78 / 2 is: 39.0
79 / 2 is: 39.5
80 / 2 is: 40.0
81 / 2 is: 40.5
82 / 2 is: 41.0
83 / 2 is: 41.5
84 / 2 is: 42.0
85 / 2 is: 42.5
86 / 2 is: 43.0
87 / 2 is: 43.5
88 / 2 is: 44.0
89 / 2 is: 44.5
90 / 2 is: 45.0
91 / 2 is: 45.5
92 / 2 is: 46.0
93 / 2 is: 46.5
94 / 2 is: 47.0
95 / 2 is: 47.5
96 / 2 is: 48.0
97 / 2 is: 48.5
98 / 2 is: 49.0
99 / 2 is: 49.5
100 / 2 is: 50.0
In [ ]: