P06d
P06d
Define the term ‘array’. (2)
An array is a collection (1) of elements
that are all homogenous/the same data type (1).
P06d
Python does not have built-in arrays. Give the name of the data structure that Python uses to implement arrays? (1)
List
P06d
Give the line(s) of code needed to create (declare/initialise) a list of integers named myNumbers, from 5 to 10. (2)
myNumbers = [ 5, 6, 7, 8, 9, 10]
myNumbers = (1)
[5, 6, 7,8, 9, 10] (1)
P06d
Load file ‘Q04_Student’ into your programming environment.
for item in fruits: (1)
print (item) (1)
P06d
Load file ‘Q05_Student’ into your programming environment.
for index in range (len(fruits) - 1, -1, -1):
print (fruits[index])
P06d
Subprograms may or may not return results. Give two other features of subprograms. (2)
One mark for each of the following up to a maximum of (2):
State the type of subprogram that always returns a result. (1)
Function
import random
theRoll = 0
def rollTheDie ():
roll = 0
roll = random.randint(1,6)
return (roll)
theRoll = rollTheDie ()
print (theRoll)
# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
starNames = ["Rigel", "Canopus", "Sirius", "Betelgeuse", "Vega"]
starDistance = [860.0, 74.0 , 8.6, 1500.0, 25.0]
# ------------------------------------------------------------
# Subprograms
# ------------------------------------------------------------
def displaySky (pNames, pDistance):
for index in range (0, len(pNames)):
print (pNames[index] + " " + str(pDistance[index]))
# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
displaySky (starNames, starDistance)
print ("Goodnight")
# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
starNames = ["Rigel", "Canopus", "Sirius", "Betelgeuse", "Vega"]
starDistance = [860.0, 74.0 , 8.6, 1500.0, 25.0]
# ------------------------------------------------------------
# Subprograms
# ------------------------------------------------------------
def displaySky (pNames, pDistance):
for index in range (0, len(pNames)):
print (pNames[index] + " " + str(pDistance[index]))
# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
displaySky (starNames, starDistance)
print ("Goodnight")