Title | Teaching | Exercises | Question | Objectives | Activity | Keypoints | ||||
---|---|---|---|---|---|---|---|---|---|---|
Next steps with Python |
15 |
1 |
|
|
|
|
Using the workshop list of commands - saved in the repository as Worksheet_Python.txt we can now start with some basic coding.
-
First let's start with some maths.
-
In a new cell, select 'code'. Remember that the code cell looks different to the markdown cell. How can you tell? Type the following inside the cell:
list(range(1, 20))
-
Click on 'Run' or use the shortcut Shift+Enter to execute the cell.
-
You have created a list of the range of numbers from 1 to 20. Woohoo!
-
In the next cell type the following:
a = list(range(1, 20))
-
Click on 'Run' or use the shortcut Shift+Enter to execute the cell.
-
This cell tells the computer that the list of numbers you created can now be called 'a'. This is called a 'value'.
-
In the next cell type the following:
x = sum(a)
-
Click on 'Run' or use the shortcut Shift+Enter to execute the cell.
-
This second instruction tells the computer to add each of those numbers in the list ('a') together and give that total a value of 'x'.
-
Now type the following in the next cell:
print(x)
-
-
Click on 'Run' or use the shortcut Shift+Enter to execute the cell.
-
This last instruction tells the computer to print the total of the list of numbers on the screen.
You just did some computing! Hooray! With these three instructions you have performed a 'sequence'. This is a list of instructions to be carried out in order and forms one of the backbones of programming.
Take a few minutes to change the range of numbers, and/or change the values and see what happens. Have a bit of fun with it - see if you can beat the computer with your amazing mental arithmetic skills. Or just be amazed at how fast it can be.