Chapter 5: Arrays, qualifiers, and reading numbers¶

Example 5.1, Page number: 84¶

In :
# Variable declaration
data = [ 34.0, 27.0, 45.0, 82.0, 22.0 ]

# Calculation
total = data + data + data + data + data
average = total/5.0

# Result
print ('Total %f Average %f' % (total, average))
Total 210.000000 Average 42.000000

Example 5.2, Page number: 87¶

In :
# Variable declaration
name = 'Sam'

# Result
print ('The name is %s' % name)
The name is Sam

Example 5.3, Page number: 88¶

In :
# Variable declaration
first = 'Steve'
last = 'Oualline'

# Calculation
full_name = first + ' ' + last

# Result
print ('The full name is %s' % full_name)
The full name is Steve Oualline

Example 5.4, Page number: 89¶

In :
# Variable declaration
line = 'hello world'

# Result
print ('The length of the line is %d' % len(line))
The length of the line is 11

Example 5.5, Page number: 90¶

In :
# Variable declaration
first = 'Steve'
last = 'Oualline'

# Calculation
full = first + ' ' + last

# Result
print ('The name is %s' % full)
The name is Steve Oualline

Example 5.6, Page number: 91¶

In :
# Variable declaration
first = 'Steve'
last = 'Oualline'

# Calculation
full = first + ' ' + last

# Result
print ('The name is %s' % full)
The name is Steve Oualline

Example 5.7, Page number: 93¶

In :
# Variable declaration
array = [[0 for x in range(5)] for x in range(5)]
array = 0 * 10 + 0
array = 0 * 10 + 1
array = 1 * 10 + 0
array = 1 * 10 + 1
array = 2 * 10 + 0
array = 2 * 10 + 1

# Result
print ('array')
print (array)
print (array)
print ('\n')

print ('array')
print (array)
print (array)
print ('\n')

print ('array')
print (array)
print (array)
print ('\n')
array
0
1

array
10
11

array
20
21

Example 5.8, Page number: 94¶

In :
# Variable declaration
line = 4

# Result
print ('Twice %d is %d' % (line, line * 2))
Twice 4 is 8

Example 5.9, Page number: 95¶

In :
# Variable declaration
width = 12
height = 10

# Calculation
area = (width * height) / 2

# Result
print ('The area is %d' % area)
The area is 60

Example 5.10, Page number: 107¶

In :
# Variable declaration
width = 4
height = 6

# Calculation
area = (width * height) / 2

# Result
print ('The area is %d' % area)
The area is 12