はじめに
この実験では、Python の関数が特定の値を返すかどうかを確認する方法を学びます。この実験では、return
文を使用した関数の戻り値の理解と出力の検証に焦点を当てています。
まず、2 つの数値の合計を計算して結果を返す関数を持つ Python スクリプトを作成します。次に、そのスクリプトを実行して出力を観察します。最後に、条件に基づいて異なる値を返すようにスクリプトを修正し、返された値を検証します。
💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください
この実験では、Python の関数が特定の値を返すかどうかを確認する方法を学びます。この実験では、return
文を使用した関数の戻り値の理解と出力の検証に焦点を当てています。
まず、2 つの数値の合計を計算して結果を返す関数を持つ Python スクリプトを作成します。次に、そのスクリプトを実行して出力を観察します。最後に、条件に基づいて異なる値を返すようにスクリプトを修正し、返された値を検証します。
このステップでは、Python の関数の戻り値について学びます。関数は、特定のタスクを実行する再利用可能なコードブロックです。多くの場合、関数が作業を完了した後に結果を返してほしいと思うでしょう。これは return
文を使用して行われます。
まず、戻り値を持つ関数を定義する単純な Python スクリプトを作成しましょう。
LabEx 環境で VS Code エディタを開きます。
~/project
ディレクトリに calculate_sum.py
という名前の新しいファイルを作成します。
~/project/calculate_sum.py
calculate_sum.py
ファイルに以下のコードを追加します。
def calculate_sum(x, y):
"""
This function calculates the sum of two numbers.
"""
sum_result = x + y
return sum_result
## Example usage:
num1 = 10
num2 = 5
result = calculate_sum(num1, num2)
print("The sum of", num1, "and", num2, "is", result)
このコードでは:
x
と y
という 2 つの引数を取る calculate_sum
という関数を定義しています。x
と y
の合計を計算し、sum_result
という変数に格納しています。return sum_result
文は、sum_result
の値を関数の呼び出し元に返します。num1 = 10
と num2 = 5
で関数を呼び出し、返された値を result
変数に格納します。print()
関数を使用して結果を表示します。次に、ターミナルで以下のコマンドを使用してスクリプトを実行します。
python ~/project/calculate_sum.py
以下の出力が表示されるはずです。
The sum of 10 and 5 is 15
この出力は、関数が正しく合計を計算して値を返し、それがコンソールに印刷されたことを確認します。
条件に基づいて異なる値を返すようにスクリプトを修正しましょう。calculate_sum.py
ファイルを編集して以下を追加します。
def calculate_sum(x, y):
"""
This function calculates the sum of two numbers.
If the sum is greater than 10, it returns double the sum.
"""
sum_result = x + y
if sum_result > 10:
return sum_result * 2
else:
return sum_result
## Example usage:
num1 = 10
num2 = 5
result = calculate_sum(num1, num2)
print("The sum of", num1, "and", num2, "is", result)
num3 = 2
num4 = 3
result2 = calculate_sum(num3, num4)
print("The sum of", num3, "and", num4, "is", result2)
ここでは、合計が 10 より大きいかどうかをチェックする条件文 (if sum_result > 10:
) を追加しています。もし大きければ、関数は合計の 2 倍を返します。そうでなければ、元の合計を返します。
再度スクリプトを実行します。
python ~/project/calculate_sum.py
これで以下の出力が表示されるはずです。
The sum of 10 and 5 is 30
The sum of 2 and 3 is 5
最初の calculate_sum
の呼び出しは、合計 (15) が 10 より大きいため 30 を返し、2 倍になります。2 番目の呼び出しは、合計 (5) が 10 より大きくないため 5 を返します。
このステップでは、Python で関数を呼び出し、その出力を取得する方法を学びます。関数の出力を取得することで、返された値をスクリプト内のさらなる計算や操作に使用することができます。
前のステップで使用した calculate_sum.py
ファイルを引き続き使用します。前のステップを完了していない場合は、以下の内容でファイルを作成してください。
def calculate_sum(x, y):
"""
This function calculates the sum of two numbers.
If the sum is greater than 10, it returns double the sum.
"""
sum_result = x + y
if sum_result > 10:
return sum_result * 2
else:
return sum_result
## Example usage:
num1 = 10
num2 = 5
result = calculate_sum(num1, num2)
print("The sum of", num1, "and", num2, "is", result)
num3 = 2
num4 = 3
result2 = calculate_sum(num3, num4)
print("The sum of", num3, "and", num4, "is", result2)
では、calculate_sum
関数の出力を取得し、それを使って追加の操作を行うようにスクリプトを修正しましょう。
VS Code エディタで calculate_sum.py
ファイルを開きます。
スクリプトを以下のように修正します。
def calculate_sum(x, y):
"""
This function calculates the sum of two numbers.
If the sum is greater than 10, it returns double the sum.
"""
sum_result = x + y
if sum_result > 10:
return sum_result * 2
else:
return sum_result
## Example usage:
num1 = 10
num2 = 5
result = calculate_sum(num1, num2)
print("The sum of", num1, "and", num2, "is", result)
## Capture the output and perform an additional operation
final_result = result + 20
print("The final result after adding 20 is:", final_result)
num3 = 2
num4 = 3
result2 = calculate_sum(num3, num4)
print("The sum of", num3, "and", num4, "is", result2)
## Capture the output and perform an additional operation
final_result2 = result2 * 2
print("The final result after multiplying by 2 is:", final_result2)
この修正されたスクリプトでは:
num1
と num2
を引数にして calculate_sum
関数を呼び出し、返された値を result
変数に格納します。result
に 20 を足して final_result
に格納します。final_result
の値を出力します。num3
と num4
を引数にして calculate_sum
関数を呼び出し、返された値を result2
変数に格納します。result2
に 2 をかけて final_result2
に格納します。final_result2
の値を出力します。以下のコマンドを使用してスクリプトを実行します。
python ~/project/calculate_sum.py
以下の出力が表示されるはずです。
The sum of 10 and 5 is 30
The final result after adding 20 is: 50
The sum of 2 and 3 is 5
The final result after multiplying by 2 is: 10
この出力は、関数を正常に呼び出し、返された値を取得し、その値をスクリプト内の後続の操作で使用できることを示しています。
このステップでは、関数の出力を期待値と比較する方法を学びます。これは、関数が正しく動作していることをテストし確認する上で重要な部分です。実際の出力を期待される出力と比較することで、コード内のエラーを特定し修正することができます。
前のステップで使用した calculate_sum.py
ファイルを引き続き使用します。前のステップを完了していない場合は、以下の内容でファイルを作成してください。
def calculate_sum(x, y):
"""
This function calculates the sum of two numbers.
If the sum is greater than 10, it returns double the sum.
"""
sum_result = x + y
if sum_result > 10:
return sum_result * 2
else:
return sum_result
## Example usage:
num1 = 10
num2 = 5
result = calculate_sum(num1, num2)
print("The sum of", num1, "and", num2, "is", result)
## Capture the output and perform an additional operation
final_result = result + 20
print("The final result after adding 20 is:", final_result)
num3 = 2
num4 = 3
result2 = calculate_sum(num3, num4)
print("The sum of", num3, "and", num4, "is", result2)
## Capture the output and perform an additional operation
final_result2 = result2 * 2
print("The final result after multiplying by 2 is:", final_result2)
では、出力を期待値と比較し、テストが合格したか不合格かを示すメッセージを出力する関数を追加しましょう。
VS Code エディタで calculate_sum.py
ファイルを開きます。
スクリプトを以下のように修正します。
def calculate_sum(x, y):
"""
This function calculates the sum of two numbers.
If the sum is greater than 10, it returns double the sum.
"""
sum_result = x + y
if sum_result > 10:
return sum_result * 2
else:
return sum_result
def compare_output(actual_output, expected_output):
"""
This function compares the actual output with the expected output.
"""
if actual_output == expected_output:
print("Test passed!")
else:
print("Test failed. Expected:", expected_output, "Actual:", actual_output)
## Example usage:
num1 = 10
num2 = 5
result = calculate_sum(num1, num2)
print("The sum of", num1, "and", num2, "is", result)
## Capture the output and perform an additional operation
final_result = result + 20
print("The final result after adding 20 is:", final_result)
## Compare the output with the expected value
expected_final_result = 50
compare_output(final_result, expected_final_result)
num3 = 2
num4 = 3
result2 = calculate_sum(num3, num4)
print("The sum of", num3, "and", num4, "is", result2)
## Capture the output and perform an additional operation
final_result2 = result2 * 2
print("The final result after multiplying by 2 is:", final_result2)
## Compare the output with the expected value
expected_final_result2 = 10
compare_output(final_result2, expected_final_result2)
この修正されたスクリプトでは:
actual_output
と expected_output
という 2 つの引数を取る compare_output
という関数を定義しています。actual_output
と expected_output
を比較します。final_result
と expected_final_result
を引数にして compare_output
関数を呼び出し、最初の計算の出力を期待値 50 と比較します。final_result2
と expected_final_result2
を引数にして compare_output
関数を呼び出し、2 番目の計算の出力を期待値 10 と比較します。以下のコマンドを使用してスクリプトを実行します。
python ~/project/calculate_sum.py
以下の出力が表示されるはずです。
The sum of 10 and 5 is 30
The final result after adding 20 is: 50
Test passed!
The sum of 2 and 3 is 5
The final result after multiplying by 2 is: 10
Test passed!
この出力は、関数の出力を期待値と正常に比較し、テストが合格したか不合格かを判断できることを示しています。
この実験では、最初のステップで Python の関数の返り値について理解することに焦点を当てています。calculate_sum.py
スクリプトを作成し、その中で calculate_sum(x, y)
という関数を定義します。この関数は 2 つの入力数値の合計を計算し、return
文を使って結果を返します。その後、スクリプトはサンプル値を使ってこの関数を呼び出し、返された合計をコンソールに出力します。これにより、関数が返す値を取得してコード内の他の場所で使用できることを示しています。
最初のスクリプトは、条件に基づいて異なる値を返すように修正されます。これにより、関数の返り値の柔軟性がさらに明確になります。このステップでは、関数が入力と内部ロジックに基づいて特定の出力を提供できる再利用可能なコードブロックであるという概念が強化されます。