Arrays - a data structure that can store a group of data values, of the same type, under the same name
They are the most useful when you have lots of related data that you want to store and it doesn't make sense to use separate variables - eg. names of people in a class
Element - each piece of data in an array - each element can be accessed using its position (or index) in the array
Creating arrays:
Python - people = {"Mark", "Adam", "Shelly"}
Pseudocode - people <- {"Mark", "Adam", "Shelly"}
2.. Retrieving elements:
Remember in pseudocode, positions are numbered starting at 0 and in python, positions are numbered starting at 1
Python - print people[1]
Pseudocode - OUTPUT people[0]
3. Changing elements:
Python - people[1] = "Tami"
Pseudocode - people[0] <- "Tami"
4. Number of elements:
Python - len(people)
Pseudocode - LEN(people)
Combining these array functions with FOR loops, will give you a systematic way of accessing and changing all of the elements in an array
Diapositiva 2
Data Type: Arrays - 2 Dimensional Arrays
Creating arrays:
trees <- [['oak', 'ash'], ['beech', 'cedar'], ['pine', 'elm']]
Positions of elements:
The position of an element is usually written [a][b] or [a,b], where a represents the separate list and b represents the position inside the individual lists.
trees[0][0] - oak
trees[0][1] - ash
trees[2][0] - pine
Length of an array or a list in the array:
Length of an array says how many individual lists there are in the array = LEN(trees) - 3
Length of a list in an array says how many elements there are in the list = LEN(trees[1]) - 2