【Python】abs() 関数の使用例とエラー例

Python の組み込み関数エービーエス abs() の『使用例』と『エラーになったコード例』です。

abs() は、timedelta や complex にも使用できました。

abs() でエラーになったコード例

abs() でエラーになったコードれいです。

Python 公式マニュアル

abs(x)

abs('')
Traceback (most recent call last):
    ...
TypeError: bad operand type for abs(): 'str'
abs('1')
Traceback (most recent call last):
    ...
TypeError: bad operand type for abs(): 'str'
abs(None)
Traceback (most recent call last):
    ...
TypeError: bad operand type for abs(): 'NoneType'
abs(())
Traceback (most recent call last):
    ...
TypeError: bad operand type for abs(): 'tuple'
abs([])
Traceback (most recent call last):
    ...
TypeError: bad operand type for abs(): 'list'
abs(set())
Traceback (most recent call last):
    ...
TypeError: bad operand type for abs(): 'set'
abs({})
Traceback (most recent call last):
    ...
TypeError: bad operand type for abs(): 'dict'
abs(lambda x: x)
Traceback (most recent call last):
    ...
TypeError: bad operand type for abs(): 'function'
abs(range(0, 1))
Traceback (most recent call last):
    ...
TypeError: bad operand type for abs(): 'range'
import datetime
abs(datetime.datetime.max)
Traceback (most recent call last):
    ...
TypeError: bad operand type for abs(): 'datetime.datetime'
import datetime
abs(datetime.date.max)
Traceback (most recent call last):
    ...
TypeError: bad operand type for abs(): 'datetime.date'
import datetime
abs(datetime.time.max)
Traceback (most recent call last):
    ...
TypeError: bad operand type for abs(): 'datetime.time'

int に abs()

整数せいすうintイント に abs() を使用したコード例です。

Python 公式マニュアル

class int([x])

abs(-2)
2
abs(-1)
1
abs(0)
0
abs(1)
1
abs(2)
2

int の文字列化

小数点以下 56 桁まで表示しました。

'%.56f' % abs(-2)
'2.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(-1)
'1.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(0)
'0.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(1)
'1.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(2)
'2.00000000000000000000000000000000000000000000000000000000'

float に abs()

浮動小数点数ふどうしょうすうてんすうfloatフロート に abs() を使用したコード例です。

Python 公式マニュアル

class float([x])

abs(float('nan'))
nan
abs(float('-inf'))
inf
abs(float('inf'))
inf
abs(-0.0)
0.0
abs(-0.1)
0.1
abs(-1.0)
1.0
abs(0.0)
0.0
abs(0.1)
0.1
abs(1.0)
1.0

float の文字列化

小数点以下 56 桁まで表示しました。

'%.56f' % abs(float('nan'))
'nan'
'%.56f' % abs(float('-inf'))
'inf'
'%.56f' % abs(float('inf'))
'inf'
'%.56f' % abs(-0.0)
'0.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(-0.1)
'0.10000000000000000555111512312578270211815834045410156250'
'%.56f' % abs(-1.0)
'1.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(0.0)
'0.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(0.1)
'0.10000000000000000555111512312578270211815834045410156250'
'%.56f' % abs(1.0)
'1.00000000000000000000000000000000000000000000000000000000'

complex に abs()

複素数ふくそすうcomplexコンプレックス に abs() を使用したコード例です。

Python 公式マニュアル

class complex([real[, imag]])

abs(complex(float('nan'), float('nan')))
nan
abs(complex(float('nan'), float('-inf')))
inf
abs(complex(float('nan'), float('inf')))
inf
abs(complex(float('nan'), -0.0))
nan
abs(complex(float('nan'), -0.1))
nan
abs(complex(float('nan'), -1.0))
nan
abs(complex(float('nan'), 0.0))
nan
abs(complex(float('nan'), 0.1))
nan
abs(complex(float('nan'), 1.0))
nan
abs(complex(float('-inf'), float('nan')))
inf
abs(complex(float('-inf'), float('-inf')))
inf
abs(complex(float('-inf'), float('inf')))
inf
abs(complex(float('-inf'), -0.0))
inf
abs(complex(float('-inf'), -0.1))
inf
abs(complex(float('-inf'), -1.0))
inf
abs(complex(float('-inf'), 0.0))
inf
abs(complex(float('-inf'), 0.1))
inf
abs(complex(float('-inf'), 1.0))
inf
abs(complex(float('inf'), float('nan')))
inf
abs(complex(float('inf'), float('-inf')))
inf
abs(complex(float('inf'), float('inf')))
inf
abs(complex(float('inf'), -0.0))
inf
abs(complex(float('inf'), -0.1))
inf
abs(complex(float('inf'), -1.0))
inf
abs(complex(float('inf'), 0.0))
inf
abs(complex(float('inf'), 0.1))
inf
abs(complex(float('inf'), 1.0))
inf
abs(complex(-0.0, float('nan')))
nan
abs(complex(-0.0, float('-inf')))
inf
abs(complex(-0.0, float('inf')))
inf
abs(complex(-0.0, -0.0))
0.0
abs(complex(-0.0, -0.1))
0.1
abs(complex(-0.0, -1.0))
1.0
abs(complex(-0.0, 0.0))
0.0
abs(complex(-0.0, 0.1))
0.1
abs(complex(-0.0, 1.0))
1.0
abs(complex(-0.1, float('nan')))
nan
abs(complex(-0.1, float('-inf')))
inf
abs(complex(-0.1, float('inf')))
inf
abs(complex(-0.1, -0.0))
0.1
abs(complex(-0.1, -0.1))
0.14142135623730953
abs(complex(-0.1, -1.0))
1.004987562112089
abs(complex(-0.1, 0.0))
0.1
abs(complex(-0.1, 0.1))
0.14142135623730953
abs(complex(-0.1, 1.0))
1.004987562112089
abs(complex(-1.0, float('nan')))
nan
abs(complex(-1.0, float('-inf')))
inf
abs(complex(-1.0, float('inf')))
inf
abs(complex(-1.0, -0.0))
1.0
abs(complex(-1.0, -0.1))
1.004987562112089
abs(complex(-1.0, -1.0))
1.4142135623730951
abs(complex(-1.0, 0.0))
1.0
abs(complex(-1.0, 0.1))
1.004987562112089
abs(complex(-1.0, 1.0))
1.4142135623730951
abs(complex(0.0, float('nan')))
nan
abs(complex(0.0, float('-inf')))
inf
abs(complex(0.0, float('inf')))
inf
abs(complex(0.0, -0.0))
0.0
abs(complex(0.0, -0.1))
0.1
abs(complex(0.0, -1.0))
1.0
abs(complex(0.0, 0.0))
0.0
abs(complex(0.0, 0.1))
0.1
abs(complex(0.0, 1.0))
1.0
abs(complex(0.1, float('nan')))
nan
abs(complex(0.1, float('-inf')))
inf
abs(complex(0.1, float('inf')))
inf
abs(complex(0.1, -0.0))
0.1
abs(complex(0.1, -0.1))
0.14142135623730953
abs(complex(0.1, -1.0))
1.004987562112089
abs(complex(0.1, 0.0))
0.1
abs(complex(0.1, 0.1))
0.14142135623730953
abs(complex(0.1, 1.0))
1.004987562112089
abs(complex(1.0, float('nan')))
nan
abs(complex(1.0, float('-inf')))
inf
abs(complex(1.0, float('inf')))
inf
abs(complex(1.0, -0.0))
1.0
abs(complex(1.0, -0.1))
1.004987562112089
abs(complex(1.0, -1.0))
1.4142135623730951
abs(complex(1.0, 0.0))
1.0
abs(complex(1.0, 0.1))
1.004987562112089
abs(complex(1.0, 1.0))
1.4142135623730951

complex の文字列化

小数点以下 56 桁まで表示しました。

'%.56f' % abs(complex(float('nan'), float('nan')))
'nan'
'%.56f' % abs(complex(float('nan'), float('-inf')))
'inf'
'%.56f' % abs(complex(float('nan'), float('inf')))
'inf'
'%.56f' % abs(complex(float('nan'), -0.0))
'nan'
'%.56f' % abs(complex(float('nan'), -0.1))
'nan'
'%.56f' % abs(complex(float('nan'), -1.0))
'nan'
'%.56f' % abs(complex(float('nan'), 0.0))
'nan'
'%.56f' % abs(complex(float('nan'), 0.1))
'nan'
'%.56f' % abs(complex(float('nan'), 1.0))
'nan'
'%.56f' % abs(complex(float('-inf'), float('nan')))
'inf'
'%.56f' % abs(complex(float('-inf'), float('-inf')))
'inf'
'%.56f' % abs(complex(float('-inf'), float('inf')))
'inf'
'%.56f' % abs(complex(float('-inf'), -0.0))
'inf'
'%.56f' % abs(complex(float('-inf'), -0.1))
'inf'
'%.56f' % abs(complex(float('-inf'), -1.0))
'inf'
'%.56f' % abs(complex(float('-inf'), 0.0))
'inf'
'%.56f' % abs(complex(float('-inf'), 0.1))
'inf'
'%.56f' % abs(complex(float('-inf'), 1.0))
'inf'
'%.56f' % abs(complex(float('inf'), float('nan')))
'inf'
'%.56f' % abs(complex(float('inf'), float('-inf')))
'inf'
'%.56f' % abs(complex(float('inf'), float('inf')))
'inf'
'%.56f' % abs(complex(float('inf'), -0.0))
'inf'
'%.56f' % abs(complex(float('inf'), -0.1))
'inf'
'%.56f' % abs(complex(float('inf'), -1.0))
'inf'
'%.56f' % abs(complex(float('inf'), 0.0))
'inf'
'%.56f' % abs(complex(float('inf'), 0.1))
'inf'
'%.56f' % abs(complex(float('inf'), 1.0))
'inf'
'%.56f' % abs(complex(-0.0, float('nan')))
'nan'
'%.56f' % abs(complex(-0.0, float('-inf')))
'inf'
'%.56f' % abs(complex(-0.0, float('inf')))
'inf'
'%.56f' % abs(complex(-0.0, -0.0))
'0.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(complex(-0.0, -0.1))
'0.10000000000000000555111512312578270211815834045410156250'
'%.56f' % abs(complex(-0.0, -1.0))
'1.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(complex(-0.0, 0.0))
'0.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(complex(-0.0, 0.1))
'0.10000000000000000555111512312578270211815834045410156250'
'%.56f' % abs(complex(-0.0, 1.0))
'1.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(complex(-0.1, float('nan')))
'nan'
'%.56f' % abs(complex(-0.1, float('-inf')))
'inf'
'%.56f' % abs(complex(-0.1, float('inf')))
'inf'
'%.56f' % abs(complex(-0.1, -0.0))
'0.10000000000000000555111512312578270211815834045410156250'
'%.56f' % abs(complex(-0.1, -0.1))
'0.14142135623730953120080755525123095139861106872558593750'
'%.56f' % abs(complex(-0.1, -1.0))
'1.00498756211208895017250597447855398058891296386718750000'
'%.56f' % abs(complex(-0.1, 0.0))
'0.10000000000000000555111512312578270211815834045410156250'
'%.56f' % abs(complex(-0.1, 0.1))
'0.14142135623730953120080755525123095139861106872558593750'
'%.56f' % abs(complex(-0.1, 1.0))
'1.00498756211208895017250597447855398058891296386718750000'
'%.56f' % abs(complex(-1.0, float('nan')))
'nan'
'%.56f' % abs(complex(-1.0, float('-inf')))
'inf'
'%.56f' % abs(complex(-1.0, float('inf')))
'inf'
'%.56f' % abs(complex(-1.0, -0.0))
'1.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(complex(-1.0, -0.1))
'1.00498756211208895017250597447855398058891296386718750000'
'%.56f' % abs(complex(-1.0, -1.0))
'1.41421356237309514547462185873882845044136047363281250000'
'%.56f' % abs(complex(-1.0, 0.0))
'1.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(complex(-1.0, 0.1))
'1.00498756211208895017250597447855398058891296386718750000'
'%.56f' % abs(complex(-1.0, 1.0))
'1.41421356237309514547462185873882845044136047363281250000'
'%.56f' % abs(complex(0.0, float('nan')))
'nan'
'%.56f' % abs(complex(0.0, float('-inf')))
'inf'
'%.56f' % abs(complex(0.0, float('inf')))
'inf'
'%.56f' % abs(complex(0.0, -0.0))
'0.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(complex(0.0, -0.1))
'0.10000000000000000555111512312578270211815834045410156250'
'%.56f' % abs(complex(0.0, -1.0))
'1.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(complex(0.0, 0.0))
'0.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(complex(0.0, 0.1))
'0.10000000000000000555111512312578270211815834045410156250'
'%.56f' % abs(complex(0.0, 1.0))
'1.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(complex(0.1, float('nan')))
'nan'
'%.56f' % abs(complex(0.1, float('-inf')))
'inf'
'%.56f' % abs(complex(0.1, float('inf')))
'inf'
'%.56f' % abs(complex(0.1, -0.0))
'0.10000000000000000555111512312578270211815834045410156250'
'%.56f' % abs(complex(0.1, -0.1))
'0.14142135623730953120080755525123095139861106872558593750'
'%.56f' % abs(complex(0.1, -1.0))
'1.00498756211208895017250597447855398058891296386718750000'
'%.56f' % abs(complex(0.1, 0.0))
'0.10000000000000000555111512312578270211815834045410156250'
'%.56f' % abs(complex(0.1, 0.1))
'0.14142135623730953120080755525123095139861106872558593750'
'%.56f' % abs(complex(0.1, 1.0))
'1.00498756211208895017250597447855398058891296386718750000'
'%.56f' % abs(complex(1.0, float('nan')))
'nan'
'%.56f' % abs(complex(1.0, float('-inf')))
'inf'
'%.56f' % abs(complex(1.0, float('inf')))
'inf'
'%.56f' % abs(complex(1.0, -0.0))
'1.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(complex(1.0, -0.1))
'1.00498756211208895017250597447855398058891296386718750000'
'%.56f' % abs(complex(1.0, -1.0))
'1.41421356237309514547462185873882845044136047363281250000'
'%.56f' % abs(complex(1.0, 0.0))
'1.00000000000000000000000000000000000000000000000000000000'
'%.56f' % abs(complex(1.0, 0.1))
'1.00498756211208895017250597447855398058891296386718750000'
'%.56f' % abs(complex(1.0, 1.0))
'1.41421356237309514547462185873882845044136047363281250000'

timedelta に abs()

時間差じかんさtimedeltaタイムデルタ に abs() を使用したコード例です。

Python 公式マニュアル

class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

class str(object='')

repr(object)

import datetime
str(abs(datetime.timedelta(0, 0, 0, 0, 0, 0, 0)))
0:00:00
import datetime
repr(abs(datetime.timedelta(0, 0, 0, 0, 0, 0, 0)))
datetime.timedelta(0)
import datetime
str(abs(datetime.timedelta(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)))
0:00:00
import datetime
repr(abs(datetime.timedelta(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)))
datetime.timedelta(0)
import datetime
str(abs(datetime.timedelta(-1, -1, -1, -1, -1, -1, -1)))
8 days, 1:01:01.001001
import datetime
repr(abs(datetime.timedelta(-1, -1, -1, -1, -1, -1, -1)))
datetime.timedelta(days=8, seconds=3661, microseconds=1001)
import datetime
str(abs(datetime.timedelta(-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0)))
8 days, 1:01:01.001001
import datetime
repr(abs(datetime.timedelta(-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0)))
datetime.timedelta(days=8, seconds=3661, microseconds=1001)
import datetime
str(abs(datetime.timedelta(1, 1, 1, 1, 1, 1, 1)))
8 days, 1:01:01.001001
import datetime
repr(abs(datetime.timedelta(1, 1, 1, 1, 1, 1, 1)))
datetime.timedelta(days=8, seconds=3661, microseconds=1001)
import datetime
str(abs(datetime.timedelta(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0)))
8 days, 1:01:01.001001
import datetime
repr(abs(datetime.timedelta(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0)))
datetime.timedelta(days=8, seconds=3661, microseconds=1001)
import datetime
str(abs(datetime.timedelta(-1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1)))
8 days, 20:19:07.101101
import datetime
repr(abs(datetime.timedelta(-1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1)))
datetime.timedelta(days=8, seconds=73147, microseconds=101101)
import datetime
str(abs(datetime.timedelta(1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1)))
8 days, 20:19:07.101101
import datetime
repr(abs(datetime.timedelta(1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1)))
datetime.timedelta(days=8, seconds=73147, microseconds=101101)
import datetime
str(abs(datetime.timedelta(-500, -500, -500, -500, -500, -500, -500)))
4021 days, 4:28:20.500500
import datetime
repr(abs(datetime.timedelta(-500, -500, -500, -500, -500, -500, -500)))
datetime.timedelta(days=4021, seconds=16100, microseconds=500500)
import datetime
str(abs(datetime.timedelta(500, 500, 500, 500, 500, 500, 500)))
4021 days, 4:28:20.500500
import datetime
repr(abs(datetime.timedelta(500, 500, 500, 500, 500, 500, 500)))
datetime.timedelta(days=4021, seconds=16100, microseconds=500500)
import datetime
str(abs(datetime.timedelta(-500.1, -500.1, -500.1, -500.1, -500.1, -500.1, -500.1)))
4021 days, 23:46:26.600600
import datetime
repr(abs(datetime.timedelta(-500.1, -500.1, -500.1, -500.1, -500.1, -500.1, -500.1)))
datetime.timedelta(days=4021, seconds=85586, microseconds=600600)
import datetime
str(abs(datetime.timedelta(500.1, 500.1, 500.1, 500.1, 500.1, 500.1, 500.1)))
4021 days, 23:46:26.600600
import datetime
repr(abs(datetime.timedelta(500.1, 500.1, 500.1, 500.1, 500.1, 500.1, 500.1)))
datetime.timedelta(days=4021, seconds=85586, microseconds=600600)
スポンサーリンク
Python評価
シェアする(押すとSNS投稿用の『編集ページ』に移動します)
フォローする(RSSフィードに移動します)
スポンサーリンク
シラベルノート
タイトルとURLをコピーしました