Notebook

Notebook

A simple notebook.

Try


Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.

Watch this video to learn about the new concepts shown in the program:

Knowledge Organiser

x = [y for index in range(z)]

Declares a list x of z elements, assigning each element the value y.

Note that any variables or arrays/lists declared inside a sub program have a `local scope`.

Local scope data structures can only be accessed inside their subprogram.

Any variables or arrays/lists declared outside of a sub program have a `global scope`.

Global scope data structures can be accessed anywhere in the program including inside subprograms.

In some situations, assigning an array/list inside a subprogram will create a new local scope structure instead. To ensure you are always using a global scope, the keyword `global` is used. E.g.


def clear_notes():

global notebook

notebook = [“” for note in range(10)]


Global scope data structures should be avoided wherever possible because they can lead to conflicts in the code, especially when more than one person is working on a program and chooses to use the same identifier. Local scope data structures also allow for dynamic use of the memory. That means a program only uses the memory it needs when it needs it. Passing parameters into subprograms and returning values from them is considered a much better practice than using global scope.

Investigate


Questions to think about with this program to check your understanding:

Relation question

Line 60 is used to assign all the notebook enties to an empty string. Why can’t lines 28 and 29 be replaced with this statement instead?

I.e.

notebook = [“” for note in range(10)]

Instead of:

for index in range(len(notebook)):

notebook[index] = “”

REVEAL ANSWER

Notebook is a global scope data structure. Using this command inside a subprogram will declare a new local scope notebook instead of applying the change to the existing global notebook.

Approach question

Consider the `show_notebook` procedure. Why has an iteration by index been chosen instead of iteration by element?

I.e.

for index in range(len(notebook)):

Instead of:

for note in notebook:

REVEAL ANSWER

The index is needed in the output as well as the data itself. An alternative approach might be:


def show_notebook():

index = 0

for note in notebook:

print(index, “:”, note)

index = index + 1


In programming there are often many different ways of approaching the same problem. If the speed of execution and the memory usage is not affected, then the readability of the source code probably matters most. Programmers should aim to create easy to read code wherever possible because it makes debugging easier.

Make


Success Criteria

Change the program so that it:

  1. Includes a `delete_note` function that removes a single note.
  2. Includes a `move_note` function that swaps two notes.

Typical inputs and outputs from the program would be:

0 :

1 :

2 :

3 :

4 :

5 :

6 :

7 :

8 :

9 :


a. Add note

d. Delete note

c. Clear notebook

m. Move note

o. Order notes

:a

Note number: 6

Enter the note: Hello

0 :

1 :

2 :

3 :

4 :

5 :

6 : Hello

7 :

8 :

9 :


a. Add note

d. Delete note

c. Clear notebook

m. Move note

o. Order notes

:a

Note number: 3

Enter the note: World

0 :

1 :

2 :

3 : World

4 :

5 :

6 : Hello

7 :

8 :

9 :


a. Add note

d. Delete note

c. Clear notebook

m. Move note

o. Order notes

:a

Note number: 8

Enter the note: Loops reduce the amount of code.

0 :

1 :

2 :

3 : World

4 :

5 :

6 : Hello

7 :

8 : Loops reduce the amount of code.

9 :


a. Add note

d. Delete note

c. Clear notebook

m. Move note

o. Order notes

:m

Note number: 3

To be moved to…

Note number: 6

0 :

1 :

2 :

3 : Hello

4 :

5 :

6 : World

7 :

8 : Loops reduce the amount of code.

9 :


a. Add note

d. Delete note

c. Clear notebook

m. Move note

o. Order notes

:m

Note number: 3

To be moved to…

Note number: 5

0 :

1 :

2 :

3 :

4 :

5 : Hello

6 : World

7 :

8 : Loops reduce the amount of code.

9 :


a. Add note

d. Delete note

c. Clear notebook

m. Move note

o. Order notes

:d

Note number: 8

0 :

1 :

2 :

3 :

4 :

5 : Hello

6 : World

7 :

8 :

9 :

Help

Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.

Evaluate


Run your program several times with different inputs to check that your program has met the success criteria.

Craig'n'Dave logo

Craig ‘n’ Dave

Bett Awards 2024 Finalist