(Python) Variables. Output, input, assignment


Variables

A computer would not be needed if it did not have the ability to store various information in its memory. In order to create more interesting programs, one must learn how to store information in a computer's memory. At the same time, we need to learn how to somehow access the memory cells of the computer in which we save something.
 
A variable is a location in computer memory that has a name and stores some value corresponding to the type.
 

The word "variable" tells us that its value can change during program execution.  The name of a variable is called identifier (from the word identify - to distinguish one object from another).

Before naming variables, you must REMEMBER easy rules:

  1. Latin letters can be used in variable names a...zA...Z (lowercase and uppercase letters differ);< /li>
  2. numbers and underscores can be used _ ;
  3. you can't start a variable name with a number!;
  4. You can't use spaces, punctuation, or arithmetic symbols;
  5. for a better understanding of the program and ease of development, it is desirable that you give "speaking" variable names.


The last rule is optional, but highly recommended. The use of single-letter variables complicates the developer's work, since you have to keep in mind what the variable is responsible for. 
And if you use "speaking" variables, the name itself will say what we store in it.
For example, if we need to store some name, then we can store the name in the  a variable, or in the  name variable. The latter option is preferable.

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")
You can display the value of a variable simply by specifying the variable name without quotation marks in the output statement (print(name))

Variable type

In addition to the name and value, each variable has its own type. The type shows what values and what operations can be performed with this variable. In addition, the type of the variable shows how to store these variables in memory.
The types we will use most often:
str - character string
int - integer (from English integer )
float - real number

Unlike other popular programming languages (C ++, Java), the Python translator automatically determines the type of a variable by the value that is assigned to it.

Keying in numeric values

To enter data from the keyboard, we studied the input () operator, but this operator allows you to enter only character strings. We need to indicate that the entered lines must be converted to a number. To do this, we need the built-in int () function - to convert to an integer, or float () - to convert to a real number (we will talk more about real numbers later).

For example, 
a = int(input())  # an integer is entered from the keyboard and written to the variable a
b = float(input())  # a real number is entered from the keyboard and written to the variable b
In the program above, the numbers must be entered one per line, because after entering the first value, you must press Enter to write the number to the variable.
Sometimes you need to enter data on one line.

In order to remember data that is entered on one line, the input line must be divided into values by spaces, using the split () function.
For example, if there are two integers on the same line, you can enter them this way:
a, b = input().split()  # We use multiple assignment.
a = int(a)              # convert string to integer
b = int(b)
You can replace all these actions with one line:
a, b = map(int, input().split())
the map function applies another function (indicated first in parentheses - int) to each part obtained after dividing the input string into numbers by spaces
The number of variables on the left must strictly match the number of numbers entered

REMEMBER
Python variable type is automatically detected
To enter numbers one per line, use a = int(input()) - for an integer и b = float(input()) - for real
If all numbers are set on one line, then you need to use split (), for example, for two integers   a, b = map(int, input().split())

TRAIN MORE AND ALL YOU WILL GET!