Watch the video and make notes on how to access data held in an array data structure by using a loop.
Run and walk through the example (in code window).
Read the task and write code to solve.
Make a note of successful code in your book.
Answer questions in your book.
Watch the video and create notes in order to be able able to apply the knowledge given.
# Example code (arrays length and loops)
characters = ["a","b","c","d","e","f"] # setup array with pre populated data elements
length = len(characters) # define variable length and use len() with the array characters to assess the number of data elements
print ("The total number of data items in the array are ",length) # print out length value for user
for loop in range (0,length,1): # use for loop with length to ensure a loop for each data element
print(characters[loop]) # use the loop variable to access each data element one after another
Apply your knowledge of arrays/append/len & loops to write a program that will achieve the following: The program will allow a user to enter 10 names and append them to an array. The array will then print those names out in order on the screen.
allow a user to input 10 names and append each to an array
output each name in order one after another (not all at once)
names = []
for collect in range (0,10,1):
newName = str(input("Enter a name to append to the array :"))
names.append(newName)
length = len(names)
for printer in range (0,length,1):
print(names[printer])
Describe why using len() can help when wanting to access the contents of an array.
Debug and complete the code given, the error given when ran is the list is out of range.
letters = ["a","b","c","d","e","f"]
for loop in range(0,5,1):
print(letters[loop])
Complete the code, use a for loop to access all data elements in reverse.
letters = ["a","b","c","d","e","f"]
for loop in range(___,___,___):
print(letters[loop])
Complete the code, use a while loop to access all data elements in order.
letters = ["a","b","c","d","e","f"]
counter = 0
length = len(letters)
while ______ > ___ :
length = length - ___
print( letters[ _______ ] )
counter = counter + ___
State which loop type you prefer for accessing data elements in an array and describe why.
Describe why using len() can help when wanting to access the contents of an array.
By using len you can get the actual length of an array this allows a developer to use iteration to access the contents of an array. This can be used to sort or search the contents.
Debug and complete the code given, the error given when ran is the list is out of range.
letters = ["a","b","c","d","e","f"]
for loop in range(0,5,1):
print(letters[loop])
for loop in range(0,6,1): OR for lop in range(0, len(letters), 1):
Complete the code, use a for loop to access all data elements in reverse.
letters = ["a","b","c","d","e","f"]
for loop in range(___,___,___):
print(letters[loop])
for loop in range(5,-1,-1): OR for loop in range(len(letters)-1,-1,-1):
Complete the code, use a while loop to access all data elements in order.
letters = ["a","b","c","d","e","f"]
counter = 0
length = len(letters)
while ______ > ___ :
length = length - ___
print( letters[ _______ ] )
counter = counter + ___
while length > 0:
length = length - 1
print( letters[counter] )
counter = counter + 1
State which loop type you prefer for accessing data elements in an array and describe why.
I would use the for loop as it uses a control variable that increments automatically, whereas when a while loop is used the developer needs to increment the variables and control the access to each array element which uses more variables and loop conditions.