Chapter 10 - Arrays

example 10.1, page no. 206

In [1]:
testScore = [] #array defined

example 10.2, page no. 206

In [2]:
print "Enter the number of test scores:",
numTests = int(raw_input())
testScore = [numTests]
Enter the number of test scores:67

example 10.3, page no. 207

In [3]:
numTests = 3
print "Enter the number of test scores:",
numTests = int(raw_input())
testScore = [numTests]
Enter the number of test scores:67

example 10.4, page no. 208

In [4]:
numTests = 3
print "Enter the number of test scores:",
num = int(raw_input())
numTests = num
testScore = [numTests]
Enter the number of test scores:78

example 10.5, page no. 213

In [5]:
name = ['J', 'e', 'f', 'f', '\0']
print name
['J', 'e', 'f', 'f', '\x00']

example 10.5, page no. 213

In [6]:
name = ['J', 'e', 'f', 'f']
print name
['J', 'e', 'f', 'f']

example 10.7, page no. 216

In [7]:
testScore = []
print "Enter test score #1: ",
testScore.append(int(raw_input()))
print "Enter test score #2: ",
testScore.append(int(raw_input()))
print "Enter test score #3: ",
testScore.append(int(raw_input()))
print "Test score #1: ", testScore[0]
print "Test score #2: ", testScore[1]
print "Test score #3: ", testScore[2]
Enter test score #1: 78
 Enter test score #2: 88
 Enter test score #3: 65
 Test score #1:  78
Test score #2:  88
Test score #3:  65

example 10.8, page no. 216

In [8]:
print "Enter test score #1: ",
testScore1 = int(raw_input())
print "Enter test score #2: ",
testScore2 = int(raw_input())
print "Enter test score #3: ",
testScore3 = int(raw_input())
print "Test score #1: ", testScore1
print "Test score #2: ", testScore2
print "Test score #3: ", testScore3
Enter test score #1: 65
 Enter test score #2: 67
 Enter test score #3: 88
 Test score #1:  65
Test score #2:  67
Test score #3:  88

example 10.9, page no. 217

In [9]:
testScore = []
for i in range(3):
    print "Enter testScore #", i+1, ": ",
    testScore.append(int(raw_input()))

for i in range(3):
    print "Test Score #", i+1, ": ", testScore[i]
Enter testScore # 1 : 3
 Enter testScore # 2 : 4
 Enter testScore # 3 : 5
 Test Score # 1 :  3
Test Score # 2 :  4
Test Score # 3 :  5

example 10.10, page no. 217

In [10]:
MAX = 3

testScore = []
for i in range(MAX):
    print "Enter testScore #", i+1, ": ",
    testScore.append(int(raw_input()))

for i in range(MAX):
    print "Test Score #", i+1, ": ", testScore[i]
Enter testScore # 1 : 5
 Enter testScore # 2 : 6
 Enter testScore # 3 : 7
 Test Score # 1 :  5
Test Score # 2 :  6
Test Score # 3 :  7

example 10.11, page no. 218

In [11]:
MAX = 3

testScore = []
for i in range(MAX+1):
    print "Enter testScore #", i+1, ": ",
    testScore.append(int(raw_input()))

for i in range(MAX+1):
    print "Test Score #", i+1, ": ", testScore[i]
Enter testScore # 1 : 4
 Enter testScore # 2 : 5
 Enter testScore # 3 : 6
 Enter testScore # 4 : 3
 Test Score # 1 :  4
Test Score # 2 :  5
Test Score # 3 :  6
Test Score # 4 :  3

example 10.12, page no. 219

In [12]:
MAX = 3
grades = [0,0,0]

for i in range(MAX):
    print "Enter Garde #", i+1, ": ",
    grades[i] = int(raw_input())
Enter Garde # 1 : 3
 Enter Garde # 2 : 4
 Enter Garde # 3 : 5

example 10.13, page no. 219

In [13]:
name = ['J','e','f','f']
print "Enter your name: ",
name = raw_input()
print "Your name is: ", name
Enter your name: Jeff
 Your name is:  Jeff

example 10.14, page no. 220

In [14]:
MAX = 3
testScore = []

for i in range(MAX):
    print "Enter test score #", i + 1, ": ",
    testScore.append(int(raw_input()))
print "The test scores are: ", testScore
Enter test score # 1 : 4
 Enter test score # 2 : 5
 Enter test score # 3 : 6
 The test scores are:  [4, 5, 6]

example 10.15, page no. 221

In [15]:
testScore = []
testScore = raw_input()
45

example 10.16, page no. 222

In [16]:
name = ['J','e','f','f']
print "Enter your name: ",
name = raw_input()
print "Your name is: ", name
Enter your name: Jeff
 Your name is:  Jeff

example 10.17, page no. 222

In [17]:
print "Enter your name: ",
name = raw_input()
print "Your name is: ", name
Enter your name: Jeff
 Your name is:  Jeff

example 10.18, page no. 223

In [18]:
print "Enter your name: ",
name = raw_input()
print "Your name is: ", name
Enter your name: Jeff
 Your name is:  Jeff

example 10.19, page no. 225

In [19]:
MAX = 3

testScore = []
for i in range(MAX):
    print "Enter test score #", i + 1, ": ",
    testScore.append(int(raw_input()))

for i in range(MAX):
    print "Test score #", i + 1, ": ", testScore[i]
Enter test score # 1 : 4
 Enter test score # 2 : 5
 Enter test score # 3 : 6
 Test score # 1 :  4
Test score # 2 :  5
Test score # 3 :  6

example 10.20, page no. 225

In [20]:
MAX = 3

def assignValues(tests, num):
    for i in range(num):
        print "Enter test score #", i + 1, ": ",
        tests.append(int(raw_input()))

def displayValues(scores, elems):
    for i in range(elems):
        print "Test score #", i + 1, ": ", scores[i]


testScore = []
assignValues(testScore, MAX)
displayValues(testScore, MAX)
Enter test score # 1 : 5
 Enter test score # 2 : 6
 Enter test score # 3 : 7
 Test score # 1 :  5
Test score # 2 :  6
Test score # 3 :  7
In [ ]: