Search Header Logo
Arrays

Arrays

Assessment

Presentation

Computers

11th Grade

Practice Problem

Medium

Created by

K Parker

Used 51+ times

FREE Resource

11 Slides • 19 Questions

1

Arrays

​So far we have only used variables to store 1 value, eg:

name = "Bob"

An array allows us to store multiple values in 1 variable. It is like a LIST. eg:

name = ["bob", "Sally", "Harry"]​

media

2

Open Ended

What is an array?

3

What is an array?

  • Array is a container which can hold a fix number of items and these items should be of the same type.​ ​ For example, here is an array called Fruit. It contains 4 items of fruit​ ​

     

  • Fruit = ["Apples", "Plums", "Oranges", "Grapes"]​


  • Notice the SQUARE brackets. This is so that Python knows that it is an ARRAY. ​

    Notice that all items (fruits)in my array are of the same type of DATA (string) and I have separated each item with a comma (,)​

4

Fill in the Blank

Here is an array called Friends - what am I missing?


friends = "Bob" , "Sally"

5

Fill in the Blank

Here is the array again:


friends = ["Bob", "Sally"]


What data types are in my array?

6

Open Ended

Create your own array

Has to include at least 2 items

Can be anything you want - football teams, colours, TV shows etc

7

Finding an item within an ARRAY​

  • Here is my fruit array:​

  • Fruit = ["Apples", "Plums", "Oranges", "Grapes"]​

  • When we could the items in an array we ALWAYS start at 0​ So, in my array above "Apples" is item number 0, Plums is item number 1 and so on​ ​

8

Fill in the Blank

Fruit = ["Apples", "Plums", "Oranges", "Grapes"]​


If I wanted to print the item called "Grapes" what number should I call?​

9

Fill in the Blank

colours = ["red", "blue", "purple", "green"]​


If I wanted to print the item called "red" what number should I call?​

10

Fruit = ["Apples", "Plums", "Oranges", "Grapes"]​

  • If I wanted to print the third item in my array ("Oranges") to the screen I would type this:​ ​

  • print (Fruit [2])​ ​

  • Notice that I have asked to print the array called Fruit and I have asked for only the item number 2 – this is in SQUARE brackets []​

11

Fill in the Blank

Emotions = ["Sad", "Happy", "Angry", "Scared"]​

print (Emotions [3])​

What would be printed?

12

Fill in the Blank

colours = ["red", "blue", "purple", "green"]​


Type the code that would print 'blue' to the screen

(
[
]
)

13

Fill in the Blank

Fruit = ["Apples", "Plums", "Oranges", "Grapes"]​


Type the code that would print Apples to the screen

14

Arrays and SELECTION​

  • Here in an array that holds items (data) about 1 car:​ ​

  • Car = ["Vauxhall", "Astra", "TRUE"]​ ​

  • The third item refers to if the car has a valid MOT certificate​

  • We can start to use SELECTION to ask our array a question​

  • In this case I want to check if the car has a valid MOT certificate, if it has then I do NOT want to call the owner, if it doesn't have a valid certificate then I DO want to call the owner​

15

Car = ["Vauxhall", "Astra", "TRUE"]​

  • If Car [2] == "TRUE":​

        print ("Do not call owner")​

    else:​

      print("Call owner")​

16

Fill in the Blank

What would be printed to the screen?

Car = ["Vauxhall", "Astra", "TRUE"]​


If Car [0] == "TRUE":​

print ("Don't call")​

else:​

print("Call owner")​

17

Fill in the Blank

colours = ["Blue", "red", "Green"]

if colours [1] == "Green":

print ("Peas")

elif colours [0] == "Blue":

print ("Blueberries")

else:

print("Strawberries")


What will be printed to the screen?

18

Fill in the Blank

friends = ["Bob", "Sally", "Jane"]

if friends [ ?? ] == "Sally":

print("Hello Sally")


What am I missing to ensure "Hello Sally" will be printed- ??

19

Multiple Choice

Arrays hold multiple values of different types.

1

True

2

False

20

Multiple Choice

What best describes an array in programming?

1

An array is a single element data type that can be changed in the porgram.

2

An array is a type of function in programming that can be re-used repeatedly

3

A data structure that holds multiple values stored in an index.

4

An array is a method for sorting data to make it more effic

21

1-Dimensional Array

This is a single list of data. Each piece of data is stored a index, we then refer to the index to edit, replace or remove the data.

Creating an array works similarly to a variable, however we use square brackets:

StudentNames = ["Lucy" , "Andrew" , "Libby" , "Mark"]

media

​We can then refer to each index location, rather than the data stored in the array.

If this was a variable it would need 4 separate names e.g.
student1 = "Lucy"
student2 = "Andrew"

22

1-Dimensional Array

We can use the following code to manipulate the data in the same array.

StudentNames [2] would return Libby

StudentNames[1] = "Adam" would replace Andrew with Adam

StudentNames[4] = "Greg" would add a new index storing Greg

Print[StudentNames] would print all data in each index.

media

23

Multiple Choice

Question image

What would the correct code to make the following array, read each one carefully:

1

StudentNames = ("Lucy" , "Andrew" , "Libby" , "Mark")

2

StudentNames = ["Lucy" "Andrew" "Libby" "Mark"]

3

StudentNames = [Lucy , Andrew , Libby , Mark]

4

StudentNames = ["Lucy" , "Andrew" , "Libby" , "Mark"]

24

Open Ended

Design pseudocode to create an array name colours, the array will store 4 colours: white, pink, red and blue in that order:

25

media

A 2D Array is a list that stores multiple values with multiple rows.

This data structure stores data in a table. Each index is given two numbers, one for the row and the other for the column.

In the exam it will tell you whether you refer to row or column first, or it will give you an example like this.

2D Arrays

26

Manipulating 2D Arrays

To make a 2D array, is quite similar, the only difference is you will need multiple pairs of square brackets. Each new pair will be a new row:

ClassCodes = [["Y7A","Y7B","Y7C"],["Y8A","Y8B","Y8C"],["Y9A","Y9B","Y9C"]]

media

​If we were to visualise the 2D array this is what it would look like as a table:

27

Manipulating 2D Arrays

When referencing an index location in our array, we use 2 numbers. In this situation we will refer to row first and column second, if only one index location is given it will output the results from the entire row or column e.g.

media
  • ClassCodes [2] would output all Y9

  • ClassCodes [1,2] would output Y8C

  • ClassCodes [0,1] would output Y7B

28

Multiple Choice

How is a 2D array different from a 1D array?

1
A 2D array can only store integers, while a 1D array can store any data type.
2
A 2D array is always larger than a 1D array in memory usage.
3
A 2D array can only be accessed sequentially, while a 1D array can be accessed randomly.
4

A 2D array has rows and columns, whereas a 1D array has only a single row

29

Fill in the Blank

Question image

If we use a row first technique for this array, what data would be output using ClassCodes [0,1]

30

Fill in the Blank

Question image

What number would be output Numbers [1,1]

Arrays

​So far we have only used variables to store 1 value, eg:

name = "Bob"

An array allows us to store multiple values in 1 variable. It is like a LIST. eg:

name = ["bob", "Sally", "Harry"]​

media

Show answer

Auto Play

Slide 1 / 30

SLIDE