はじめに
この実験では、Python で数値が特定の範囲内にあるかどうかをチェックする方法を学びます。この実験では、数値範囲を定義し、比較演算子を利用して、与えられた数値が定義された区間内にあるかどうかを判断することに焦点を当てています。
これらの概念を探索するために、ranges.py という名前の Python スクリプトを作成します。このスクリプトでは、数値を定義し、比較演算子(例:<=)を使用して、その数値が 10 から 100 などの指定された範囲内にあるかどうかをチェックする方法を示します。数値を変更してスクリプトを実行することで、数値が定義された範囲内にあるか外にあるかに応じて出力がどのように変化するかを観察できます。
数値範囲を定義する
このステップでは、Python で数値範囲を定義する方法を学びます。数値範囲は、数値が特定の区間内にあるかどうかをチェックする必要がある場合に便利です。Python では、比較演算子や range() 関数を使った、数値範囲を定義して使用する方法がいくつか用意されています。
まず、VS Code エディタを使って ~/project ディレクトリに ranges.py という名前の Python スクリプトを作成しましょう。
#!/usr/bin/env python3
## Define a variable
number = 50
## Check if the number is within a range using comparison operators
if 10 <= number <= 100:
print(f"{number} is within the range of 10 to 100")
else:
print(f"{number} is outside the range of 10 to 100")
このスクリプトでは、以下のことを行っています。
- 変数
numberを定義し、値 50 を代入しています。 - 比較演算子 (
<=) を使って、numberが 10 から 100 の範囲内にあるかどうかをチェックしています。 - 条件が真の場合、数値が範囲内にあることを示すメッセージを出力します。そうでない場合、数値が範囲外にあることを示すメッセージを出力します。
では、スクリプトを実行しましょう。
python ~/project/ranges.py
以下の出力が表示されるはずです。
50 is within the range of 10 to 100
次に、別の数値をチェックするようにスクリプトを変更しましょう。number 変数の値を 5 に変更して、再度スクリプトを実行します。
#!/usr/bin/env python3
## Define a variable
number = 5
## Check if the number is within a range using comparison operators
if 10 <= number <= 100:
print(f"{number} is within the range of 10 to 100")
else:
print(f"{number} is outside the range of 10 to 100")
スクリプトを実行します。
python ~/project/ranges.py
以下の出力が表示されるはずです。
5 is outside the range of 10 to 100
これで、Python で比較演算子を使って数値範囲を定義し、チェックする方法がわかりました。
比較演算子を使用する
このステップでは、Python の比較演算子について深く理解し、それらを使ってより複雑な数値範囲を定義する方法を学びます。比較演算子を使うと、値同士の関係をチェックする条件を作成できます。
~/project ディレクトリの ranges.py ファイルを引き続き編集しましょう。スクリプトを変更して、より多くの比較演算子を使うようにします。
#!/usr/bin/env python3
## Define a variable
number = 50
## Check if the number is greater than or equal to 20 and less than 80
if number >= 20 and number < 80:
print(f"{number} is greater than or equal to 20 and less than 80")
else:
print(f"{number} is not greater than or equal to 20 and less than 80")
## Check if the number is equal to 50 or equal to 100
if number == 50 or number == 100:
print(f"{number} is either 50 or 100")
else:
print(f"{number} is not 50 or 100")
このスクリプトでは、以下のことを行っています。
>=(以上)と<(未満)の演算子を使って、numberが特定の範囲内にあるかどうかをチェックしています。and演算子を使って 2 つの条件を結合しており、全体の条件が真になるには両方の条件が真でなければなりません。==(等しい)演算子を使って、numberが特定の値と等しいかどうかをチェックしています。or演算子を使って 2 つの条件を結合しており、全体の条件が真になるには少なくとも一方の条件が真であればよいです。
では、スクリプトを実行しましょう。
python ~/project/ranges.py
以下の出力が表示されるはずです。
50 is greater than or equal to 20 and less than 80
50 is either 50 or 100
次に、スクリプトを再度変更して、number の値を 100 に変更し、出力を観察しましょう。
#!/usr/bin/env python3
## Define a variable
number = 100
## Check if the number is greater than or equal to 20 and less than 80
if number >= 20 and number < 80:
print(f"{number} is greater than or equal to 20 and less than 80")
else:
print(f"{number} is not greater than or equal to 20 and less than 80")
## Check if the number is equal to 50 or equal to 100
if number == 50 or number == 100:
print(f"{number} is either 50 or 100")
else:
print(f"{number} is not 50 or 100")
スクリプトを実行します。
python ~/project/ranges.py
以下の出力が表示されるはずです。
100 is not greater than or equal to 20 and less than 80
100 is either 50 or 100
これで、Python で比較演算子と論理演算子 (and, or) を使って、数値範囲に対するより複雑な条件を定義する方法がわかりました。
整数に対して range() を使用してチェックする
このステップでは、Python の range() 関数を使って数値のシーケンスを生成し、整数がその範囲内にあるかどうかをチェックする方法を学びます。range() 関数は、数値のシーケンスを繰り返し処理する必要がある場合や、特定の区間内の整数のリストを作成する場合に特に便利です。
VS Code エディタを使って ~/project ディレクトリに range_check.py という名前の新しい Python スクリプトを作成しましょう。
#!/usr/bin/env python3
## Define a variable
number = 25
## Check if the number is within the range of 1 to 50 (exclusive)
if number in range(1, 50):
print(f"{number} is within the range of 1 to 49")
else:
print(f"{number} is outside the range of 1 to 49")
## Check if the number is within the range of 0 to 100 with a step of 5
if number in range(0, 101, 5):
print(f"{number} is within the range of 0 to 100 with a step of 5")
else:
print(f"{number} is outside the range of 0 to 100 with a step of 5")
このスクリプトでは、以下のことを行っています。
- 変数
numberを定義し、値 25 を代入しています。 range(1, 50)関数を使って、1 から 50 まで(ただし 50 は含まない)の数値のシーケンスを生成しています。in演算子を使って、numberが生成されたシーケンス内に存在するかどうかをチェックしています。range(0, 101, 5)関数を使って、0 から 101 まで(ただし 101 は含まない)、ステップ幅 5 で数値のシーケンスを生成しています(つまり、0, 5, 10, 15, ..., 100)。
では、スクリプトを実行しましょう。
python ~/project/range_check.py
以下の出力が表示されるはずです。
25 is within the range of 1 to 49
25 is within the range of 0 to 100 with a step of 5
次に、スクリプトを変更して、number の値を 7 に変更し、出力を観察しましょう。
#!/usr/bin/env python3
## Define a variable
number = 7
## Check if the number is within the range of 1 to 50 (exclusive)
if number in range(1, 50):
print(f"{number} is within the range of 1 to 49")
else:
print(f"{number} is outside the range of 1 to 49")
## Check if the number is within the range of 0 to 100 with a step of 5
if number in range(0, 101, 5):
print(f"{number} is within the range of 0 to 100 with a step of 5")
else:
print(f"{number} is outside the range of 0 to 100 with a step of 5")
スクリプトを実行します。
python ~/project/range_check.py
以下の出力が表示されるはずです。
7 is within the range of 1 to 49
7 is outside the range of 0 to 100 with a step of 5
これで、Python で range() 関数と in 演算子を使って、整数が特定の範囲内にあるかどうかをチェックする方法がわかりました。
まとめ
この実験では、Python で数値範囲を定義し、特定の区間内に数値が含まれるかどうかをチェックする方法を学びました。最初のステップでは、ranges.py という名前の Python スクリプトを作成し、比較演算子 (<=) を使って、与えられた数値が 10 から 100 の範囲内にあるかどうかを判断しました。
この実験では、スクリプトを変更して異なる数値をテストし、それに対応する出力を観察することで、各数値が定義された範囲内にあるか範囲外にあるかを確認する方法を示しました。これにより、Python で比較演算子を使って範囲をチェックする方法を実践的に理解することができました。



