Guide to getting your child started on Python
A guide to getting your child started with Python. The content below is adapted from our Python Masterclass modules. Our students typically learn these concepts at Primary 5.
To learn more visit: Python Masterclass.
What software do you need?
You only need one of the following common tools to get started with Python. We recommend using Google Colab since it is web-based and you do not need to install any software.
Google Colab (https://colab.research.google.com/)
Microsoft Visual Studio (https://code.visualstudio.com/)
Python Interpreter (https://www.python.org/downloads/)
All you need to do is type the python code into the and hit run it by clicking the icon to the left of the section of code. The output is given immediately below. Yes, it is that simple.
String Slicing
String Slicing an important concept in programming in Python as it allows you to extract (cut out) data from a string hence the term String Slicing.
In Python, characters in a string are known as elements. The first element always has an index of 0 and the index increases by 1 going from left to right.
In the string “HELLO”,
the first element is “H” at index of 0,
the second element is “E” at index of 1,
the third element is “L” at index of 2
the fourth element is “L” at index of 3
the last element is “O” at index of 4.
Slicing a single element
String = “HELLO” print(String[0])
Try with numbers from 0 to 5. Note that if the integer within the square brackets is greater than the number of elements within the string, an error is returned.
Slicing more than one element
At times we need to slice more than one element. Perhaps we like to get the middle 3 elements from the string “HELLO”, this would be “ELL”.
This is done through using a colon(:) to indicate which index we like to start and stop in the format variable[start:stop]. It is crucial to remember that the stop index is non-inclusive. This means that to return “ELL” we need to use [1:4]. String = “HELLO” print(String[1:4])
Try the following tasks yourself:
If Statements and Comparison Operators
If statements are essential when programming, it allows certain parts of the code to be executed only if certain conditions are met. This is the basic building block of coding logic and algorithms. If statements are used in any programming language, even in block based low-code/no-code platforms.
The expression is evaluated in a true/false (boolean) context. Notice that lines 1-4 are indented using tab, these represent the blocks of code that are executed only if the expression is true. If the expression is false, the block of code is skipped over. Execution proceeds with line 5 thereafter.
Comparison operators are used within the expression to test if a specific condition is true or false. Python supports these logical conditions from Mathematics:
If you type the following into Google Colab and run the code. You will notice that X being 50 is indeed larger than 20.
Try the following tasks yourself:
If-Else Statements
If-else statement is an extension of the if statement. An else: block can be added to any if statement. The if block of code is executed if the expression is true, while else block of code is only executed if the expression is false.
Try the following tasks yourself:
Application Question
Similar to math, learning the concepts and applying them to solve problems are entirely different. This is where critical thinking and problem-solving skills come into play.