0% found this document useful (0 votes)
17 views

Arrays - Part3

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)
17 views

Arrays - Part3

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/ 27

Lesson 18 Saturday, May 11, 2024

Arrays & lists


Lesson objectives…
Understand the difference between arrays
& lists.
Solve real life problems using lists.
Use iteration to loop through a list.
https://youtu.be/in2-sWY2eHg

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.

Arrays are static data structures meaning it can not


GROW or SHRINK.

Created by Mr Suffar
Arrays & Lists
• A list is also a data structure that stores multiple
values, under 1 identifier.

• However, the data can be of DIFFERENT data types.

• A list can grow and shrink.

• The way you create a list in python is exactly the same


as an Array using [ ]

However, if it grows or shrinks while the program is


running, it means it’s a list.
Created by Mr Suffar
Lists
Append means adding something to the end.
#Creates an empty list/array called numbers.
numbers = [ ]
#Loops 4 times.
for x in range(4):
#Ask the user for a number
num = int(input("Enter a number"))
#Adds the number to the end of the list.
numbers.append(num)

#Displays the list


print(numbers)

Created by Mr Suffar
Lists
Empty list

Adds the name to the


end of the 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

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
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:

Clue under here!

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:

Clue under here!

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:

Clue under here!

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:

Clue under here!

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]

print("Total points earned by Mancity:",totalMancity)


print("Total points earned by Liverpool:",totalLiverpool)
if totalLiverpool > totalMancity:
print("Liverpool has the highest number of points",totalLiverpool)
elif totalLiverpool < totalMancity:
print("Man City has the highest number of points",totalMancity)
else:
print("They have the same number of points")
https://youtu.be/wGdoOlX4058

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:

Clue under here!

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:

Clue under here!

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:

Clue under here!

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:

Clue under here!

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:

Clue under here!

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy