Python で変数が整数かどうかをチェックする方法

PythonPythonBeginner
今すぐ練習

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、Python で変数が整数であるかどうかを確認する方法を学びます。まず、小数点のない整数である整数型の基本的なデータ型について理解し、整数変数を宣言して使用する方法を学びます。

次に、type() 関数を使って変数のデータ型を識別し、isinstance() 関数を使って変数が整数であることを確認します。これにより、整数を効果的に扱い、Python コードで正しい操作を行うためのツールを身につけることができます。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python/BasicConceptsGroup -.-> python/variables_data_types("Variables and Data Types") python/BasicConceptsGroup -.-> python/numeric_types("Numeric Types") python/BasicConceptsGroup -.-> python/type_conversion("Type Conversion") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") subgraph Lab Skills python/variables_data_types -.-> lab-559605{{"Python で変数が整数かどうかをチェックする方法"}} python/numeric_types -.-> lab-559605{{"Python で変数が整数かどうかをチェックする方法"}} python/type_conversion -.-> lab-559605{{"Python で変数が整数かどうかをチェックする方法"}} python/build_in_functions -.-> lab-559605{{"Python で変数が整数かどうかをチェックする方法"}} end

整数型を理解する

このステップでは、Python の基本的なデータ型の 1 つである整数型について調べます。整数型は、小数点のない整数で、正の数、負の数、またはゼロのいずれでもかまいません。Python で数学的な演算を行ったり、数値データを扱ったりするには、整数型を理解することが重要です。

まず、VS Code エディタを使って、~/project ディレクトリに integers.py という名前の Python ファイルを作成しましょう。

~/project/integers.py

次に、エディタで integers.py を開き、以下のコードを追加します。

## 整数値を変数に代入する
x = 10
y = -5
z = 0

## 変数の値を出力する
print("The value of x is:", x)
print("The value of y is:", y)
print("The value of z is:", z)

このコードでは、3 つの変数 xyz に整数値を代入しています。x には正の整数 (10) が代入され、y には負の整数 (-5) が代入され、z にはゼロ (0) が代入されています。そして、print() 関数を使ってこれらの変数の値を表示しています。

スクリプトを実行するには、VS Code のターミナルを開き (通常は「表示」 -> 「ターミナル」にあります)、以下のコマンドを実行します。

python integers.py

以下の出力が表示されるはずです。

The value of x is: 10
The value of y is: -5
The value of z is: 0

これは、Python で整数変数を宣言して使用する方法を示しています。整数型は、カウント、インデックス付け、計算など、さまざまなプログラミングタスクで広く使用されます。

type() 関数を使って整数型を識別する

このステップでは、Python の type() 関数を使って変数のデータ型を識別する方法を学びます。type() 関数は、オブジェクトの型を返す組み込み関数です。これは、さまざまなデータ型を扱い、正しい操作を行う必要がある場合に特に便利です。

前のステップで作成した integers.py ファイルを変更して、type() 関数を追加しましょう。VS Code エディタで integers.py を開き、以下のコードを追加します。

## 整数値を変数に代入する
x = 10
y = -5
z = 0

## 変数の値を出力する
print("The value of x is:", x)
print("The value of y is:", y)
print("The value of z is:", z)

## type() 関数を使ってデータ型を識別する
print("The type of x is:", type(x))
print("The type of y is:", type(y))
print("The type of z is:", type(z))

ここでは、type() 関数を使って変数 xyz のデータ型を判定する 3 つの新しい print() 文を追加しています。

次に、ターミナルで以下のコマンドを実行して、スクリプトを再度実行します。

python integers.py

以下の出力が表示されるはずです。

The value of x is: 10
The value of y is: -5
The value of z is: 0
The type of x is: <class 'int'>
The type of y is: <class 'int'>
The type of z is: <class 'int'>

ご覧のとおり、type() 関数によって xyz がすべて int (整数型) であることが確認されます。これは、Python で変数のデータ型を検証する簡単で強力な方法です。

isinstance() 関数で確認する

このステップでは、Python の isinstance() 関数について調べます。この関数は、変数のデータ型を検証する別の方法を提供します。isinstance() 関数は、オブジェクトが指定されたクラスまたは型のインスタンスであるかどうかをチェックします。これは、より複雑な型チェックのシナリオで特に便利です。

integers.py ファイルの変更を続けましょう。VS Code エディタで integers.py を開き、以下のコードを追加します。

## 整数値を変数に代入する
x = 10
y = -5
z = 0

## 変数の値を出力する
print("The value of x is:", x)
print("The value of y is:", y)
print("The value of z is:", z)

## type() 関数を使ってデータ型を識別する
print("The type of x is:", type(x))
print("The type of y is:", type(y))
print("The type of z is:", type(z))

## isinstance() を使ってデータ型を確認する
print("Is x an integer?", isinstance(x, int))
print("Is y an integer?", isinstance(y, int))
print("Is z an integer?", isinstance(z, int))

このコードでは、isinstance() 関数を使って変数 xyzint クラスのインスタンスであるかどうかをチェックする 3 つの新しい print() 文を追加しています。isinstance() 関数は、オブジェクトが指定されたクラスのインスタンスである場合は True を返し、そうでない場合は False を返します。

次に、ターミナルで以下のコマンドを実行して、スクリプトを再度実行します。

python integers.py

以下の出力が表示されるはずです。

The value of x is: 10
The value of y is: -5
The value of z is: 0
The type of x is: <class 'int'>
The type of y is: <class 'int'>
The type of z is: <class 'int'>
Is x an integer? True
Is y an integer? True
Is z an integer? True

isinstance() 関数によって、xyz が実際に整数であることが確認されます。このメソッドは、特に継承やポリモーフィズムを扱う場合の柔軟性から、type() よりも好まれることが多いです。

まとめ

この実験では、まず Python の整数型について調べ、整数が小数点のない整数(正、負、またはゼロ)であることを理解しました。integers.py ファイルを作成し、変数 xyz に整数値を代入し、それらの値をコンソールに出力しました。これにより、整数型変数の宣言と使用方法を実証しました。

次に、type() 関数を使って変数のデータ型を識別する方法を学びました。この組み込み関数はオブジェクトの型を返し、さまざまなデータ型を扱う際に正しい操作を行うために役立ちます。