Python KPRIDE: Arrays and Lists
star
star
star
star
star
Last updated 7 months ago
12 questions
Keywords
- Array: A collection of similar data of a fixed length. E.g. myArray = ["Apple", "Banana", "Cherry"]
- List: A collection of different data types of any size. E.g. myList = ["Apple", "B" , 3.142, ]
- Data Structure: A storage that is used to store and organize data (such as Arrays)
- One-dimentional Array: An array that holds a "row" of data
- two-dimentional Array: An array that holds a "rows" and "columns" of data
https://www.w3schools.com/python/python_arrays.asp

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] )
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] )
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] )
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)
What is the output:
myArray = ["Apple", "Banana", "Cherry"]
print (myArray)
1
What is the output:
myArray = ["Apple", "Banana", "Cherry"]print (myArray[0])
What is the output:
myArray = ["Apple", "Banana", "Cherry"]
print (myArray[0])
1
What is the output?
myArray = []myArray.append("A")print (myArray)
What is the output?
myArray = []
myArray.append("A")
print (myArray)
1
What is the output?
cars = ["Ford", "Volvo", "BMW"] cars.remove("Ford")print(cars)
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])
What is the output?
cars = ["Ford", "Volvo", "BMW"]
cars.remove("Ford")
print(cars[0])
Investigate
- Look at the code on the right.
- Run it and see what it does.
- Click on the pencil icon at the top to see the code again.
FIRST EXAMPLE:
- Run the program and see what it does
- Add some items into myShoppingList = [], so that it already starts with items.
- What does while removeItem != "": do? How does this stop?
- Look at this code for items in myShoppingList: What is different about this for loop?
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
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.
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.
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
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