Variables, calculations and decisions
Learn about variables, math operators, if/elif/else conditions, and logical operators.
What You'll Learn
- Understand what variables are and how to use them to store data
- Use math operators for calculations
- Write if/elif/else conditions to make decisions in your program
- Combine conditions using the logical operators and, or, not
In any RPG, your character has stats: health points, attack power, level, gold coins. These numbers change constantly, you gain experience, lose health in battle, buy a new sword. Well, in programming we do exactly the same thing, except we use variables to keep track of all these values.
In this lesson you will learn how to create variables, do calculations with them, and write code that makes decisions, just like a game decides whether your attack hits or whether you have enough gold to buy armor. By the end of the lesson, you will be able to build the basic logic of a mini RPG.
Get ready to create your first character in code. Let's begin!
Your hero from the Python Kingdom needs stats: health points, attack, defense, gold. In this lesson you will build the character sheet for your RPG game. You will use variables to store the hero's stats, if/elif/else to implement class selection, and a shop where the hero can buy equipment.
Variables
A variable is like a box with a label on it. You put a value inside and give it a name so you can find it later. In Python, you create a variable simply by writing its name, the = sign, and the value you want to store.
In the example above we created 5 variables. Each has a different data type:
"Aldric"is a string (str), meaning text, always between quotes120and350are integers (int), numbers without decimals7is also anintTrueis a boolean (bool), can only beTrueorFalse
There are also decimal numbers, called float:
To see what you stored in a variable, you use the print() function:
You can also display multiple things on the same line, separated by commas:
An important thing: variable values can change. That is why they are called "variables". If your hero takes damage, you subtract from their HP:
Math operators
In RPGs everything is based on calculations: how much damage you deal, how many coins you have left after buying something, how much experience you need until the next level-up. Python gives you all the operators you need.
Here are the basic math operators:
| Operator | What it does | Example | Result |
|---|---|---|---|
+ | Addition | 100 + 3 | 103 |
- | Subtraction | 100 - 3 | 97 |
* | Multiplication | 100 * 3 | 300 |
/ | Division | 100 / 3 | 33.333... |
// | Floor division | 100 // 3 | 33 |
% | Remainder (modulo) | 100 % 3 | 1 |
Let's see some practical examples with an RPG theme:
Now let's see the more special operators, floor division (//) and modulo (%):
// tells you how many times 12 fits into 50 (4 times), and % tells you how much is left over (2 coins).
Conditions (if / elif / else)
Up until now, our code ran line by line, top to bottom, without "thinking". But in a game you need decisions: if HP reaches 0, the character dies; if you have enough gold, you can buy a weapon; if you are level 10, you unlock a new ability.
This is where conditions come in. The basic structure looks like this:
If we also want to handle the case where the player is alive, we add else:
When you have multiple options, you use elif (short for "else if"):
Python checks conditions from top to bottom. The first condition that is true gets executed, and the rest are ignored. If none are true, the else block runs.
Indentation
You may have noticed that after if, elif, and else, the code that belongs to that block is shifted to the right by 4 spaces. This is called indentation and it is mandatory in Python.
Both print lines are indented by 4 spaces. This means they both belong to the if block. If you remove the spaces, Python will throw an error:
Other programming languages use curly braces {} to mark code blocks. Python uses indentation, which makes code cleaner and easier to read. But you must be consistent, either use 4 spaces or a tab, but do not mix them.
Logical operators (and, or, not)
Sometimes a single condition is not enough. Maybe you want to check if the player has both a high enough level and enough gold. Or maybe you want to check if they have either a shield or armor. That is what logical operators are for.
and - both conditions must be true
Both conditions are true (8 >= 5 and 200 >= 150), so the success message is displayed.
or - at least one condition must be true
Even though you do not have a shield, you have armor, and that is enough when using or.
not - reverses a condition
not False becomes True, so the first block runs.
You can combine multiple logical operators in a single condition:
The difference between = and ==
This is one of the most common confusions for beginners, so let's clear it up once and for all.
A single = means assignment, you give a value to a variable:
Two equals == means comparison, you check if two values are equal:
If you accidentally write = instead of == in an if, Python will give you an error. Remember: one equal sets, two equals asks.
Variables store data, operators do calculations, and conditions make decisions. With these three tools you can build the logic of any game. Remember: one equal (=) sets a value, two equals (==) compares values.
Exercise : Hero Sheet from the Python Kingdom
Exercise : Hero class selection
Exercise : Kingdom Shop
Exercise : Attack with critical hit
Exercise : Team average damage
Exercise : Rank system
Complete game code: Python Kingdom (Lesson 2)
Here is your game so far! Now you have the title screen and character creation with class selection and a shop:
Test Your Knowledge
Check how well you understood the lesson with these 6 questions.

