Variable value
A variable contains only one value. If you write another value to it, then the old one is "erased".
In Python, when a
variable changes its value, a new memory area is allocated, and the old memory area is no longer available and will be freed by the so-called
garbage collector - a special program that manages memory
There are two ways to save a value in a variable.
1) using the assignment operator. eg
name = "Petr"
Thus, the variable
name
is associated with the value
Petr
. The assignment operator works from right to left: it takes the value that is to the right of the "=" sign and writes it to the variable to the left of the "=" sign
2) вenter the desired value from the keyboard using the built-in function (command) input, for example
name = input()
In the latter case, when the command is executed, the program will wait for the user to enter some value (which specific one we cannot know, depends on the user's desire) and after pressing the
Enter key, the entered character string is written into the name variable
An operator is a programming language command
To display the value of a variable on the screen, in the
print
output operator, we simply specify the variable name without quotes, for example, a program
name = "Petr"
print(name)
# will display Petr
REMEMBER:
1 You can set the value of a variable using the input operator
(name = input ()
) or the assignment operator (
name = "petr
")
2 You can display the value of a variable simply by specifying the variable name without quotation marks in the output statement (
print(name)
)