Python revision guide

By Interwoven Maths

Practise Python View all questions Back to Computer Science

Everything you need to know

Python is a text-based programming language. This topic covers the statements needed to write a working program in it.

Writing statements in Python

print() displays text and input() reads what the user types. Anything read by input() arrives as a string, so int() is needed before it can be used in arithmetic.

A single = assigns a value; == tests whether two values are equal, and != tests whether they differ. A # starts a comment.

Decisions use if, with else running when the condition is false. A for loop repeats a set number of times and while repeats while a condition holds. range(3) gives the values 0, 1 and 2.

Indentation shows which statements belong inside a loop or a decision. The common types are int for whole numbers, str for text and bool for True and False.

Lists

A list stores several values under one name. Indexing starts at 0, so [10, 20, 30][1] gives 20.

append() adds an item to the end and pop() removes and returns the last one. Slicing takes part of a sequence, so "cat"[1:] gives "at". len() gives the number of items or characters.

Functions

def defines a function. return sends a value back to whatever called it. A value supplied to a function when it is called is an argument.

import loads a module so its functions can be used.

Back to Python practice ยท View all Python questions