We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
The code developed for this chapter is located
at https://github.com/PracticalBooks/Python-For-
Beginners/tree/main/ChapterO5. We recommend
that you develop the code by yourself to improve
your coding skills. Then, in case of any issues, you
can compare your code with the code available in the
repository.
Remember that we suggest you create a new Colab
document for each book chapter. It will allow you to
keep your codes organized.
5.1. Variables
A variable is a reserved space in the computer’s
memory where we store a value. In Python, we cre-
ate a variable using the following syntax (we will
explain this process step by step in the next section).
For example, the following code creates a variable
“age” with a value of 21:
Code
age=21When we create the variable age, the computer re-
serves a space in its memory to store the value of this
variable (in this case, the number 21).
In this book, we will imagine variables as “boxes”
that contain two labels and a piece of paper. For ex-
ample, Figure 5-1 graphically represents the variable
age.
Main label
(variable namey — fo, Secondary label
(variable type)
Figure 5-1. Graphical representation of a variable.
The above box contains the following:
Variable name: This is the main label of the
box (located on the bottom left side of the
box). We choose the name ourselves, and it
should be descriptive enough to help us to re-
member what we are storing in the box.Variable value: This is the piece of paper
stored inside the box. We define the value
ourselves depending on what we want to
store.
Variable type: This is the secondary label
of the box (located on the bottom right side
of the box). Python dynamically assigns
the type based on the value we assign to the
variable. In this case, Python assigns it an
int (integer) type since we are storing the
number 21 (later, we will see different vari-
able types).
As we develop more complex programs, we may use
hundreds or thousands of variables to store different
types of information. Therefore, understanding how
to create and use variables is an essential skill for any
programmer.
5.2. Creating variables