Python Programming

Variables

Easy
20 min

What is a variable?

Variables have a name and a value in programming. Usually, the value varies during program execution. In Python, setting a variable is simple, the syntax is:

variable_name = variable_value

Variable names

The variable name has some restrictions. For example, it cannot start with a number, it cannot contain spaces, and it cannot have many special characters. Underscore is typically used as a replacement for a space, as shown above.

Example

Let's say we have a record of two players' HP that we want to keep track of.

player1_hp = 100
player2_hp = 90

Using Variables

Once the variables have been defined, we can use them in the code. For example, like this, we could print the HP of both players:

print(player1_hp)
print(player2_hp)

Exercise

Just do the task! Remember to be careful with names and values, the automatic checking mechanism is quite a stickler for details.

Hint

Exercises

Task 1

Set a variable named pelaajan_nimi and set its value to the text "Jaska".

Task 2

Set the variable pelaajan_hp and set it to 100.

hakatemia pro

Ready to become an ethical hacker?
Start today.

As a member of Hakatemia you get unlimited access to Hakatemia modules, exercises and tools, and you get access to the Hakatemia Discord channel where you can ask for help from both instructors and other Hakatemia members.