[go: up one dir, main page]

0% found this document useful (0 votes)
31 views29 pages

Arrays - Part1

Uploaded by

avantica2802
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views29 pages

Arrays - Part1

Uploaded by

avantica2802
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Lesson 16 Saturday, May 11, 2024

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.

Array: A data structure that stores MULTIPLE data under


one identifier. All of the data in the array MUST be of the
same data type.
Array name

names
0 1 2 3 Index number
(Starts from 0)
Tom Sam Ron Chi

Contents of the array (Data type string)

Created by Mr Suffar
Arrays
Advantages of using array?

• Allows multiple items of data to be


stored under 1 identifier.

• Reduce the need for multiple


variables.

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

This table can be stored in a record, however it can’t


be stored in an array as it contains multiple data
types

Created by Mr Suffar
Arrays & Variables
Which one is easier to read and more efficient?

Using array: Using variables:


numbers = [5,4,8,3,6,1,4] number1 = 5
number2 = 4
number3 = 8
number4 = 3
number5 = 6
number6 = 1
number7 = 4

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"]

What do you think the following code will display?


print(names[0])
• Rom

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:

Clue under here!

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:

Clue under here!

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:

films = ["Saw", "Sharks", "Lion king"]


print(films)
print(films[0])
print(films[-1])

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:

Clue under here!

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:

Clue under here!


https://youtu.be/-x698NfpgS8

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:

Clue under here!

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:

Solution 1 (using FOR):

Clue under here!

Solution 2 (using IF):


https://youtu.be/emDvgB3eMfA

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:

Solution 1 (using FOR):


numbers = [2,3,4,5,6,7,90]
number=int(input("enter number"))
for x in range(0,len(numbers)):
if number == numbers[x]:
print ("found")

Solution 2 (using IF):


numbers = [2,3,4,5,6,7,90] https://youtu.be/emDvgB3eMfA
number=int(input("enter number"))
if number in numbers:
print ("found")
else:
print("Not found")
Created by Mr Suffar
The code below will loop through the array. If any part of
the array is empty, it will ask for a name, then adds that
name to that part.

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:

Clue under here!

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

You might also like