工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
home  MfgRobots >> 工業製造 >  >> Industrial programming >> Python

Python abs() 関数:絶対値の例

Python abs()

Python abs() Python の標準ライブラリで利用できる組み込み関数です。指定された数値の絶対値を返します。数値の絶対値は、符号を考慮しない値です。数値は、整数、浮動小数点数、または複素数にすることができます。指定された数値が複素数の場合、その大きさを返します。

構文:

abs(value)

パラメータ:(値)

絶対値を取得するために abs() に与えられる入力値。整数、浮動小数点数、または複素数を指定できます。

戻り値:

指定された数値の絶対値を返します。

例:

コード例 1:整数と浮動小数点数

整数と浮動小数点数の絶対値を取得するには、次のコードを確認してください:

# testing abs() for an integer and float

int_num = -25

float_num = -10.50

print("The absolute value of an integer number is:", abs(int_num))
print("The absolute value of a float number is:", abs(float_num))

出力:

The absolute value of an integer number is: 25
The absolute value of a float number is: 10.5

例 2:複素数

複素数の絶対値を取得する

# testing abs() for a complex number

complex_num = (3+10j)

print("The magnitude of the complex number is:", abs(complex_num))

出力:

The magnitude of the complex number is: 10.44030650891055

まとめ:


Python

  1. Python 変数、定数、およびリテラル
  2. Python 関数の引数
  3. Python 匿名/Lambda 関数
  4. Python ジェネレーター
  5. Python クロージャー
  6. Python デコレータ
  7. Python Print() ステートメント:例を使用して印刷する方法
  8. 例を使用した Python 文字列 strip() 関数
  9. 例を使用した Python 文字列 count()
  10. Python String format() 例で説明
  11. Python String find() メソッドと例