Pythonキーワードと識別子(更新済み)

Pythonのキーワードと識別子について話しましょう。最近、このPythonのチュートリアルで初心者向けにPythonのインストールとセットアップの完全なチュートリアルもカバーしました。

Pythonのキーワード

Pythonのキーワードは、予約されている単語です。つまり、変数、クラス、関数などの名前として使用することはできません。

では、これらのキーワードは何のためにあるのか考えてみましょう。それらはPython言語の構文と構造を定義するためのものです。

このチュートリアルの執筆時点では、Pythonプログラミング言語には33個のキーワードがあります。ただし、数は変動する可能性があります。また、Pythonのキーワードは大文字と小文字を区別します。そのため、そのまま書く必要があります。以下は、Pythonプログラミングのすべてのキーワードのリストです。

すべてのキーワードを一度に見て理解しようとすると、圧倒されるかもしれません。ですので、今はこれらがキーワードであることを知っておいてください。それぞれの使用方法を学びます。PythonシェルのヘルプからPythonのキーワードのリストを取得することができます。

すべてのPythonキーワードのリスト

and Logical operator
as Alias
assert For debugging
break Break out of Python loops
class Used for defining Classes in Python
continue Keyword used to continue with the Python loop by skipping the existing
def Keyword used for defining a function
del Used for deleting objects in Python
elif Part of the if-elif-else conditional statement in Python
else Same as above
except A Python keyword used to catch exceptions
FALSE Boolean value
finally This keyword is used to run a code snippet when no exceptions occur
for Define a Python for loop
from Used when you need to import only a specific section of a module
global Specify a variable scope as global
if Used for defining an “if” condition
import Python keyword used to import modules
in Checks if specified values are present in an iterable object
is This keyword is used to test for equality.
lambda Create anonymous functions
None The None keyword represents a Null value in PYthon
nonlocal Declare a variable with non-local scope
not Logical operator to negate a condition
or A logical operator used when either one of the conditions needs to be true
pass This Python keyword passes and lets the function continue further
raise Raises an exception when called with the specified value
return Exits a running function and returns the value specified
TRUE Boolean value
try Part of the try…except statement
while Used for defining a Python while loop
with Creates a block to make exception handling and file operations easy
yield Ends a function and returns a generator object

以下は、Pythonプログラムでのif-elseの使用例を示すシンプルな例です。

var = 1;

if(var==1):
    print("odd")
else:
    print("even")

上記のプログラムを実行すると、Pythonは固定されたキーワードと構文によってif-elseブロックを理解し、さらなる処理を行います。

Pythonの識別子とは何ですか?

Pythonの識別子は、変数、関数、クラス、モジュール、または他のオブジェクトを識別するために付ける名前です。つまり、エンティティに名前を付けたいときに、それを識別子と呼びます。

変数と識別子がしばしば同じものと誤解されることがありますが、実際には異なります。はっきりさせるために、変数とは何かを見てみましょう。

Pythonでの変数とは何ですか?

A variable, as the name indicates is something whose value is changeable over time. In fact a variable is a memory location where a value can be stored. Later we can retrieve the value to use. But for doing it we need to give a nickname to that memory location so that we can refer to it. That’s identifier, the nickname.

識別子を書くためのルール

識別子を書くためのいくつかのルールがあります。しかしまず最初に、Pythonは大文字と小文字を区別することを知っておく必要があります。つまり、NamenameはPythonでは異なる識別子です。Pythonでの識別子を書くためのいくつかのルールを以下に示します。

  1. 識別子は大文字と小文字の文字、数字、またはアンダースコア(_)の組み合わせにすることができます。そのため、myVariablevariable_1variable_for_printはすべて有効なPython識別子です。
  2. 識別子は数字で始めることはできません。そのため、variable1は有効ですが、1variableは有効ではありません。
  3. !、#、@、%、$などの特殊記号は識別子として使用できません。
  4. 識別子の長さは任意です。

これらは識別子を書くための厳格なルールですが、従うことが推奨されるいくつかの命名規則もあります。

  1. クラス名は大文字で始まります。その他の識別子は小文字で始まります。
  2. 単一の先頭アンダースコアで識別子を開始すると、その識別子はプライベートであることを示します。
  3. 識別子が2つのアンダースコアで始まり終わる場合、その識別子は言語で定義された特別な名前であることを意味します。
  4. c = 10は有効ですが、count = 10と書く方が意味があり、長い時間が経ってからコードを見ても何をしているのかがわかりやすくなります。
  5. 複数の単語はアンダースコアで区切ることができます。例えば、this_is_a_variableです。

Pythonの変数のサンプルプログラムを以下に示します。

myVariable="hello world"
print(myVariable)

var1=1
print(var1)

var2=2
print(var2)

プログラムを実行すると、出力は以下の画像のようになります。

結論

それでは、今日はここまでです。次のチュートリアルでは、Pythonの文とコメントについて学びます。それまで #happy_coding 🙂

Source:
https://www.digitalocean.com/community/tutorials/python-keywords-identifiers