Python の組み込み関数 oct の使い方です。
oct 関数で、整数を『8 進数 (octal)』で表現するコード例と、実行結果を載せました。
oct 関数は、整数を 8 進数の文字列に変換してくれました。
(例)oct(8) -> '0o10'
あと、『エラーになったコード例』も書きました。
エラーになったコード例
oct 関数で、エラーになったコード例です。
oct 関数に整数でないものを渡したら、『整数として解釈できませんでした』という旨のエラーメッセージが出ました。
oct('') # '' 空文字列
TypeError: 'str' object cannot be interpreted as an integer
oct('5') # '5'
TypeError: 'str' object cannot be interpreted as an integer
oct('a') # 'a'
TypeError: 'str' object cannot be interpreted as an integer
oct(1.2) # 1.2
TypeError: 'float' object cannot be interpreted as an integer
oct(complex(3, 4)) # (3+4j)
TypeError: 'complex' object cannot be interpreted as an integer
oct(None) # None
TypeError: 'NoneType' object cannot be interpreted as an integer
oct(()) # ()
TypeError: 'tuple' object cannot be interpreted as an integer
oct([]) # []
TypeError: 'list' object cannot be interpreted as an integer
oct(set()) # set()
TypeError: 'set' object cannot be interpreted as an integer
oct({}) # {}
TypeError: 'dict' object cannot be interpreted as an integer
oct(range(0, 1)) # range(0, 1)
TypeError: 'range' object cannot be interpreted as an integer
oct(lambda x: x) # <function <lambda> at 0x0000000000000000>
TypeError: 'function' object cannot be interpreted as an integer
oct(bytes([5])) # b'\x05'
TypeError: 'bytes' object cannot be interpreted as an integer
oct(bytearray([5])) # bytearray(b'\x05')
TypeError: 'bytearray' object cannot be interpreted as an integer
from decimal import Decimal
oct(Decimal(1)) # Decimal('1')
TypeError: 'decimal.Decimal' object cannot be interpreted as an integer
import datetime
oct(datetime.datetime.max) # datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)
TypeError: 'datetime.datetime' object cannot be interpreted as an integer
import datetime
oct(datetime.timedelta.max) # datetime.timedelta(days=999999999, seconds=86399, microseconds=999999)
TypeError: 'datetime.timedelta' object cannot be interpreted as an integer
bool を渡した結果
oct 関数に、bool(真理値)を渡した結果です。
oct(False) # False
'0o0'
oct(True) # True
'0o1'
int を渡した結果
oct 関数に、int(整数)を渡した結果です。
oct(8) # 8
'0o10'
oct(7) # 7
'0o7'
oct(1) # 1
'0o1'
oct(0) # 0
'0o0'
oct(-1) # -1
'-0o1'
oct(-7) # -7
'-0o7'
oct(-8) # -8
'-0o10'
ほかの方法
oct 関数の代わりに、
- format 関数
str.format
メソッドf''
%
演算子
を使用して、8 進数の文字列に変換することもできました。
接頭辞についてです。
8 進数の先頭に '0o'
を付けることもできましたし、逆に、'0o'
を付けずに、8 進数の部分だけを取得することもできました。
8 -> '0o10'
8 進数の先頭に、接頭辞の '0o'
を付けて変換するコード例です。
format 関数の実行結果です。
(Python) format(value[, format_spec])
format(8, '#o')
'0o10'
format(7, '#o')
'0o7'
format(0, '#o')
'0o0'
format(-7, '#o')
'-0o7'
format(-8, '#o')
'-0o10'
str.format
メソッドの実行結果です。
(Python) str.format(*args, **kwargs)
'{n:#o}'.format(n=8)
'0o10'
'{n:#o}'.format(n=7)
'0o7'
'{n:#o}'.format(n=0)
'0o0'
'{n:#o}'.format(n=-7)
'-0o7'
'{n:#o}'.format(n=-8)
'-0o10'
f''
の実行結果です。
(Python) フォーマット済み文字列リテラル (f-string, f''
)
f'{8:#o}'
'0o10'
f'{7:#o}'
'0o7'
f'{0:#o}'
'0o0'
f'{-7:#o}'
'-0o7'
f'{-8:#o}'
'-0o10'
%
演算子の実行結果です。
(Python) printf 形式の文字列書式化(%
演算子)
'%#o' % 8
'0o10'
'%#o' % 7
'0o7'
'%#o' % 0
'0o0'
'%#o' % -7
'-0o7'
'%#o' % -8
'-0o10'
8 -> '10' 接頭辞 '0o' 無し
8 進数の先頭に、接頭辞の '0o'
を付けないようにして変換するコード例です。
『8 進数の部分だけ』の文字列を取得することができました。
当然のことながら、結果だけを見たら、もはや何進数の数なのか、わからなくなっていました。
format 関数の実行結果です。
(Python) format(value[, format_spec])
format(8, 'o')
'10'
format(7, 'o')
'7'
format(0, 'o')
'0'
format(-7, 'o')
'-7'
format(-8, 'o')
'-10'
str.format
メソッドの実行結果です。
(Python) str.format(*args, **kwargs)
'{n:o}'.format(n=8)
'10'
'{n:o}'.format(n=7)
'7'
'{n:o}'.format(n=0)
'0'
'{n:o}'.format(n=-7)
'-7'
'{n:o}'.format(n=-8)
'-10'
f''
の実行結果です。
(Python) フォーマット済み文字列リテラル (f-string, f''
)
f'{8:o}'
'10'
f'{7:o}'
'7'
f'{0:o}'
'0'
f'{-7:o}'
'-7'
f'{-8:o}'
'-10'
%
演算子の実行結果です。
(Python) printf 形式の文字列書式化(%
演算子)
'%o' % 8
'10'
'%o' % 7
'7'
'%o' % 0
'0'
'%o' % -7
'-7'
'%o' % -8
'-10'
以上です。