Preview

08 - Lists and Introducing Arrays

 1. In Python - which of the following options has been declared as a list?
#Option 1
list1={1,2,3,4,5}

#Option 2
list2=[1,2,3,4,5]

#Option 3
list3=<1,2,3,4,5]

  None of the lists shown are lists!

  Option 2

  Option 3

  Option 1

 2. What is wrong with the creation/declaration of this list?
list2=[Adam,Eve,Cain,Abel,Seth]

  When you create a list of strings the strings must be in speech marks (e.g. list=["Adam","Eve"]

  You need curly braces instead of square brackets around the strings

  There is nothing wrong - it will work fine

  You do not need the square brackets around the strings

 3. You want the output to be 'zara' and '200'. What do you need to change?
list1, list2 = [123, 'xyz', 'zara', 'abc'], [456, 700, 200]
print "Max value element : ", max(list1)
print "Max value element : ", max(list2)

  Change line 3 to print "Min value element : ", min(list2)

  Change line 3 to: print "Min value element : ", max(list2)

  This cannot be done

  Change line 2 to: print "Max value element : ", min(list2)

 4. You want the output to be '123','xyz','abc' - what do you need to change?
list1, list2 = [123, 'xyz', 'zara', 'abc'], [456, 700, 200]
del list1[3]
print(list1)

  Delete line 1

  Change line 1 to: list1, = [eliminate zara]

  Delete line 2

  Change line 2 to: del list1[2]

 5. What do you need to put on line 2 in order to sort the list?
aList = [6,2,3,7,1,4,5];
#What needs to go here to get the output of 1,2,3,4,5,6,7?
print "List : ", aList

  sort(aList):

  aList.sort();

  aList.quicksort();

  sort().lista

 6. ave you heard anyone say: "I look up at the night sky and saw an array of stars". The word array suggests …..
Note: This powerpoint from www.teachyourselfpython.com walks you through all you need to know on lists. 

  There is no such term in programming

  a "mixed" group of items with several functions embedded

  a "single" item as opposed to many

   "lots" of items, as opposed to just one

 7. The following code employs the use of a list. What would the output be if you entered "Mathematics" at the input prompt?
subjects=["Computing","Maths","History"]


def main():
  course=input("Hello, what course are you finding out about?:")
  if course in subjects:
    print("We still have spaces on the: ", course, "course.")
  else:
    print("Sorry, we do not run that course")


main()

  Error

  None of the above

  We still have spaces on the Mathematics course

  Sorry, we do not run that course

 8. In a language like Python you don't 'declare' an array like you would in VB.Net but lists function in a similar way.

  TRUE

  FALSE

 9. What is the output for the following?
list=["Adam","Eve","Cain","Abel","Seth"]
print(list[3])

  Seth

  Eve

  Adam

  Abel

 10. What is different about the list 'matrix'?
def main():
#1. Create a nested list (each list is a row on the matrix)
   matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#2. print the matrix so it displays row by row in a square grid
   for i in matrix:
      print(i)


main()

  All of the above option sare correct

  It is a list within a list. The original or main list has within it three other lists, each seperated by commas

  It is named 'matrix' which is a peculiar name for a list

  It is a straightforward list with 10 elements contained within it

 11. In this 2d array (matrix) why is the output 6?
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[1][2])

  The output is 2, not 6

  Because 1 x 2 + 3 = 6. There are three lists within the list and the index numbers are 1 and 2

  Because it is searching for the second list (0…1) and the third element in that list (0…1…2), which is 6

  None of the above

 12. Trial the following code. On what line are the usernames that are entered being added to the list 'usernames'?
#TASK: Get the user to enter a list of usernames and passwords and store them in the lists
#----------the comments below tell you where to add code! Follow the hints and get it working
usernames=[]

#create another empty list to store user entered passwords

def main():
   register()
   

def register():
   username=input("Enter a username:")
   #********ask the user to also enter a password


   usernames.append(username)
   #********append the entered password to the passwords list that you created above


   
   answer=input("Do you want to make another registration?")
   if answer=="y" or answer=="Y" or answer=="Yes" or answer=="YES":
      register()
   else:
      registration_details()

  Line 12

  Line 18

  Line 20

  Line 16

 13. In the following python task (which you can attempt if you have the time) the third player is not assigned a weapon. Why?
#************TASK
#ask the user to enter four names of players one after the other, and store it in a list called "players"
#create a list of four different weapons and store it in a list called "weapons"
#assign each player a weapon, by looping over both lists simulatenously. e.g Player 1: Sword; Player 2: Arrows
#Print the result
#...........SOME OF THE CODE HAS BEEN STARTED FOR YOU - THERE ARE SOME ERRORS TOO - FIX AND COMPLETE!

#1. Create an empty list
players=[]
#2. Create a list of 4 weapons
weapons=["SWORD","BELT OF TRUTH"]
#2  Create the main sub
def main():
   #ask for the name of player 1
   player1=input("Enter name of Player 1:")
   #append the name of player 1 to the list of players
   players.append(player1)
   #ask for the name of player 2
   player2=input("Enter name of Player 2:")
   #append the name of player 2 to the list of players
   players.append(player2)
   #print the list of players that have been entered
   player3=input("Enter name of Player 3:")
   #append the name of player 2 to the list of players
   players.append(player3)
   #print the list of players that have been entered
   print("The four players are:", players)
   #assign each player a weapon and print the result(loop over both lists)
   for (x,y) in zip(players,weapons):
      print(x, "has been assigned the:",y)
main()

  Because the third player cannot be held in a list that is of size '2'

  Because the third player is deleted along with the weapon by the for loop going over the list

  None of the above are applicable - the third player is assigned a weapon along with the first two just fine!

  Because the list 'weapons' only has two weapons in it

 14. What one word do you need to add on line 3 for this whole mini program to work
subjects = ['Biology', 'Chemistry', 'Physics', 'Maths', 'Computing', 'English']

for i in range((subjects)):
    print('Room[',i,'] =', subjects[i])

what_subject = input("What Subject do you have next?: ")
print("That subject is assigned to the room: ", subjects.index(what_subject))

  for i in range(len(subjects)):

  All of the above are valid options

  for i in range(i(subjects)):

  for i in range(3(subjects)):

 15. What does this code do?
import random
weapons_list = ['sword', 'belt', 'armour', 'arrows', 'shield']
print(random.choice(weapons_list))

  Generates a list of random numbers and assigns them to a weapon

  Generates a list of random numbers (using the random.choice feature) and prints the whole list

  Generates and prints a random item (weapon) from the list

  None of the above