Python KPRIDE: Arrays and Lists

Last updated 7 months ago
12 questions

For GCSE, you must know the following for programming:

  • Outputs and Inputs
  • Variables
  • Maths Operators
  • Random numbers
  • Strings and Casting
  • Booleans
  • If, elif, else
  • Nested Ifs
  • For loops
  • While loops
  • Nested loops
  • Arrays/Lists
  • 2D Lists
  • File handling
  • Subprograms(functions/procedures)

Warm-up Practice:
https://type.withcode.uk/python/Arrays

Learning Objectives:

  • I know what an array and a list is
  • I can create a one-dimentional array/list
  • I can create and solve problems using arrays and lists

Keywords

  1. Array: A collection of similar data of a fixed length. E.g. myArray = ["Apple", "Banana", "Cherry"]
  2. List: A collection of different data types of any size. E.g. myList = ["Apple", "B" , 3.142, ]
  3. Data Structure: A storage that is used to store and organize data (such as Arrays)
  4. One-dimentional Array: An array that holds a "row" of data
  5. two-dimentional Array: An array that holds a "rows" and "columns" of data

https://www.w3schools.com/python/python_arrays.asp
What are arrays / lists?

Arrays hold many values of the same data type
List hold many values that can be any mixed data types.
In Python, we can make lists using name = [values]

For GCSE most of the time, we can use arrays and lists to mean the same thing. It is more specific in A-Level or in the work force.


Imaging you have some variables:
item1 = "apple"
item2 = "banana"
item3 = "Cherry"

It can be easier to have it under one "variable" name:
myItems = [ "apple", "banana", "cherry"]

So print(myItems) would output
[ "apple", "banana", "cherry"]

Similar to indexing with variables using [x],
using it on arrays/lists will output the item in the list.
1

An array can be created like this:

arrayName = [ "Value 1", "Value 2" , .... ]

Look at the image on the left.
What would you think the code below will do:

print ( a [0] )

1

An array can be created like this:

arrayName = [ "Value 1", "Value 2" , .... ]

Look at the image on the left.
What would you think the code below will do:

print ( a [1] )

1

An array can be created like this:

arrayName = [ "Value 1", "Value 2" , .... ]

Look at the image on the left.
What would you think the code below will do:

print ( a [4] )

Predict and Run

Look at the code for each question.
What do you think will be the output?
What is the actual output?
1

What is the output:

myArray = ["Apple", "Banana", "Cherry"]
print (myArray)

1

What is the output:

myArray = ["Apple", "Banana", "Cherry"]
print (myArray[0])

1

What is the output?

myArray = []
myArray.append("A")
print (myArray)

1

What is the output?

cars = ["Ford", "Volvo", "BMW"]
cars.remove("Ford")
print(cars)

1

What is the output?

cars = ["Ford", "Volvo", "BMW"]
cars.remove("Ford")
print(cars[0])

Investigate

  1. Look at the code on the right.
  2. Run it and see what it does.
  3. Click on the pencil icon at the top to see the code again.

FIRST EXAMPLE:
  1. Run the program and see what it does
  2. Add some items into myShoppingList = [], so that it already starts with items.
  3. What does while removeItem != "": do? How does this stop?
  4. Look at this code for items in myShoppingList: What is different about this for loop?

Debug

Click on each link and try dragging and dropping code into the correct order
https://7strikelink7.github.io/NormalPythonParsons/parsons/Arrays.html

Extend

Help:
print ( ) is used to output text
input( ) is used to get the user to type something

Variables store values that can change.
Name = Value here

To manipulate "Strings":
len( word ) returns how long it is
.upper() turns it to upper case
.lower() turns it to lower case
variable[x] returns the letter at position x
int( string) turns it to a string (if possible)
float (string) turns it to a float (if possible)
str (variable) turns it to a string " "


Boolean: a data type that can hold one of two values (true or false)
> Symbol: Greater than
< Symbol: Less than
== Symbol: Compares if both values are the same
!= Symbol: Not equal to
>= Symbol: Greater than or equal to
<= Symbol: Less than or equal to

Selection -
if (condition) :
(indent) program here....

Iteration -

for varName in range (start, end, increment):
(indent) program here....

while (condition) :
program here ....

Lists:
myList = [ "item1", "item2", "item3"]
print (myList)
print (myList[0]) #Outputs 1st item
myList.append("Item4") #Add new item
myList.remove("Item4") #Removes "item4"
myList[0] = "newItem" #Changes value in 0
1

Create a program to store these values in one list:
1,1,2,3,5,8,12,20

then print it all out

1

Create a program with this list:
myList = [ "item1", "item2", "item3"]

remove "item1" then print the list.

1

Create a program with this list:
myList = [ "item1", "item2", "item3"]

change the value of item2 to your favourite food using this code:

myList[1] = "food here"

print the whole list.

1

Create a program that stores peoples name for a volunteering group.
Keep adding names into the list until a blank value "" is added.

Output the whole list