Python Variables for Beginners

Learn Python variables through games: store scores, lives and player names, change them with +=, and avoid the most common beginner mistakes.

A variable is a name for a value your game needs to remember: the score, the lives left, the player's name, whose turn it is. You create one with = and can change it whenever you like.

example 1 · press Run, then edit and run again

Types of values

Swapping turns

Two-player games flip a variable every turn. A conditional expression does it in one line:

example 2 · press Run, then edit and run again

Good names

Use lowercase words joined by underscores: high_score, shots_left. Names can't start with a number or contain spaces, and Python cares about capitals: Score and score are different variables.

💡 A NameError usually means a typo, or using a variable before the line that creates it has run.

Games that use Variables

Practise this with 7 of the 20 games:

Frequently asked questions

What is a variable in Python?

A name that points to a value. score = 0 creates the name score and stores 0 in it. Assigning again replaces the value.

What does += mean in Python?

score += 5 is short for score = score + 5. There's also -=, *= and /=.

More Python concepts