All homework for next week is now on Classroom.
Author: Mr Stratton
Computing teacher and a PT at Coltness High School.
AH – 2D Array Recap
It took us a little while in class today to remember how 2D arrays work, so I thought I might go over it.
A 2 dimentional (2D) array is an array of arrays. So [[1,2,3,4],[5,6,7,8],[9,10,11,12]] is a 3 item array where each item is a 4 item array. This can be written as
[[1,2,3,4],
[5,6,7,8],
[9,10,11,12]]
You might notice that it looks like a grid (or matrix), this is where the 2D comes in to the name. In python we can create the array and assign it.
In the code above you can see the the individual elements of the 2D array are accessed using a pair of square brackets [][]. Using the image below we can see the [1][2] is number 7, as demonstrated in the program above.
We can use a loop to display each value in the 1st column.
Or a nested for to display each item in each array
Homework
AH – Scholar Homework Session
The next Advanced Higher Computing Science homework session from Scholar will be on Wednesday 9th November.
The topic is “Stacks, Queues and Linked Lists”.
The session starts at 6.30pm.
Syndication
Posts from this blog will shortly be appearing on the Business ICT blog.
Higher – Homework for Monday
AH – HW for Monday
AH – Binary Search
Binary search is a more efficient way of searching a sorted list.
The Python function given gives you an example of how this might be implemented. It either returns the found position or -1 meaning that the value has not been found.
There are other implementations available that make use of different constructs including recursion.
Nat 5 – Our first program
Today in class we used TRACS to look at the program from last time, so we could better understand how it works and how the Python translator works.
- A compiler changes all the source code (python) to machine (object) code. The machine code is then executed after all lines have been complied.
- An interpreter changes one line of source (Python) code to Machine (object) code, this is then executed immediately.
The class was then set a new program to create.
Task
Write a program that takes in 3 numbers, calculates the total, then works out the change from a given amount.
Test data
2.99, 1.50, 3.00
10.00
N5 Python – Introduction
Our 1st Python program makes use of a number of new constructs and variables.
# – Internal Commentary, anything after the # on the same line is ignored by the translator
= (Assignment) – values are assigned to the variables using an equals sign.
input() – a string is captured from the keyboard using the input() function
int() – this function changes the datatype of the given variable to an integer (Whole number)
Expression – The expression is the right hand side of the =, this is evaluated and any calculations performed, the results are then assigned to the variable on the left hand side of the equals
print() – This function displays a string. The “,” is used to concatenate strings and add a space between them.