05-Python

Python(Part.6)| python 【Using Variables】

python| まとめ | 現役エンジニア&プログラミングスクール講師「python」のまとめページです。pythonに関して抑えておきたい知識や文法やにについて記事をまとめています。まとめページの下部には「おすすめの学習書籍」「おすすめのITスクール情報」「おすすめ求人サイト」について情報を掲載中...

Objective

To understand how to use “variables”.

In the previous article, we dealt with an overview of “variables” in python. This time, we will deal with the basic usage of variables. The way “variables” are used is not always as simple as in this article (Python (Part.6) | python [Using Variables]), but by understanding what they can do, you will be able to apply them to your work.

In this article, we will use “script files” to execute “python”. A “script file” is a file that describes a program. Not only “python”, but any file that describes a program is called a “script file”. A file in which “python” is written is saved with the extension “.py”. This file is called a “py file“ or “pi file“.

Python Basics (Using Variables)

Defining Variables

Variables can be used by defining them. The word “define” means not only giving a name to a variable, but also assigning a value to it using an assignment statement (“=”) to complete the definition. Below is a scene of an actual definition.

int_num = 5
str_name = 'ほわほわ'
list_obj1 = [1, 2, 3, 4, 5]

At this time, the “ID (identification number)” in each value is assigned to the “variable” and tied to the “value”. (The figure below is re-posted.)

【English translation of captcha】

変数 → Variables
値の代入 → Value Assignment
値のIDの代入 → Assigning an ID to a value
IDを利用した値の参照 → Referencing a value by its ID

How to name “variables“

How to name “variables“

Defining a variable requires two tasks: naming the variable and associating values with it. In this section, we would like to review the rules for naming variables. The following rules apply to naming variables

  1. Available characters are as follows
    • 「a」~「z」、「A」~「Z」、「0」~「9」、「_(Underscore)」「Kanji」
  2. Capital letters and lowercase letters are distinguished.
  3. How to give a name that is not preferred
    • Although “_ (underscore)” can be used as the first character, it is not often used as the first character in variables in “python” because “_ (underscore)” is used as the first character for special purposes such as methods and encapsulation.
  4. As a rule, several patterns cannot be used for variable names.
    • Numeric characters in the first letter (1name, 2tel, 3,address, etc.)
    • Reserved words (expressions and statements prepared for programming in python, such as “if” and “for”)

How to name ”variables” for use as ”constants”

Since constants are not supported in python, variables named with all “uppercase letters” and “_ (underscore)” are used as constants as a convention.

CONST_ID = 5
CONST_STR = 'IT'

How to use “Variables“

Variables are used to reuse values. For this reuse, it is necessary to associate a “value” with a “variable,” and the assignment statement “=” is used to write this operation in a program. Variables can be used in the following ways. All of them are frequently used.

It is the function of the “assignment statement” that performs these various ties.

Simple stringing

This is a one-to-one association of “variables” and “values.

example_variable_1 = 55
example_variable_2 = 3.14
example_variable_3 = 'sample'
example_variable_4 = True
example_variable_5 = [1, 2, 3, 4, 5]

Simply write “variable,” “assignment statement,” and “value” in that order.

【English translation of captcha】

変数 → Variables
代入文 → Assignment statement
値 → Value

How to tie a single value to multiple variables simultaneously

This is a method of simultaneously linking “values” by arranging “multiple variables” using an “assignment statement”.

example_variable_1 = example_variable_2 = 55

In the above program, the two variables will refer to “55”.

How to tie individual values to individual variables simultaneously

On the left side of the assignment expression, “multiple variables” are separated by “, (comma)” and arranged in order from left to right, and on the right side, “values to be associated” with the variables on the left side are separated by “, (comma)” and arranged in the same order as the variables.

example_variable_1, example_variable_2, example_variable_3 = 55, 3.14, 'sample'

As shown in the capture below, each variable and each value is separated by “,” (comma).

【English translation of captcha】

変数 → Variables
代入文 → Assignment statement
値 → Value

The “value” portion can also use “arithmetic operations“.

example_variable_1, example_variable_2, example_variable_3 = 55 + 5, 3.14 * 2 * 2, 'sample'

As we will cover in the future on this site, variables can also be associated with “instances of classes” and “return values of functions”. That is all for this time.

「python」おすすめ書籍 ベスト3 | 現役エンジニア&プログラミングスクール講師「python」の学習でお勧めしたい書籍をご紹介しています。お勧めする理由としては、考え方、イメージなどを適切に捉えていること、「生のpython」に焦点をあてて解説をしている書籍であることなどが理由です。勿論、この他にも良い書籍はありますが、特に質の高かったものを選んで記事にしています。ページの下部には「おすすめのITスクール情報」「おすすめ求人サイト」について情報を掲載中。...

ブックマークのすすめ

「ほわほわぶろぐ」を常に検索するのが面倒だという方はブックマークをお勧めします。ブックマークの設定は別記事にて掲載しています。

「お気に入り」の登録・削除方法【Google Chrome / Microsoft Edge】「お気に入り」の登録・削除方法【Google Chrome / Microsoft Edge】について解説している記事です。削除方法も掲載しています。...
【パソコン選び】失敗しないための重要ポイント | 現役エンジニア&プログラミングスクール講師【パソコン選び】失敗しないための重要ポイントについての記事です。パソコンのタイプと購入時に検討すべき点・家電量販店で見かけるCPUの見方・購入者が必要とするメモリ容量・HDDとSSDについて・ディスプレイの種類・バッテリーの持ち時間や保証・Officeソフト・ウィルス対策ソフトについて書いています。...
RELATED POST