Introduction to Python with PRIMM

Featured course

Andy Colley

>_

Python · PRIMM

Predict, run, investigate, modify, make.

Your first steps in Python, from printing a single line of text to writing your own sub programs. Every chapter puts you in the driver's seat: predict what a piece of code will do, run it to find out, take it apart, change it, and then build a program of your own from scratch.

For complete beginners: no programming experience needed.

Covers every Python programming skill in the OCR GCSE Computer Science specification (J277).

What you'll learn

  1. Computers, programs and algorithms
  2. Output: putting text on the screen with print
  3. Variables: storing data and joining text together
  4. Input: reading and using what the user types
  5. Calculations: arithmetic operators and casting
  6. Exponentiation and modulo: powers and remainders
  7. Selection: making decisions with if and else
  8. Selection: choosing between many options with elif
  9. Iteration: repeating code with while loops and counters
  10. Iteration: count controlled loops with for and range
  11. Lists: storing many values under one name
  12. Random numbers: letting the computer pick
  13. String manipulation: slicing, case and ASCII codes
  14. 2D arrays: data in rows and columns
  15. Reading files: using the data in a text file
  16. Writing files: saving data to a text file
  17. Sub programs: splitting a program into named functions

About the author

Andy Colley has taught computing in secondary schools since 2004 and is Director of Computing at a school in Greater Manchester, UK, teaching Computer Science to students aged 11 to 18. Andy is a CAS Master Teacher with Computing at School, the UK's network for computing teachers, and a lead practitioner for teaching and learning, and has trained other teachers in PRIMM, the approach this course is built on.

Find Andy on Bluesky.

Learning path

What's next in this course
Programming basics

How a computer turns input into output, and what makes a set of instructions an algorithm. No code yet: this chapter sets up the vocabulary the rest of the course uses.

Status Status Type Title
Computers and programs
Algorithms

Printing text to the screen with print, and the syntax rules that make it work. Four Make exercises build the same joke program under increasingly tight constraints.

Variable assignment

Storing data in named variables, replacing that data while the program runs, and joining strings together with concatenation.

Status Status Type Title
Variable assignment
Concatenation
Predict and run
Investigate
Modify
Make
Make with sentences
Make in one sentence

Reading what the user types with input, storing it in a variable, and building a response around it.

Status Status Type Title
Input
Predict and run
Investigate
Modify
Make

Arithmetic operators and casting between text and numbers, so a program can calculate with its input instead of only echoing it.

Status Status Type Title
Arithmetic operators
Casting
Predict and run
Investigate
Modify
Make
Make a perimeter calculator
Make a volume and surface calculator
Make a tip calculator

Exponentiation and modulo

The remainder operator % and the power operator **, and the problems each one solves: leftovers, splitting seconds into minutes, squares and cubes.

Status Status Type Title
Remainders and powers
Predict and run
Investigate
Modify
Make
Make a cupcake packer
Selection and iteration

Selection with IF and ELSE

Boolean conditions and two-way decisions, letting a program take a different path depending on what it finds.

Status Status Type Title
Boolean conditions
Selection
Predict and run
Investigate
Modify
Make
Make with grades

Selection with IF, ELIF and ELSE

Chaining conditions with elif so a program can choose between more than two options, and why the order of the branches decides the outcome.

Status Status Type Title
Selection with ELIF
Predict and run
Investigate
Modify
Make a room finder
Make the insult-o-matic
Make a time reminder

Iteration with WHILE

Repeating code with while loops and counters, including loops that keep asking until they get the answer they want.

Status Status Type Title
While loops
Counters
Predict and run
Investigate
Modify
Make
Make with attempt counting
Make a countdown
Make the cubed sequence

Iteration with FOR

Repeating code a set number of times with for loops and range. Count controlled iteration: the number of repetitions is known before the loop starts.

Status Status Type Title
For loops
The range function
Predict and run
Investigate
Modify
Modify with even numbers
Make
Make a repeater
Make a total
Working with data

Storing many values under one name, reading them back by index, and changing a list while the program runs.

Status Status Type Title
Lists
Changing a list
Predict and run
Investigate
Modify
Make
Make a fruit picker
Make a name replacer

Random numbers

Letting the computer pick: random whole numbers with randint and randrange, a random item from a list with choice, and games built on them.

Status Status Type Title
The randint function
The choice function
The randrange function
Predict and run
Investigate
Modify
Modify with repeated guesses
Modify with playing cards
Make
Make with close guesses

String manipulation

Working with text: its length, upper and lower case, cutting out substrings with slicing, looping through the characters, and converting to and from ASCII codes.

Status Status Type Title
Length and case
Substrings
Looping through a string
ASCII codes
Predict and run
Modify
Modify with ASCII codes
Make
Make a vowel counter
Make initials

Data with rows and columns, stored as a list of lists: reading and changing one item with two indices, and visiting every item with nested loops.

Status Status Type Title
2D arrays
Looping through a 2D array
Predict and run
Investigate
Modify
Modify with step counts
Make
Make with averages
Files and sub programs

Reading files

Opening a text file and reading it into a program, all at once with a for loop or one line at a time with readline, and using what it holds.

Status Status Type Title
Reading a file
Reading one line at a time
Predict and run
Investigate
Modify
Make
Make a pass counter

Writing files

Saving data from a program into a text file: write mode replaces what was there, append mode adds to the end, and every line ends with a newline.

Status Status Type Title
Writing to a file
Appending to a file
Predict and run
Investigate
Modify
Modify with the grade on one line
Modify with validation
Make
Make a results file

Splitting a program into named subroutines, passing arguments into them, and tracing the order in which they run.

Status Status Type Title
Sub programs
Functions and arguments
Predict and run
Investigate
Modify
Make
Trace the execution