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

PythonPythonBeginner
今すぐ練習

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

はじめに

この実験では、Python で数値が負数かどうかを判断する方法を学びます。実験ではまず、負数の概念を紹介し、Python スクリプト内で負数を割り当てて操作する方法を示します。negative_numbers.py ファイルを作成し、温度、借金、変化を表す変数に負の値を割り当て、その後基本的な算術演算を行って、負数の振る舞いを観察します。

次に、条件文を使用して数値がゼロ未満かどうかをチェックする方法を学びます。これには、「ゼロ未満」のチェックを使用して数値の符号を判断することが含まれます。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/ControlFlowGroup(["Control Flow"]) python/BasicConceptsGroup -.-> python/variables_data_types("Variables and Data Types") python/BasicConceptsGroup -.-> python/numeric_types("Numeric Types") python/ControlFlowGroup -.-> python/conditional_statements("Conditional Statements") subgraph Lab Skills python/variables_data_types -.-> lab-559551{{"Python で数値が負かどうかをチェックする方法"}} python/numeric_types -.-> lab-559551{{"Python で数値が負かどうかをチェックする方法"}} python/conditional_statements -.-> lab-559551{{"Python で数値が負かどうかをチェックする方法"}} end

負数の理解

このステップでは、Python の負数について学びます。負数はゼロ未満の数で、先頭にマイナス記号 (-) が付いて表されます。負数を理解することは、ゼロ以下の温度、借金、または数量の変化を表すなど、様々なプログラミングタスクにおいて重要です。

負数を探索するための Python スクリプトを作成して始めましょう。

  1. LabEx 環境で VS Code エディタを開きます。

  2. ~/project ディレクトリに negative_numbers.py という名前の新しいファイルを作成します。

    ~/project/negative_numbers.py
  3. negative_numbers.py ファイルに以下のコードを追加します。

    ## Assigning negative values to variables
    temperature = -10
    debt = -100
    change = -5
    
    ## Printing the values
    print("Temperature:", temperature)
    print("Debt:", debt)
    print("Change:", change)
    
    ## Performing calculations with negative numbers
    new_temperature = temperature + 5
    remaining_debt = debt + 20
    new_change = change * 2
    
    print("New Temperature:", new_temperature)
    print("Remaining Debt:", remaining_debt)
    print("New Change:", new_change)

    このスクリプトは、変数に負の値を割り当て、それらを使って基本的な算術演算を行う方法を示しています。

  4. negative_numbers.py ファイルを保存します。

  5. ターミナルで python コマンドを使ってスクリプトを実行します。

    python negative_numbers.py

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

    Temperature: -10
    Debt: -100
    Change: -5
    New Temperature: -5
    Remaining Debt: -80
    New Change: -10

    この出力は、初期の負の値と計算結果を示しています。

ゼロ未満のチェックを使用する

このステップでは、Python で条件文を使って数値がゼロ未満かどうかをチェックする方法を学びます。これはプログラミングにおける基本的な概念で、変数の値に基づいて判断を行うために使用されます。

「ゼロ未満」のチェックを実証する Python スクリプトを作成しましょう。

  1. LabEx 環境で VS Code エディタを開きます。

  2. ~/project ディレクトリに less_than_zero.py という名前の新しいファイルを作成します。

    ~/project/less_than_zero.py
  3. less_than_zero.py ファイルに以下のコードを追加します。

    ## Assign a value to a variable
    number = -5
    
    ## Check if the number is less than zero
    if number < 0:
        print("The number is less than zero.")
    else:
        print("The number is not less than zero.")
    
    ## Change the value of the variable
    number = 10
    
    ## Check again if the number is less than zero
    if number < 0:
        print("The number is less than zero.")
    else:
        print("The number is not less than zero.")

    このスクリプトは if 文を使って、number 変数の値がゼロ未満かどうかをチェックします。ゼロ未満の場合、数値がゼロ未満であることを示すメッセージを出力します。そうでない場合、数値がゼロ未満ではないことを示すメッセージを出力します。

  4. less_than_zero.py ファイルを保存します。

  5. ターミナルで python コマンドを使ってスクリプトを実行します。

    python less_than_zero.py

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

    The number is less than zero.
    The number is not less than zero.

    この出力は、スクリプトが数値がゼロ未満かどうかを正しく識別していることを示しています。

ゼロの考慮

このステップでは、Python プログラムにおいてゼロを考慮する方法を学びます。ゼロは正でも負でもない特殊な数です。コード内でゼロを正しく扱うことは、予期しない動作を避けるために重要です。

ゼロを考慮する方法を実証する Python スクリプトを作成しましょう。

  1. LabEx 環境で VS Code エディタを開きます。

  2. ~/project ディレクトリに account_for_zero.py という名前の新しいファイルを作成します。

    ~/project/account_for_zero.py
  3. account_for_zero.py ファイルに以下のコードを追加します。

    ## Assign a value to a variable
    number = 0
    
    ## Check if the number is less than zero
    if number < 0:
        print("The number is less than zero.")
    elif number > 0:
        print("The number is greater than zero.")
    else:
        print("The number is equal to zero.")
    
    ## Change the value of the variable
    number = -3
    
    ## Check again if the number is less than zero
    if number < 0:
        print("The number is less than zero.")
    elif number > 0:
        print("The number is greater than zero.")
    else:
        print("The number is equal to zero.")
    
    ## Change the value of the variable
    number = 5
    
    ## Check again if the number is less than zero
    if number < 0:
        print("The number is less than zero.")
    elif number > 0:
        print("The number is greater than zero.")
    else:
        print("The number is equal to zero.")

    このスクリプトは if-elif-else 文を使って、number 変数の値がゼロ未満、ゼロより大きい、またはゼロに等しいかどうかをチェックします。そして結果を示すメッセージを出力します。

  4. account_for_zero.py ファイルを保存します。

  5. ターミナルで python コマンドを使ってスクリプトを実行します。

    python account_for_zero.py

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

    The number is equal to zero.
    The number is less than zero.
    The number is greater than zero.

    この出力は、スクリプトが数値がゼロ未満、ゼロより大きい、またはゼロに等しいかどうかを正しく識別していることを示しています。

まとめ

この実験では、まず Python での負の数について理解します。負の数はマイナス記号で表されるゼロ未満の数です。negative_numbers.py スクリプトを作成し、温度、借金、変化量などの変数に負の値を割り当て、それらの負の数で基本的な算術演算を行い、初期値と計算結果の両方を出力します。

その後、この実験では、数値がゼロ未満かどうかをチェックするために条件文を使用する概念が導入されます。