VARI­ABLES



Let's recall a recent example: how old will you be in two years? Analyze how you will come to an answer.

The very first thing is to recall how old we are now. Keeping in mind the number, add a deuce to it and remember the result. And only then we can state the result, record it, express in any way. In the calculation process, it is important to remember three values - the age, the added deuce and the result.

The computer works the same way.


STORING VALUES


If we think even harder about how we recalled the age, we will be confused. OK, we just know it. Somehow, we can extract this data from the depths of the brain with the confidence that the extracted number is our age.

It is not quite clear even to scientists how the brain remembers this or that information. There is an assumption that in this case associative thinking is activated and the information is somehow "squeezed out" from the memory by the characteristics of the information. For example, when we "recall" the age, the characteristics will be "I", "life", "birth", "life path", "I would like to buy a hamburger" .

Words really mean nothing. Each characteristic written here is unique for each of us: “life” is different, “birth” is different and, in the end, “I” is completely different!

If we try to think even deeper, having specifically started to remember different things such as the home address, the phone number of the loved one, the Steam password, we won't understand, how it works! Just recalled and that's all!

The trouble is that it is too complicated for a computer. It cannot “just recall” or “just remember”. It needs something different.

And it is called...



VARI­ABLES


A variable is intended for storing values. A computer remembers any data, extracts it from the memory, do calculations or (if you want to call it in a word) processes data through variables. Everything outside variables simply doesn't exist for a computer.

Continuing the analogy with the human memory, a variable is responsible for extracting values from the computer memory.

Technically, a variable is a link to a memory cell. Exactly the same as an Internet link. You click on the underlined phrase with the mouse and get to the desired site. As for programming, you specify the name of the variable and the action with it. A computer works with "remembered" value and name. In the example with a link to a site, the link itself is the name of the variable, and the site content is the value.

A variable is a named memory area for storing values.

If we speak about named area, it has a name. Thanks, Cap.


NAME AND VALUE


Each variable is defined by its name. The name of the variable is called an identifier. Two different values ​​must be stored under two different identifiers.

The value of the variable can be assigned directly by user or can be calculated. In the latter case it would be the result of the expression in some programming language.

Python itself determines the type of the variable at the moment when you create it and assign the value.

Variable name requirements:

Variable values requirements:

The child is called by parents. The variable is called by a programmer.

Try to give names that reflect the essence of the stored value. For example, it is a good idea to call a variable health if you want to store the value responsible for health in CS:GO, and money - nice for storing the amount of money to buy weapons and armor.

If you want to create an identifier consisting of several words, then the first word is written in small letters, each subsequent one with a capital letter. For example, healthOfPlayer.

Also, the underscore line is used to separate words in the variable name: "_". For example, health_of_player. In this case, each new word in the identifier is written with a small letter.

And still we can use something simpler for variables names such as a, b, c, x, y. If your code is short, use single-character names, it's easier .



VALUE ASSIGN­MENT


To assign value use the operator =. A variable name is written on the left of the sign, a value or an expression - on the right.

The sign itself is called an assignment operator.

Example:

health = 100 money = 16000

After these two lines are executed, the values ​​100 and 16000 will be stored in the computer’s RAM. To access them, use the relevant identifiers health and money.

When the program stops running, the values will be automatically deleted from the memory. Thus, the memory is “cleared” and the free space is available for other programs, or for our relaunched one.

To assign a string value, the string must be enclosed in double quotes.

myName = "Viktor"

The action similar to the previous one will happen, but the memory cell will store the string with my name instead of a number.


VALUE CALCU­LATION


Variable values can be easily changed.

a = 3 b = 5 a = a + 5 b = b + a print(a + b)

What number will appear on the screen?

Answer

21


Be sure to check that the variable used in calculations already exists in the program.

For example, the program below will throw an error:

b = 5 a = a + 5 b = b + a print(a + b)

When Python reaches the line a = a + 5, it will try to "recall" variable a value. And there is no any value. We haven't defined it. Trying to make the computer calculate "nothing" + 5 is not a good deal, actually.

To avoid such errors, I recommend to initialize each variable in the beginning of the program.

Initializing a variable is a process, in which we define a variable and set its value. Most often, the value is zero. Initialization usually occurs in the first lines of a program. Although the requirement to initialize variables for the Python language is not necessary, the habit of initializing each variable used will help you to avoid problems in the future (not all problems, related to coding, you know).

Here is the program that calculates the area of ​​the rectangle:

a = 30 b = 7 s = a * b print(s)

The initialization occurred in the lines:

a = 30 b = 7 s = a * b

The variable s in the expression s = a * b isn't initialized, but it doesn't really matter because the program is short. The initialization occurs at the time of the calculation a * b.


INPUT, PROCCESS AND OUTPUT BLOCKS


Haven't you noticed that in the previous program...

a = 30 b = 7 s = a * b print(s)

...there are all three blocks: input, process and output? Let's take a closer look.

The input block

a = 30 b = 7

With these two lines, we enter the values into the program for further processing. The program receives numbers and set them into the variables with identifiers a and b. In other words, it "remembers" them.

Why do we use short variable names?

Answer

Because the program is short! Short names are in short programs, that's easy!


The process block

s = a * b

At this point, Python uses processor resources and calculates the value of the expression a * b. That is, the program takes and calculates the values stored in computer memory with identifiers a and b.

The output block

print(s)

The values are processed. The result is got. The result is assigned to the variable s. Now we must display the result on the screen. If we cut out the line print(s), the program will become pointless. In the depths of the computer memory there would be the variable s with the value 210 (30 * 7), but we would never see it.

We can shorten the program to:

a = 30 b = 7 print(a * b)

...or even to:

print(30 * 7)

...or even to:

print(210)

But we won't do it. Why?


WHEN TO USE VARI­ABLES?


Always!

Consider the disadvantages of the previous versions of the program:

    print(210)

  1. This is an awful program, if we even could call a program this piece of code. It's static and do nothing. With the same success, you can use a calculator without facing any difficulties with learning Python.

    Moreover, there is only one block in this version: the output block. A complete program always contains all three blocks.

  2. print(30 * 7)

    The same arguments are relevant to this version, although the process block appeared. There is no input block, so there is no program here.

  3. a = 30 b = 7 print(a * b)

    A little more interesting. We combined the process block with the output one. There is nothing wrong with that. It is permissible.

    But if we continue to write a program and we need to operate with the area of the rectangle, we will have to recalculate it with a * b, making the processor count once again.

    If we leave s = a * b, we can use the variable s to operate with the value.

  4. a = 30 b = 7 s = a * b print(s)

    It seems to be ideal, but... What does our program do? It calculates the area of the rectangle! So let the user know about it:

    a = 30 b = 7 s = a * b print("The area of the rectange with the sides", a, " and ", b, "is", s)

I like all in the latest version, but...

To calculate the area of another rectangle, we have to change the first and the second lines. Imagine that your grandmother, for example, would like to calculate the area of the room. So you will try to teach her basic programming, won't you? It would be much easier if the computer asked for data, we would type it with the keyboard and get the result. Each time the program would start, we could calculate different data! U is for universality!

That's exactly what I want to discuss with you in the next chapter. Don't switch the channel!


© 2019-2021 Viktor Trofimov
© 2019-2021 Translation and adaptation by Danil Shentsov
[ Top page ]