Getting started with Python

There are many online resources for beginning in python. Here are a few

https://wiki.python.org/moin/BeginnersGuide/NonProgrammers – Python’s own suggestions for getting started
http://www.letslearnpython.com/learn/ – An online tutorial for kids used at PyCon each year
http://kata.coderdojo.com/wiki/Python_Path – Some international CoderDojo resources

At Kenmore CoderDojo Term 3 2017 we are going to be using Python 3. We will also be using Python with Minecraft. For full setup instructions, refer to https://www.triptera.com.au/wordpress/2017/06/02/coderdojo-minecraft-with-python-setup/

Python shell

At our first session, we installed Python 3.6.2 and used the IDLE development environment for running python in a shell.

Python can use operators +, -, *, /, //, % and ** to preform mathematical operations

>>> 5 + 3
8
>>> 5 - 3
2
>>> 5 * 3
15
>>> 5 / 3
1.666666666667
>>> 5 // 3
1
>>> 5 % 3
2
>>> 5 ** 3
125

+ addition
- subtraction
* multiplication
/ division (decimal)
// division (integer)
% modulo (remainder from division)
** to the power of

Variables

To store data in memory, create a name for the memory location, and assign it using the = sign. These memory locations are called variables because you can change the data in them. Type the variable name by itself to see the value stored in it. You can use variables in mathematical equations.

>>>speed=50
>>>duration=3
>>>speed
50
>>>speed * duration
150

Python program

When using the shell the commands are not saved. By putting the commands in a file they can be run over and over again without having to be retyped. In IDLE choose New File from the File menu and type the following

print(3)
print(2)
print(1)
print("blast off")

Save this file from the File menu. From the Run menu select Run module F5. Every time you run it you will see the same output in the shell window.

3
2
1
blast off

Using loops to save typing

Loops are sections of code which are run multiple times. while loops will keep looping until a specified condition is True. The indented lines indicate which part of the code is included in the loop.

x = 5
while x > 0:
    print(x)
    x -= 1
print("blast off")

The condition is x > 0 which is True for 10 loops and then becomes False when the value of x is zero.
The command x -= 1 is a shorthand for telling python to reduce the value of x by 1 every time it is run.
This program gives the following output every time it is run.

5
4
3
2
1
blast off

Only one line needs to be changed to start the countdown from a different number.

Categories: CoderDojo