Victor Jeman Academy
Python Fundamentals9 min

Variables and math

Learn about variables, data types, and math operators to store and change your hero's stats.

What You'll Learn

  • Store data in variables and change their values later
  • Recognize the basic data types: str, int, float, bool
  • Do math with the operators +, -, *, /
  • Split things evenly with floor division (//) and remainder (%)

In the intro lesson you printed a title screen for Python Kingdom with print() and left yourself notes with # comments. Nice screen. But your hero has no stats yet, no HP, no attack, no gold. That's what we fix today.

Every RPG character is really just a pile of numbers that keep changing: you take damage and HP drops, you win a fight and gold goes up, you hit level 10 and unlock a new skill. To track a number that changes, you need a variable. To change it, you need math.

So in this lesson you build the hero's character sheet: variables for the stats, and the math operators that change them. By the end your title screen has a real hero standing behind it.

Variables

Variabila = o cutie cu eticheta
player_hp
120
int
HP inainte de lupta

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 quotes
  • 120 and 350 are integers (int), numbers without decimals
  • 7 is also an int
  • True is a boolean (bool), can only be True or False

There are also decimal numbers, called float:

You already know print() from the intro lesson. Point it at a variable instead of a fixed message and it shows you what's inside:

Pass print() several things separated by commas and it puts them on one line with a space between each. This is how you mix text and a number safely, the comma keeps them apart so Python never tries to glue text onto a number:

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

Calculator RPG

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:

OperatorWhat it doesExampleResult
+Addition100 + 3103
-Subtraction100 - 397
*Multiplication100 * 3300
/Division100 / 333.333...
//Floor division100 // 333
%Remainder (modulo)100 % 31

Here they are at work on hero stats:

The two odd ones are floor division (//) and modulo (%). Optional deep-dive, skip if you just want the basics; +, -, *, / cover most everyday math. You'll lean on these two later for things like splitting loot into even stacks:

// tells you how many times 12 fits into 50 (4 times), and % tells you how much is left over (2 coins).

Variables store data and operators change it. A variable's value can change at any time, that is the whole point. Pick the right type (str, int, float, bool) and use clear names so your code reads like the game it describes.

Exercise : Hero Sheet from the Python Kingdom

Create the variables for the hero of the Python Kingdom game: hero_name (string), hero_class (string), hp (int), attack (int), defense (int), and gold (int). Choose the initial values (for example: "Aldric", "Warrior", 100, 15, 10, 50). Then display the hero sheet with all the stats, each on its own line.

Exercise : Team average damage

You have a squad of 3 warriors. The first deals 22 damage per hit, the second deals 18, and the third deals 31. Calculate the total damage of the squad and display it. Then calculate and display the average damage per warrior (with decimals).

Complete game code: Python Kingdom

Last lesson the hero's name and stats were baked into print() lines. Now they live in variables, and math turns them into a real character sheet. Copy the code below into a .py file and run it:

The stats are real values now: total_power is computed from them, and buying the Iron Sword spends gold and raises attack with plain math. Change hp or gold in one place and everything downstream updates. But the game still buys the sword no matter what, even if the hero is broke. Next lesson you teach it to check first.

What's next

Your hero has stats now, name, HP, attack, gold, and you can change them with math. But the game still runs every line top to bottom and can't choose anything. It can't check "do I have enough gold to buy the sword?". Next lesson, Making decisions, you teach it to decide.

Test Your Knowledge

Check how well you understood the lesson with these 3 questions.

Question 1 of 3

What data type is the value 3.14?