What are variables

In Python, as in any other programming language, there are things called "variables".

We can imagine variables as boxes: inside each box there is a value, for example a number or a text.

Each box has a label on top, with a name.

So a variable has a name and a value in Python, while in other programming languages it also has a defined type - like in C or C++ - but we are currently talking about Python.



How to define a variable

To define a variable we use the following syntax:

x = 2
In this case the variable is of numeric type.

In case it is a textual variable:
name = "valerio"
Note that in Python to define a "text string", i.e. a variable with a textual value, it is necessary to use double or single quotes (for Python it doesn't matter which one).



Boolean variables

If the variable is of boolean type, the story changes: a boolean variable is a variable that can only have two values: True or False. Therefore it is useful to check whether a condition is true or false.

Below, examples of boolean variables:

variable_true = True 

variable_false = False
Note that you should never use a space to define a variable name, otherwise Python will consider it a syntax error. It is recommended instead to use the underscore ("_").

Recommendation: True and False must be written with a capital letter to assume the value of true or false.



Numeric decimal variables

Numeric variables can also be decimal:

decimal = 1.2 

decimal = 1.22
Remember: use a dot, not a comma.



Summary syntax

A variable is declared in this way: variable name = variable value Python makes everything super simple thanks to its syntax.



Useful links to learn more

LearnPython.org - Variables and Types

Aulab.it - Variables and data types in Python



Author: Valerio De Marchis

Date: 22/07/2026