0% found this document useful (0 votes)
48 views

Lists, Array, Random

Here are the answers: 1. print(fruits[1]) 2. fruits[0] = "kiwi" 3. fruits.append("orange") fruits += ["orange"] 4. fruits.insert(1, "lemon") 5. fruits.remove("banana") del fruits[1] 6. print(fruits[0]) 7. print(len(fruits)) 8. "apple" in fruits 9. more_fruits = ['orange', 'mango', 'grapes']

Uploaded by

Alexander Brooks
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Lists, Array, Random

Here are the answers: 1. print(fruits[1]) 2. fruits[0] = "kiwi" 3. fruits.append("orange") fruits += ["orange"] 4. fruits.insert(1, "lemon") 5. fruits.remove("banana") del fruits[1] 6. print(fruits[0]) 7. print(len(fruits)) 8. "apple" in fruits 9. more_fruits = ['orange', 'mango', 'grapes']

Uploaded by

Alexander Brooks
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

PYTHON & PSEUDOCODE

ARRAYS, LISTS
RANDOM NUMBER GENERATION
CHAPTER 2A.4
VARIABLES AND MEMORY
• In programming languages a variable is used to
hold a particular piece of data. It could be of type
‘string’, ‘Boolean’, ‘integer’ etc. And then
depending on the type of data, a certain amount
of memory will be given to the variable to store
the data. We can consider memory in a computer
system to be like a huge bookshelf with each shelf
being a single memory location. So a ‘Boolean’
variable may require one of these ‘shelves’
whereas a string may need ‘more’.
DATA STRUCTURES - ARRAYS

There is a limitation to using only variables to hold data when programming.


When data relates to one another it is much more sensible to group that
data together.
Programming languages have such a structure to enable this to happen and
it is called an ‘Array’. When an ‘array’ is declared its size and data type
must be stated. This is so that a set amount of memory locations can be
created to store the data. Because of this, ‘arrays’ cannot grow is size
(although data in an array can be edited). Arrays are therefore known as
‘Static’.
DATA STRUCTURES- LISTS

Some languages however, have ‘List’ data structures. These are known
as ‘Dynamic’ arrays as they enable the grouping of data in programs
with the added advantage of being able to ‘grow’ in size. Behind the
scenes, the ‘List’ structure is in fact a ‘Static’ array. But when the
programmer wishes to add data to the list, if there is no room, the
language will create a new ‘static’ array in another part of memory of
twice the size and copy the data over. Lists therefore do not have to be
declared to begin with and can be appended to. The downside is that in
programs with lots of data, processing times can be longer.
Python doesn’t have arrays – it has lists!

In theory, they are NOT the same!

You must be aware of this for your exam!


1-DIMENSIONAL ARRAYS
If you want to store the names of 1000 students you will need a 1-dimensional
array. If you want to visualize this array it will look like the following picture

In the boxes you will place the names of the student. The numbers above is the
index.
Notice that the index in Python starts always from 0, while in pseudocode it ups
on you.
HOW CAN YOU CREATE AN 1-DIMENSIONAL LIST

StudentNames = [] # first we create an empty list


# we are going to continue the following code 10 times, since we have 10 students
for x in range(0,10):
name = input(“Enter the student’s name: “)
StudentNames.append(name) #you add this value into the list
print(StudentNames)

Run the code, to see the result


CHANGE ONE ELEMENT OF THE LIST
Let’s say that you have the list colors = [‘red’, ‘green’, ‘blue’, ‘orange’] and you
want to replace the second value (green) with the value ‘purple’. The index of
this is 1, thus:
colors[1] = ‘purple’
You can either output the whole array – list
Print(colors)
Or just one element
Print(colors[1])
2-DIMENSIONAL ARRAYS
If you want to store the student name and his/her grade, under one identifier
then you should use a 2 dimensional array. Thus your code will be:
students = [[],[]] # first we create an empty list
# we are going to continue the following code 10 times, since we have 10 students
for x in range(0,10):
name = input(“Enter the student’s name: “)
students[0].append(name) #you add this value into the first list
grade = input(“Enter the student’s grade: “)
students[1].append(grade) #you add this value into the first list
print(students)

Run the code to see the result


HOW TO ACCESS PARTS OF THE LIST
2D_Array = [[23, 3, 1, 4] , [5, 22, 6, 3]]
If you want to access the whole sub list you type the name of the list and into
square brackets the index of the of the sub list
2D_Array[0] will evaluate [23, 3, 1, 4]
2D_Array[1] will evaluate [5, 22, 6, 3]
If you want to access an element of the list the you should first declare in which
sub list it is and then the index of this element
2D_Array[0][1] will evaluate 3
2D_Array[1][2] will evaluate 6
There is no limit to the amount of dimensions an array can have.
This is how to visualise a 3D array?
LIST & ARRAY METHODS
Method Description
append() Adds an element at the end of the list, eg list.append(element)
Clear() Removes all the elements from the list, eg list.clear()
Copy() Returns a copy of the list, eg list.copy()
Count() Returns the number of elements with the specified value, eg list.count(element)
Extend() Add the elements of a list to the end of the current list, eg listA.extend(listB)
Index() Returns the indexof the first element with the specified value, eg
list.index(element)
Insert() Adds an element at the specified position, eg list.insert(position, element)
Pop() Removes the element at the specified position, eg list.pop(position)
Reverse() Reverses the order of the list
GENERATE A RANDOM INTEGER

In many cases, you will need to generate a random number (integer or real). In
order to do this, you will need first to import the random library. For instance, if
you want to generate a random integer in range 1 to 10 (now both boundaries
are included), you should type:
import random # you import the library
num = random.randint(1,10)
print(num)
GENERATE A RANDOM REAL NUMBER
On the other hand if you want to generate a random number, you should type:
import random
num = random.rand()
print(num)

By default the range of the random number will be from 0 to 1. if you want to generate
a float number with a different range, then you should use a mathematical function to
achieve this range, for instance if you want to generate a random float number in the
range 5 to 8 then:
import random
decPart = random.rand()
intPart = random.randint(5,8)
num = intPart + decPart
print(num)
CHOOSE A RANDOM ELEMENT OF THE LIST
You can choose a random element from a given list either using the index of the array,
or by using the random function directly into the list. Below you can see both methods.
import random
colors = [‘red’, ‘green’, ‘blue’, ‘orange’]
colorChoice = random.choice(colors)
print(colorChoice)

Or
import random
colors = [‘red’, ‘green’, ‘blue’, ‘orange’]
index = random.randint(0, len(colors)-1)
colorChoice = colors[index]
print(colorChoice)
HOMEWORK

1. Study chapter 2A.4, pages 36-39


2. Study your notes
3. Answer questions 15 & 16 on 38, questions 17 & 18 on page 39
4. Answer the questions on the next page
fruits = [“apple”, “banana”, “cherry”]
1. Print the second item in the list
2. Change the value from “Apple” to “kiwi”
3. Add ‘orange to the list (two ways)
4. Insert ‘lemon’ as the second item in the list
5. Remove banana from the list (2 ways)
6. Print the first item of the list
7. Print the number of the items in the list
8. Check is ‘apple’ is present in the list
9. Add more_fruits = [‘orange’, ‘mango’, ‘grapes’]

You might also like