Watch the video and take notes, pay attention to key functionality of an array.
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.
Insert data into a given position in the array
# Example code (insert)
names = ["Owen","Henry","Gemma"] # setup an array that contains 3 items
newName = str(input("Enter a new name to insert into the list :")) # input a name into a variable
names.insert(1,newName) # insert the contents of newName into the array in position 1
names.insert(0,"Robert") # insert the string "Robert" into the array at position 0
print(names) # print the contents of the array to show position of inserted data
Index positions for data held in an array
# Example code (index)
names = ["Owen", "Henry", "Gemma", "Robert", "Kimberly"] # an array of string values (peoples names)
print(names) # prints out the arrays contents to inform the user
nameFromUser = str(input("Enter a name from the array above :")) # inputs a name to seach the list for and stores it is nameFromUser
position = names.index(nameFromUser) # searches the names array for an entry that matches the contents of nameFromUser and puts its index position in the position variable as an integer
print("The name",nameFromUser,"is found at position :",position) # prints the information on screen
Sort contents of an array (ascending & descending)
# Example code (sort)
names = ["Owen", "Henry", "Gemma", "Robert", "Kimberly"] # an array of string values (peoples names)
print(names) # prints out the arrays contents to inform the user
names.sort() # sorts the contents of the array a to z (asending)
print(names) # prints out the contents of the sorted array
names.sort(reverse=True) # sorts the contents of the array z to a (desending)
print(names) # print out the arrays contents of the sorted array
Apply your knowledge of insert/index and sort to write a program that will prompt the user input the index position of two numbers from the array they wish to multiply. The program will multiply the two numbers and insert the answer at position 0 within the array before outputting its content. The array will then be sorted in ascending order and outputted again.
output array contents to allow user index position selection
ask user to input two integer values (index positions of numbers to use from array)
multiply the integers from the given positions
output and store the result of the multiplication
insert the result of the multiplication into the array in position 0
output the arrays contents
sort the array in ascending order
output the sorted arrays contents
Use the following array in your program:
numbers = [2,5,4,8,6,12,19,4,1,0]
numbers = [2,5,4,8,6,12,19,4,1,0]
print (numbers)
positionOne = int(input("Enter the index position of the first number :"))
positionTwo = int(input("Enter the index position of the second number :"))
result = numbers[positionOne] * numbers[positionTwo]
print("The result of the multiplication was :", result)
numbers.insert(0,result)
print(numbers)
numbers.sort()
print(numbers)
Describe the purpose of each part of the insert line of code below
names = ["Ted","Fred","Jed"]
names.insert(2,"Gary")
Describe one real world example of when insert could be used in a computer application or game.
Index is a command that finds the position in an array of any given value. State a reason why this is useful when programming
Describe the process index undertakes while finding the value in an array
State what happens if index cannot find a specified value in an array
State the output from the following program
fruit = ["Apples", "Pears", "Plums", "Apples"]
print(fruit.index("Apples")
Describe the purpose of each part of the line code that sorts the array
names = ["Ted","Fred","Gary","Jed"]
names.sort(reverse=True)
Describe the purpose of each part of the insert line of code below
names = ["Ted","Fred","Jed"]
names.insert(2,"Gary")
name of the array to insert data into . command ( index position to insert data into , data to insert )
Describe one real world example of when insert could be used in a computer application or game.
An array might hold information like a games character names. An array could also be used to hold stats or results for sports game.
Index is a command that finds the position in an array of any given value. State a reason why this is useful when programming
index is incredibly useful as it gives a programmer the ability to search an array of data no matter how large for a occurrence of a single value. This value can then to used or acknowledged as present in the array.
Describe the process index undertakes while finding the value in an array
The index function checks the value you are searching for against each value in an array starting at 0. It moves through the array one at a time until the search value is equal to the contents of a arrays index positions value.
State what happens if index cannot find a specified value in an array
The program will crash as the value is not found in the array
State the output from the following program
fruit = ["Apples", "Pears", "Plums", "Apples"]
print(fruit.index("Apples")
0 (zero would be the output on the screen as it will find the first instance of the value then stop)
Describe the purpose of each part of the line code that sorts the array
names = ["Ted","Fred","Gary","Jed"]
names.sort(reverse=True)
name of the array to sort . command to sort ( declare that it is to be sorted in descending order )