What do we observe? There is a declared
variable x which stores the input data
entered when the program is run. Therefore, any value entered via the keyboard will be displayed in the console.
Running the program again, you can input another value!
This is where the name "variable" comes from - although it has a single name
x,
it stores different data, thus having
a variable content.
What Is The Mechanism?
Essentially, let's imagine the variable
x as a "
box" located in the internal
memory of our computer, where we store "
":
x
Definition 1. By
reading we mean the operation through which the computer takes data from the outside and
places it in internal memory. For reading, the external medium will be the keyboard for now, using
the
input instruction.
Definition 2. By
writing we mean the operation through which the computer takes data from internal
memory and places it outside. For writing, the external medium will be the console for now, using
the
print instruction.
Declaring Variables
In our program, a variable named
x is
declared and
assigned the
value entered by the user (returned by the
input function).
Of course, we could choose not to request user input and instead declare it with a value directly in the program.
For example, we write the equal sign after the variable name and enter the desired value:
x = 47
print(x)
In this case, the program would display "
47" every time...
The equal sign "
=" is an
assignment operator.
When creating your own programs, you will need one or more variables to assist in developing algorithms,
as you will see
later on.