Programming Examples - Python

These are example Python programs that I've written as demonstrations of particular programming techniques or types of solution for both KS3 Computing and GCSE Computer Science.  They are organised and categorised by technique/concept.  There are further examples in the Python course and Microtips playlist on the Computing and ICT in a Nutshell YouTube channel.  The idea is that students can see how the various methods can be used as part of a larger program after learning the basics of programming.

Most programs open in Basthon, which is a French IDE that doesn't require you to register. Click the Exécuter button to run them - and make sure that you don't translate the page! Where a program doesn't run in Basthon (e.g. because it uses the turtle) then the link opens the code, which you can copy and paste into your own IDE.

The sections are:

NB. Some examples appear twice (possibly with different descriptions) because they use multiple techniques, and some examples with similar names might approach the same problem in different ways.

Repetition with For

Repetition with While

Nested Loops

Lists and Tuples

Multi-Dimensional Lists

Dictionaries

Character/String Manipulation

Casting

Boolean Logic

Random Numbers

Modular Arithmetic (and other Mathematical Techniques)

Sorting and Searching

Number Bases

Compression Techniques

Drawing with Turtle

Reading and Writing Files

Recursion

Object-oriented Methods

Larger Programs/Projects

You'll notice that I tend to use lists or tuples in my code to avoid the need for multiple IFs - I explain the technique in this article, and also demonstrate how it can be used to improve a common Magic 8 Ball program here.  You can also make use of True and False to avoid the need for a lot of IFs in your code.  Finally, there is a video on recursion, in which I explain how the factorial, palindrome and prime factorisation programs work.

If you prefer to use a local IDE, you can download programming examples in BASIC, JavaScript and Python.

* These programs are also solutions to OCR Coding Challenges.