Python KPRIDE: Two-Dimensional Arrays/Lists

Last updated 6 months ago
8 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
Remember that an array or a list holds a collection of data.
A One-Dimensional Array is like a Row (going across) of lockers. Each "locker" has a position". The very first "item" location is at position 0.

Now a Two-Dimensional Array has a Row and a column. The Top-left position is at position 0,0.
The bottom-left position is at position 1,0.
1

See Icon 1 - what position might this be in a 2D array?

1

See Icon 2 - what position might this be in a 2D array?

1

See Icon 3 - what position might this be in a 2D array?

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 = [["Adam","Bianca","Chase"],[1,2,3]]
print(myArray)

1

What is the output:

myArray = [["Adam","Bianca","Chase"],[1,2,3]]
print(myArray[0][0])

1

What is the output:

myArray = [["Adam","Bianca","Chase"],[1,2,3]]
myArray[0][0] = "Albert"
print(myArray[0][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. If you were to print myArray, what would be the output?
  3. What happens if you output row on line 8?
  4. What happens if you added another name to the first row?


Extra Knowledge: https://www.tutorialspoint.com/python_data_structure/python_2darray.htm

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
twoDimensionalList = [ [row1Item], [row2Item]]
1

Create a 2D array for a row of names, and another row for their scores.

print out each of the names and the scores

1

Create a 2D array for a row of adjectives (describing words) and a row of nouns.

Ask the user to enter a number that helps chooses an adjective, and another number to choose the noun (person/place/thing).