sec01 - Assigning values to variables

In this lecture, we will learn about variable assignments.

Variable assignment

In the previous lecture, we explained that variables are like boxes that store information. Storing values in variables is called “variable assignment.” Another name for this term is “variable declaration” or “defining variables.” In either case, the program prepares boxes for storing data (more precisely, it reserves space in memory for storing data) and writes the data into them.

How to assign values to variables

Now, let's write some Python code.

Creating Python files

(1) Create a folder for the new section.

Open the EXPLORER panel in Visual Studio Code.

If you closed the “python101” folder that was open during the previous lecture, please open it again from the “File > Open Folder...” menu.

Clear the selection (focus) of the folders and files displayed in the panel.
(* If an item in the list is selected, an attempt will be made to create a folder under that directory. To create a new folder directly under the root folder, you must clear the selection of all items.)

Click the “New Folder...” icon in the EXPLORER panel and enter a folder name.

(2) Create a new Python file.

Select the folder where you want to create the file, and click the “New File...” icon. Enter a file name including the extension, and create a Python file.

Preparing to write code

When writing code, it is convenient to separate files for each lecture so that you can review them later. Since it would be cumbersome to explain file creation every time, we will omit this explanation from the next lecture onwards.

When creating Python files for each lecture, follow these steps: (1) Create a folder for the section, and (2) Create a .py file for the lecture.

Write code

Write the following code in the newly created file.

num = 5

In Python, variables are assigned in this way.

In the expression, “num” is the name of the variable, and “5” is the value to be recorded. The “=‘’ between them means ”assign the value on the right side of '=' to the variable on the left side."

The Meaning of Blank Spaces

There is one space on either side of “=”, which is used to make the code easier to read. Spaces do not affect processing, so using spaces appropriately improves code readability.

※ Spaces only affect the result when writing strings to a file or outputting them to the screen. For example, in the case of “Hello,World!,” there are no spaces between the comma (“,”) and the W, so no spaces will be output to the TERMINAL. However, if there is a space between the comma and the W, as in “Hello, World!,” the space will appear in the result.

As code, you can execute just one line of variable assignment, and the processing will actually be performed, but nothing will be displayed as a result. Therefore, it is impossible to determine whether the code is working correctly, so add a print statement to display the value in the TERMINAL.

num = 5
print(num)
Multiline code

In Python, code is written one line at a time for each process.

In this example,

(1) “Assign 5 to num” process

(2) “Print the value assigned to num using the print statement” process

are written one line at a time. This principle remains the same even when writing large programs.

※ When describing a single process, if a line becomes too long, you may “split a single process into multiple lines.” However, do not “write multiple processes in a single line.”The following code is an example of assigning information with multiple values to a variable. This is used when writing in one line becomes too long or when splitting into multiple lines improves readability. However, the principle is to “write one operation per line,” so keep this in mind.

num = [2500000, 15000, 1000, 3600000]

# ↓In some cases, write in multiple lines.

num = [
    2500000,
    15000,
    1000,
    3600000
]

print is a command (strictly speaking, a function) that outputs the value inside the parentheses. In the previous lectures, we directly wrote the information to be output in the parentheses, as shown in the code below.

print('Hello, World!')

However, in this case, the code displays the value assigned to “num” in the first line. Python references the information assigned to the “num” variable and displays the value “5” recorded in the variable.

Execute code

Once you have finished writing the code, save the file.

num = 5
print(num)

As introduced in the previous lecture, if the launch.json settings have been configured, you can immediately execute the code by pressing the F5key.

When the code is executed, TERMINAL will open automatically and display the progress of the processing. If the value “5” output by the print statement is displayed as shown in the figure below, the operation is successful.

Code execution procedure

Programs are executed in order from the first line, top to bottom. In complex calculations, changing the order of execution may result in different calculation results.
It is necessary to write code while confirming how the calculation or processing status changes in each line, starting from the top.

Variable assignment (2)

Add two lines to the previous code. Please add the code in lines 3 and 4 of the code below.

num = 5
print(num)
word = 'Hello!'
print(word)

This time, we are assigning the string information “Hello!” to the variable “word.” As you can see, in Python, you can assign not only numbers but also strings and other information to variables.

Also, the “num” variable and the “word” variable are different variables and manage information separately.

Save this code and try running it by pressing F5.

The words “5” and “Hello!” will appear on the TERMINAL.

The processing procedure is as follows: After “5” is output by the print statement in the second line, the character information “Hello!” is assigned to the variable “word” in the third line, and then “Hello!” is output by the print statement in the fourth line.

In this way, you can include multiple variable assignments and multiple print outputs in the code.

おすすめの記事