Watch the video and take notes, pay attention to how one array can hold multiple pieces of data.
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.
Append data to a list example
# EXAMPLE CODE (Arrays / Lists)
subjects = [] # Create an empty list
favorite = "Computer Science"
subjects.append(favorite) # append the contents of a value into the list (adds "Computer Science") in position 0
subjects.append("History") # appends the string data "History" to the list in position 1
subjects.append("Math") # appends the string data "Math" to the list in position 2
subjects.append("English") # appends the string data "English" to the list in position 3
subjects.append(str(input("Enter a subject area"))) # uses an input statement to input string data into a list at position 3
print(subjects) # when giving printing the whole list it will just print all of the subjects out on the screen
print(subjects[1]) # print out only the subject in the list at position 1
print(subjects[1:3]) # prints the values in the list between the two index numbers given
print(subjects[:3]) # prints the values from the start of the list to index position 2
print(subjects[2:]) # prints the values from the index position 2 to the end of the list
Delete data from list example
# EXAMPLE CODE (Array / Lists)
names = ["Owen", "Gemma", "Robert", "Kimberly", "Steven", "Emma"]
print (names) # print out the contents of the list
del names[2] # delete the name in position 2
print(names) # print out the contents to show deletion of name
del names [:2] # delete the names from the start up to index position 2
print(names) # print out names to show deletion of names
del names [1:] # delete the names after position 1 to the end
print(names) # print out the names after deletion
Apply your knowledge of arrays/lists to write a program that will collect data from a user. The program should allow a user to enter 10 names of their friends. The program will then print out only their best friend from the list.
input the names of a friend, appending it to a list
repeat 10 times (iteration)
print out contents of list of names
print out "My best friend is.." then print the name of their best friend using the index position in the array.
friends = [] # empty list to use when appending string data
friends = []
for loop in range (1,11,1):
name = str(input("Enter a friends name :"))
friends.append(name)
print(friends)
print ("My best friend is...", friends[4])
Give the starting index position of every array/list
Describe why you might use an array/list in a program
Explain the following: print(friends[5:9])
Explain the following: print(friends[:5])
Explain the following: del friend[5:9]
Give the starting index position of every array/list
0
Describe why you might use a array/list in a program
You would use an array/list in a program when you need to collect multiple data items that have the same use and data type, for example a contacts program would need an array of surnames which would all be strings.
Explain the following: print(friends[5:9])
5 is the index position in the array/list to start printing from 9 is the position in the array/list that it will stop printing. The : essentially means between
Explain the following: print(friends[:5])
Because their is no start index position the print simply starts at the beginning of the array/list
Explain the following: del friend[5:]
Because their is no end position to stop at the delete simply deletes from index position 5 to the end of the list leaving data in positions 0 to 4.