﻿

# Learn to Program in Python or BASIC

This is a set of free resources aimed at helping students learn to program in either Python or BASIC.  It is primarily intended to be a programming scheme of work for teachers of KS3 Computing or GCSE Computer Science classes, but could also be used for self-study or revision in conjunction with the videos on the Computing and ICT in a Nutshell YouTube channel. Once you have mastered the basics you can develop your skills with the practice tasks.

In KS3 programming lessons, I teach students to "program" - I don't like the term "coding" because it suggests that the syntax is more important than the concepts (and also because "coding" has other meanings in relation to databases and encryption and could confuse students).

## Computational Thinking

The real reason that we teach programming in schools is not to provide vocational training for the software industry, but to give students a grounding in general problem solving skills that they can apply in non-programming contexts.  This is called computational thinking.  For each task they are given, I encourage them to consider that:

• the method is more important than the result - think about how you perform the task rather than what the solution looks like.
• if you strip away the superfluous detail, it might be essentially the same as a problem you've solved before.  Could you use the same solution?  Solving a Sudoku puzzle, for example, is essentially the same as creating a school timetable.
• it's better to think ahead and consider how you could generalise the solution - write it to be flexible from the start
• we should aim to make the program more efficient or more concise.

We can apply these ideas to a simple program, e.g. if asked to print the two-times table, then we wouldn't just print 2, 4, 6, 8, etc., we'd think about how those answers are generated, count from 1 to 12, multiplying by two as we went along.  It's essentially the same as any counting program, e.g. Ten Green Bottles (see below).  If we stored the 2 in a variable and used that to multiply, then we could easily generalise the program to display other tables.  Using a for loop wouldn't really make the program more efficient (in the sense that it wouldn't be quicker or use less memory), but it would be more concise.

We can also apply this to non-programming tasks, e.g. making a web-page.  Thinking about the structure is more important than the appearance - how does a rollover work; how do we make a drop-down menu, etc?  Could we make a page that appears equally well on all devices (by using a fluid layout and a careful choice of fonts)?  Could we improve the page by using efficient file formats, minimising the use of unnecessary images and http requests, etc?

Still not sure that programming is for you? If you are interested in programming, but think would like to start with something simpler, why not try Scratch? I have created some Scratch examples using interesting and/or useful programming techniques.  Programming is a compulsory part of the Computing National Curriculum at KS3 in all state schools in England - you are required to learn two different languages, at least one of which must be text-based. You can also use programming to investigate other areas of the curriculum.

## Key Programming Concepts

Below are links to presentations explaining the key programming concepts. I have written an article describing my top ten programming techniques for older students, but these are the ones that I start with at KS3. They also include code examples in BASIC or Python.   The list may expand as the course is extended, so come back soon if there's something else you'd like to see here. There playlists for programming in Just Basic, Python, Scratch and Visual Basic on the Computing and ICT in a Nutshell YouTube channel.

• Lesson 1 - Introduction, PRINT and INPUT [BASIC / Python]
• Lesson 2 - Variables [BASIC / Python]
• Lesson 3 - Repetition [BASIC / Python]
• Lesson 4 - Decisions [BASIC / Python]
• Lesson 5 - Random Numbers [BASIC / Python]
• Lesson 6 - Introduction to Arrays (Lists for Python) [BASIC / Python]
• Lesson 6½ - Using Lists and Tuples for reference [Python]
• Lesson 7 - Text Functions [BASIC / Python]
• Lesson 8 - Character Functions [BASIC / Python]
• Lesson 9 - Numerical Functions [BASIC / Python]
• Lesson 9½ - Nesting (ifs and loops) [Python]
• Lesson 10 - Functions and Procedures [BASIC / Python]
• Lesson 11 - Library Functions [Python]
• Lesson 12 - File Handling [Python]
• Extension - Object-Oriented Programming in Python [Text / Video]

There are wider computing concepts spread throughout the site, but for interactive examples aimed at younger students, have a look at the Interactive section.

You might also want to cover general programming ideas such as:

Here are some of the programming challenges that students could approach to begin with - there are versions of most tasks for BASIC and Python:

• Hello World - installing the IDE and writing your first program [BASIC / Python]
• Variables - understand what variables are and how we can use them [BASIC / Python]
• Times Tables - using a loop and user input and formatting the output [BASIC]
• Times Table Tester - using random numbers and variables to record the score [BASIC / Python]
• Reverse-a-Word - using string manipulation techniques to reverse a word [BASIC / Python]
• Anagrams - using string manipulation techniques to create anagrams [BASIC]
• Caesar Shift Cipher - using string manipulation techniques to create and decipher encrypted messages [BASIC / Python]
• Using Lists in Python [Python]
• The Twelve Days of Christmas - using nested loops and arrays to print the lyrics to the song [BASIC / Python]
• Rock, Paper, Scissors - using random numbers and arrays, lists or tuples to recreate the classic game [BASIC / Python]
• Penalty - another game based on lists and random numbers [Python]
• Phonetic Alphabet - using ASCII codes and an array, list or tuple to spell out words in the phonetic alphabet [BASIC / Python]
• Lottery - using loops and arrays to create six random numbers from 1 - 49, in order, with no duplicates [BASIC / Python]
• Teams - using lists/tuples and modular arithmetic to put students into teams [Python]
• Functions - write a numerical function and use it to test a range of values [Python]
• Address Book I - take information from the user and save it into a file [Python]

Most tasks have extensions, and we encourage students to enhance their programs beyond the bare minimum, where possible.

If you enjoy attempting tasks like these, then why not register for Project Euler or the Ubuntu Forums for some more challenges?

## Examples

I have created a set of Python examples organised by programming technique/construct, which I use to demonstrate concepts to students of KS3 Computing and GCSE Computer Science.

You can download .zip files containing examples and sample solutions to the above tasks done in BASIC and Python.  There are also Python examples on my replit.com page.

### Programming the Curriculum

Why not link with other areas of the new Computing National Curriculum by getting your students to create programs to demonstrate, or test their understanding of key Computing concepts? You can create programs for converting numbers from denary to binary (using bitwise Boolean logic or otherwise) or binary to denary, for selection sort and merge sort, searching, to demonstrate the use of Exclusive-OR for encryption, bitwise manipulation, compression with Run-Length Encoding, Huffman Coding or dictionary encoding, or to help them think about the storage of text by capitalising text or creating anagrams or shift ciphers.  There are also some examples using Python with Logo - snowflakes for Christmas and spider webs for Halloween - plus lots more.  Links above are to replit.com, but you can also download examples for BASIC or Python.

## Learn to Program in Python

I think that BASIC is a better first programming language to learn than Python.  Python's a bit quirky - things like for loops and arrays are different from nearly every other language I've used - and you need to be careful not to teach anything that's too "Pythonic" (such as list comprehension).  As there's no need to declare variables or be careful with types, and it doesn't support things in specifications such as arrays, constants and passing by reference/value, it's probably not the best language for GCSE and A level students, either. Creating a Windows form-based application is particularly fiddly compared with something like Visual Basic, or even HTML and JavaScript

Finally... Python is very, very slow.  If you compare my Sudoku solving program written in JavaScript and Python, using this 17-clue puzzle from Wikipedia, then (on my computer) the JavaScript version running in the Edge browser takes about 50 seconds, the Visual Basic version takes about a minute, but the Python version (interpreted locally with py.exe rather than in replit.com or IDLE) takes 36 minutes!

However, I am aware that a lot of schools use it, so I have adapted a set of the above presentations and example programs so that students can use Python.  You can download all of the presentations and some example programs below:

Both sets of files are in a .zip file / compressed folder. There is no presentation on arrays for Python - it has been replaced by the one on lists and tuples.

There is also a set of examples available on my replit.com page - click on the heading Andrew's Public Repls to see the full list - and, finally, I'm slowly working my way through the OCR Coding Challenges.

## JavaScript Examples

There is no JavaScript course, but I have created a zipped file of JavaScript examples that can be used to demonstrate key techniques to students, particularly changing text and colours on the page to display state. Examples include choosing lottery numbers, a traffic light, a die model, a seven-segment display, a Simon game, a Sudoku solver and rotation. There are also more generic examples in the JavaScript section of this site, and examples of how you can add animation to web-page without using any JavaScript at all.

## Video Courses

I am developing video courses on how to learn to program on the Advanced ICT YouTube channel:

For older students I have also written articles on the use of modular arithmetic, using arrays for selection, creating functions and object-oriented programming in Python as well as my top ten programming techniques and a page on recursion. There is also a Python guide for GCSE (aimed at OCR as it contains some ERL).