Menu

Pseudocode

Variables and Constants

An assignment gives a value to a variable. The variable on the left of the assignment receives the result of the expression on the right, which is worked out first. For example, points = 6 * 7 stores 42 in points.

The = sign in an assignment does not mean "is equal to" — it means "store this value". A variable can be reassigned while a program runs, so its value can change. In a game, lives = 3 might run at the start; later, after a hard level, lives = 1 runs, and lives now holds 1. Only the most recent value is kept.

Constants

A constant is given a value that does not change while the program runs. You use a constant for something fixed, such as the length of one lap of a running track or the number of points a badge is worth. By convention, a constant in Python is written in capital letters with underscores, for example MAX_POINTS, so anyone reading the program can see the value should not be altered. Python does not enforce this, but the capital letters are a clear signal.

Using a constant makes a program easier to read and change, because the value is set in one place. For example, if POINTS_PER_BADGE = 10 is defined once, every calculation can use it. If the value ever needed updating, you would change that one line rather than hunting through the program for every 10.

# constant: set once, used in every calculation
POINTS_PER_BADGE = 10
week_one = 74
week_two = 38
badges_one = week_one // POINTS_PER_BADGE
badges_two = week_two // POINTS_PER_BADGE
print("Week 1 badges: " + str(badges_one))
print("Week 2 badges: " + str(badges_two))
print("Points per badge: " + str(POINTS_PER_BADGE))

Output:

Week 1 badges: 7
Week 2 badges: 3
Points per badge: 10

Practise this