Arrays - Part1
Arrays - Part1
Arrays
Lesson objectives…
Describe the purpose of an array.
Create a program using arrays.
Explain the difference between arrays and
records.
https://youtu.be/ZfQ5XM7i7gk
Created by Mr Suffar
Arrays
Variable: Location in memory that stores a value which
can be changed. A variable can only store 1 Value.
names
0 1 2 3 Index number
(Starts from 0)
Tom Sam Ron Chi
Created by Mr Suffar
Arrays
Advantages of using array?
Created by Mr Suffar
Records
Records: A data structure that stores multiple data. Data
can be of MULTIPLE data types.
Name Price
Biscuit 2.5
Crisps 1.5
Created by Mr Suffar
Arrays & Variables
Which one is easier to read and more efficient?
Created by Mr Suffar
Arrays
Array index starts from 0. Quotation marks are needed around
strings but not around integers.
names = ["Rom","Ash","Sam","Ali","Mark"]
print(names[2])
• Sam
Created by Mr Suffar
138) Create an array called numbers and store 5 numbers in the array. Display the 2nd
number in the array.
Paste your code below:
https://youtu.be/L4ASjfTz-mA
Created by Mr Suffar
138) Create an array called numbers and store 5 numbers in the array. Display the 2nd
number in the array.
Paste your code below:
numbers = [1,3,2,6,7]
print(numbers[1])
https://youtu.be/L4ASjfTz-mA
Created by Mr Suffar
139)
• Create an array called films and store your top 3 films in it.
• Display your array.
• Then display the first film in the array on a separate line.
• Then display the last film in the array on a separate line.
Paste your code below:
https://youtu.be/fSXovg_c5JI
Created by Mr Suffar
139)
• Create an array called films and store your top 3 films in it.
• Display your array.
• Then display the first film in the array on a separate line.
• Then display the last film in the array on a separate line.
Paste your code below:
https://youtu.be/fSXovg_c5JI
Created by Mr Suffar
140)
• Create an array called names and store 3 names inside it.
• Ask the user which index number do they want to see. If the user picks a number that is
outside the index range (above 2) then repeat the question again until a valid number is
entered.
• Display the item on that index number.
Paste your code below:
https://youtu.be/Zq9owChfHeM
Created by Mr Suffar
140)
• Create an array called names and store 3 names inside it.
• Ask the user which index number do they want to see. If the user picks a number that is
outside the index range (above 2) then repeat the question again until a valid number is
entered.
• Display the item on that index number.
Paste your code below:
names = ["Tom","Sam","Roy"]
number = int(input("Enter index number"))
while number > 2:
number = int(input("Enter index number"))
print(names[number])
https://youtu.be/Zq9owChfHeM
Created by Mr Suffar
Slicing
array = [2,3,4,5]
print(array[0:2]) #displays index 0&1
print(array[0:3]) #prints index 0&1&2
print(array[1:3]) #prints index 1&2
Created by Mr Suffar
141. Create an array called singers that contains the following singers:
"Rihanna","Katy","Adele","Ed"
Use 3 print commands to:
• Display the first singer in the array
• Then display the first 3 singers in the array.
• Then display the last singer in the array
Paste your code below:
Created by Mr Suffar
141. Create an array called singers that contains the following singers:
"Rihanna","Katy","Adele","Ed"
Use 3 print commands to:
• Display the first singer in the array
• Then display the first 3 singers in the array.
• Then display the last singer in the array
Paste your code below:
singers = ["Rihanna","Katy","Adele","Ed"]
print(singers[0])
print(singers[0:3])
print(singers[-1])
https://youtu.be/-x698NfpgS8
Created by Mr Suffar
Arrays
What will the following code display?
• 3
Created by Mr Suffar
Arrays
You can use a FOR LOOP to loop through an array.
The following code will display ALL of the contents in the
array on separate lines:
Created by Mr Suffar
Lets see how it works in a trace table:
x Output
films = ["Saw", "Sharks", "Lion king"]
for x in range(0,len(films)):
print(films[x])
0 Saw
1 Sharks
2 Lion King
Created by Mr Suffar
The code below asks the user to enter a name, if the
name is inside the array, it will display Found.
Created by Mr Suffar
142.
• Create an array called games and store the following games inside it:
"OW","CSGO","FIFA","FORTNITE"
• Use a FOR LOOP to loop through the array and display all 4 games on separate lines.
Paste your code below:
https://youtu.be/d60jdqOLNDI
Created by Mr Suffar
142.
• Create an array called games and store the following games inside it:
"OW","CSGO","FIFA","FORTNITE"
• Use a FOR LOOP to loop through the array and display all 4 games on separate lines.
Paste your code below:
games = ["OW","CSGO","FIFA","FORTNITE"]
for x in range(0,len(games)):
print(games[x])
https://youtu.be/d60jdqOLNDI
Created by Mr Suffar
143.
• Create the following array: numbers = [2,3,4,5,6,7,90]
• Ask the user to guess a number.
• If the number is inside the array, display “found”.
Paste your code below:
Created by Mr Suffar
143.
• Create the following array: numbers = [2,3,4,5,6,7,90]
• Ask the user to guess a number.
• If the number is inside the array, display “found”.
Paste your code below:
Created by Mr Suffar
144. A teacher wants to add his pupil names to an array. His class has 10 students. He created the array,
however some names are missing.
The array has been created:
students = ["Tom","","Andy","","Lisa","","","","","Abodi"]
Create an algorithm that:
• Checks each index in the array for a name, if it has a name, move on to the next index, if it doesn’t,
then ask the user for a name and add it to that index.
• Once all 10 names have been added to the array, display the array.
Paste your code below:
https://youtu.be/qFVALKQi5rY
Created by Mr Suffar
144. A teacher wants to add his pupil names to an array. His class has 10 students. He created the array,
however some names are missing.
The array has been created:
students = ["Tom","","Andy","","Lisa","","","","","Abodi"]
Create an algorithm that:
• Checks each index in the array for a name, if it has a name, move on to the next index, if it doesn’t,
then ask the user for a name and add it to that index.
• Once all 10 names have been added to the array, display the array.
Paste your code below:
students = ["Tom","","Andy","","Lisa","","","","","Abodi"]
for x in range(0,len(students)):
if students[x] == "":
name= input("Enter a name")
students[x] = name
print(students)
https://youtu.be/qFVALKQi5rY
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
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
Other Methods…
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
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Created by Mr Suffar