Arrays - Part3
Arrays - Part3
Created by Mr Suffar
Arrays
Arrays are a data structure which can store multiple
values, however, all of the data must be of the same data
type.
Created by Mr Suffar
Arrays & Lists
• A list is also a data structure that stores multiple
values, under 1 identifier.
Created by Mr Suffar
Lists
Empty list
Created by Mr Suffar
Methods that can be applied to arrays
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current
list
index() Returns the index of the first element with the specified value
https://youtu.be/GC2uLJKa80M
Created by Mr Suffar
153. Write a program that asks the user to enter the name of 4 games and store these
within a list (array). Display the list (array).
Paste your code below:
array = []
for i in range (4):
game = input("Enter a game")
array.append(game)
print(array)
https://youtu.be/GC2uLJKa80M
Created by Mr Suffar
154.
• Create an empty list (array)
• Ask the user to enter 4 numbers
• Store them in the list (array).
• Display the list (array).
Paste your code below:
https://youtu.be/gT3oYosS2Sw
Created by Mr Suffar
154.
• Create an empty list (array)
• Ask the user to enter 4 numbers
• Store them in the list (array).
• Display the list (array).
Paste your code below:
numbers = []
for x in range (4):
num = int(input("Enter a number"))
numbers.append(num)
print(numbers)
https://youtu.be/gT3oYosS2Sw
Created by Mr Suffar
155.
• Create an empty list (array)
• Ask the user to add 6 numbers to the list (array) using a for loop.
• Display the first number in the list (array)
• On a separate line display the 4th number in the list (array)
• Calculate & display the AVERAGE of the numbers inside the list.
Paste your code below:
https://youtu.be/KuSShb1k4NA
Created by Mr Suffar
155.
• Create an empty list (array)
• Ask the user to add 6 numbers to the list (array) using a for loop.
• Display the first number in the list (array)
• On a separate line display the 4th number in the list (array)
• Calculate & display the AVERAGE of the numbers inside the list.
Paste your code below:
numbers = []
total = 0
for x in range(6):
num= int(input("Enter a number"))
numbers.append(num)
total = total + num
print(numbers[0])
print(numbers[3])
average = total / 6
print("The average is",average)
https://youtu.be/KuSShb1k4NA
Created by Mr Suffar
156.
• Liverpool = [55,65,75,65,78,45,76,66,98,102]
• ManCity = [52,61,85,75,68,65,96,86,99,85]
• The above 2 arrays shows the points earned by 2 teams at the end of each season over 10 years.
Write an algorithm that:
• Adds together all the number in each array (Liverpool and ManCity) and store them in separate variables.
• Displays the total points earned by each team over 10 years.
• Then displays which team has the highest number of points over 10 years or whether they have the same
number of points.
Paste your code below:
https://youtu.be/wGdoOlX4058
Created by Mr Suffar
156.
• Liverpool = [55,65,75,65,78,45,76,66,98,102]
• ManCity = [52,61,85,75,68,65,96,86,99,85]
• The above 2 arrays shows the points earned by 2 teams at the end of each season over 10 years.
Write an algorithm that:
• Adds together all the number in each array (Liverpool and ManCity) and store them in separate variables.
• Displays the total points earned by each team over 10 years.
• Then displays which team has the highest number of points over 10 years or whether they have the same
number of points.
Paste your code below:
liverpool = [55,65,75,65,78,45,76,66,98,102]
mancity = [52,61,85,75,68,65,96,86,99,85]
totalLiverpool = 0
totalMancity = 0
# in pseudocode= for x in range (0,9) or for x in range (0,len(array)-1)
for x in range (0,len(liverpool)):
totalLiverpool = totalLiverpool + liverpool[x]
totalMancity = totalMancity + mancity[x]
Created by Mr Suffar
157. A teacher wants to store student names in a list. He wants 2 separate lists, 1 for males and 1 for
females. Create a program that:
• Ask the user how many students are in his class.
• Asks for the name of each student.
• Ask if the student is male or female.
• Stores the names of the students in a list, male students in a list called male and female students in a
list called female.
• Displays both lists at the end of the program.
Paste your code below:
https://youtu.be/mhVBRoZrtUg
Created by Mr Suffar
157. A teacher wants to store student names in a list. He wants 2 separate lists, 1 for males and 1 for
females. Create a program that:
• Ask the user how many students are in his class.
• Asks for the name of each student.
• Ask if the student is male or female.
• Stores the names of the students in a list, male students in a list called male and female students in a
list called female.
• Displays both lists at the end of the program.
Paste your code below:
male = []
female = []
number = int(input("How many students are in your class?"))
for x in range (number):
name = input("Enter your name")
gender = input("Are you male or female")
if gender == "male":
male.append(name)
else:
female.append(name)
print(male)
print(female)
https://youtu.be/mhVBRoZrtUg
Created by Mr Suffar
158)
• Ask the user to enter 6 numbers and store them in a list (array).
• Ask the user if they want to see the total (sum) or the average of the 6 numbers.
• Loop through the list to calculate the sum of the numbers.
• Display the answer depending on the user’s choice.
Paste your code below:
https://youtu.be/0nNcCSP0EcU
Created by Mr Suffar
158)
• Ask the user to enter 6 numbers and store them in a list (array).
• Ask the user if they want to see the total (sum) or the average of the 6 numbers.
• Loop through the list to calculate the sum of the numbers.
• Display the answer depending on the user’s choice.
Paste your code below:
score = []
for i in range (6):
number=int(input("Enter a number: "))
score.append(number)
option = input("Do you want the total or the average?")
total = 0
for x in range(len(score)):
total = total+score[x]
if option == "total":
print(total)
elif option == "average":
print(total/6)
else:
print("Invalid option")
https://youtu.be/0nNcCSP0EcU
Created by Mr Suffar
Sorting
array = [1,7,2,5,6]
array.sort() Sorts the list in number/alphabetical order.
print(array)
array.reverse()
print(array)
Sorts the list in reverse order.
Created by Mr Suffar
159.
• Create an empty list (array).
• Ask the user to enter 3 games and store it in an list (array).
• Sort the list (array) in alphabetical order.
• Display the list (array)
• Then ask the user which index number in the list (array) do they want to see.
• Display the value of that index number.
Paste your code below:
https://youtu.be/fZ-XE500jbk
Created by Mr Suffar
159.
• Create an empty list (array).
• Ask the user to enter 3 games and store it in an list (array).
• Sort the list (array) in alphabetical order.
• Display the list (array)
• Then ask the user which index number in the list (array) do they want to see.
• Display the value of that index number.
Paste your code below:
array = []
for i in range (3):
game = input("Enter a game")
array.append(game)
array.sort()
print(array)
number = int(input("Which index number do you want to see"))
print(array[number])
https://youtu.be/fZ-XE500jbk
Created by Mr Suffar
160.
• Create an empty list.
• Ask the user to enter 5 numbers and store them in the list.
• Sort the list in order then display the list.
• Then Reverse the order of the list then display the new list in reverse order.
Paste your code below:
https://youtu.be/TfS7dnpAsYs
Created by Mr Suffar
160.
• Create an empty list.
• Ask the user to enter 5 numbers and store them in the list.
• Sort the list in order then display the list.
• Then Reverse the order of the list then display the new list in reverse order.
Paste your code below:
array = []
for i in range (5):
number = int(input("Enter a number"))
array.append(number)
array.sort()
print(array)
array.reverse()
print(array)
https://youtu.be/TfS7dnpAsYs
Created by Mr Suffar
Remove function
names = ["Tom","Sam","Ash","Mel"]
print(names)
names.remove(names[0])
print(names)
Remove() function is used to remove
specific items in the array. In this case, index
0 is removed from the array.
Created by Mr Suffar
Remove function
names = ["Tom","Sam","Ash","Mel"]
print(names)
choice = input("enter name to remove")
names.remove(choice)
print(names)
Removes the name that the user picks, if
the user types Sam, then Sam will be
removed.
Created by Mr Suffar
161.
• Create an empty list (array).
• Ask the user to input the name of 4 films and store them in a list (array).
• Display the first 2 films in the list.
• Remove the last film in the list hint: use
• Display the list
Paste your code below:
https://youtu.be/AaqqS1LpQWA
Created by Mr Suffar
161.
• Create an empty list (array).
• Ask the user to input the name of 4 films and store them in a list (array).
• Display the first 2 films in the list.
• Remove the last film in the list hint: use
• Display the list
Paste your code below:
array = []
for i in range (4):
film = input("Enter a film")
array.append(film)
print(array[0:2])
array.remove(array[-1])
print(array)
https://youtu.be/AaqqS1LpQWA
Created by Mr Suffar