Python の組み込み関数コンプレックス complex() の『使用例』と『エラーになったコード例』です。
complex() でエラーになったコード例
complex() でエラーになったコード例です。
(Python) class complex([real[, imag]])
complex(None) # NoneType を渡したらエラーになった。
TypeError: complex() first argument must be a string or a number, not 'NoneType'
complex(b'') # bytes を渡したらエラーになった。
TypeError: complex() first argument must be a string or a number, not 'bytes'
(Python) class bytes([source[, encoding[, errors]]])
complex(bytearray(b'')) # bytearray を渡したらエラーになった。
TypeError: complex() first argument must be a string or a number, not 'bytearray'
(Python) class bytearray([source[, encoding[, errors]]])
complex(()) # tuple を渡したらエラーになった。
TypeError: complex() first argument must be a string or a number, not 'tuple'
complex([]) # list を渡したらエラーになった。
TypeError: complex() first argument must be a string or a number, not 'list'
(Python) class list([iterable])
complex(set()) # set を渡したらエラーになった。
TypeError: complex() first argument must be a string or a number, not 'set'
(Python) class set([iterable])
complex({}) # dict を渡したらエラーになった。
TypeError: complex() first argument must be a string or a number, not 'dict'
complex(range(0,1)) # range を渡したらエラーになった。
TypeError: complex() first argument must be a string or a number, not 'range'
(Python) range(start, stop[, step])
complex(lambda x: x) # function を渡したらエラーになった。
TypeError: complex() first argument must be a string or a number, not 'function'
import datetime
complex(datetime.datetime.max) # datetime を渡したらエラーになった。
TypeError: complex() first argument must be a string or a number, not 'datetime.datetime'
import datetime
complex(datetime.date.max) # date を渡したらエラーになった。
TypeError: complex() first argument must be a string or a number, not 'datetime.date'
(Python) class datetime.date(year, month, day)
import datetime
complex(datetime.time.max) # time を渡したらエラーになった。
TypeError: complex() first argument must be a string or a number, not 'datetime.time'
(Python) class datetime.time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
import datetime
complex(datetime.timedelta(0)) # timedelta を渡したらエラーになった。
TypeError: complex() first argument must be a string or a number, not 'datetime.timedelta'
引数無し
complex() を引数無しで使用したコード例です。
(Python) class complex([real[, imag]])
complex()
0j
complex() に str
complex() に文字列の str を使用したコード例です。
(Python) class complex([real[, imag]])
complex('1j')
1j
complex(' -1j ')
-1j
complex(' -1 j ')
ValueError: complex() arg is a malformed string
complex('1.j')
1j
complex('1.0j')
1j
complex('1.2j')
1.2j
complex('0j')
0j
complex('-0j')
-0j
complex('- 0j')
ValueError: complex() arg is a malformed string
complex('0.j')
0j
complex('0.0j')
0j
complex('-0.0j')
-0j
complex('-infj')
-infj
complex('nanj')
nanj
complex('-1-1j')
(-1-1j)
complex('-1.-1.j')
(-1-1j)
complex('-1.0-1.0j')
(-1-1j)
complex('-1.0 -1.0j')
ValueError: complex() arg is a malformed string
complex('-1.2-1.2j')
(-1.2-1.2j)
complex('0+0j')
0j
complex('-0-0j')
(-0-0j)
complex('0.0+0.0j')
0j
complex('-0.0-0.0j')
(-0-0j)
complex(' -1-1j ')
(-1-1j)
complex('- 1 - 1 j')
ValueError: complex() arg is a malformed string
complex('-1 -1j')
ValueError: complex() arg is a malformed string
complex('-1 -1 j')
ValueError: complex() arg is a malformed string
complex('-inf+1j')
(-inf+1j)
complex('1-infj')
(1-infj)
complex('nan+1j')
(nan+1j)
complex('1+nanj')
(1+nanj)
complex('nan+infj')
(nan+infj)
complex('inf+nanj')
(inf+nanj)
complex('nan+nanj')
(nan+nanj)
complex('-nan-nanj')
(nan+nanj)
complex() に bool
complex() に真理値の bool を使用したコード例です。
(Python) class complex([real[, imag]])
実部 (real) に bool を渡した場合
complex(True)
(1+0j)
complex(False)
0j
虚部 (imag) に bool を渡した場合
complex(imag=True)
1j
complex(imag=False)
0j
実部と虚部に bool を渡した場合
complex(True, True)
(1+1j)
complex(False, False)
0j
complex() に int
complex() に整数の int を使用したコード例です。
(Python) class complex([real[, imag]])
実部 (real) に int を渡した場合
complex(1)
(1+0j)
complex(0)
0j
complex(-1)
(-1+0j)
虚部 (imag) に int を渡した場合
complex(imag=1)
1j
complex(imag=0)
0j
complex(imag=-1)
-1j
実部と虚部に int を渡した場合
complex(1, 1)
(1+1j)
complex(0, 0)
0j
complex(-1, -1)
(-1-1j)
complex() に float
complex() に浮動小数点数の float を使用したコード例です。
(Python) class complex([real[, imag]])
実部 (real) に float を渡した場合
complex(1.0)
(1+0j)
complex(0.1)
(0.1+0j)
complex(0.0)
0j
complex(-1.0)
(-1+0j)
complex(-0.1)
(-0.1+0j)
complex(-0.0)
(-0+0j)
complex(float('inf'))
(inf+0j)
complex(float('-inf'))
(-inf+0j)
complex(float('nan'))
(nan+0j)
虚部 (imag) に float を渡した場合
complex(imag=1.0)
1j
complex(imag=0.1)
0.1j
complex(imag=0.0)
0j
complex(imag=-1.0)
-1j
complex(imag=-0.1)
-0.1j
complex(imag=-0.0)
-0j
complex(imag=float('inf'))
infj
complex(imag=float('-inf'))
-infj
complex(imag=float('nan'))
nanj
実部と虚部に float を渡した場合
complex(1.0, 1.0)
(1+1j)
complex(0.1, 0.1)
(0.1+0.1j)
complex(0.0, 0.0)
0j
complex(-1.0, -1.0)
(-1-1j)
complex(-0.1, -0.1)
(-0.1-0.1j)
complex(-0.0, -0.0)
(-0-0j)
complex(float('inf'), float('inf'))
(inf+infj)
complex(float('-inf'), float('-inf'))
(-inf-infj)
complex(float('nan'), float('nan'))
(nan+nanj)
complex() に complex
complex() に複素数の complex を使用したコード例です。
(Python) class complex([real[, imag]])
実部 (real) に complex を渡した場合
complex(complex(-1, 5))
(-1+5j)
complex(complex(-0.0, 5))
(-0+5j)
complex(complex(0, 5))
5j
complex(complex(1, 5))
(1+5j)
complex(complex(float('nan'), 5))
(nan+5j)
complex(complex(float('inf'), 5))
(inf+5j)
complex(complex(float('-inf'), 5))
(-inf+5j)
complex(complex('-inf', 5))
TypeError: complex() can't take second arg if first is a string
complex(complex(5, '-inf'))
TypeError: complex() second arg can't be a string
complex(complex(5, '7'))
TypeError: complex() second arg can't be a string
complex(complex(5, -1))
(5-1j)
complex(complex(5, -0.0))
(5-0j)
complex(complex(5, 0))
(5+0j)
complex(complex(5, 1))
(5+1j)
complex(complex(5, float('nan')))
(5+nanj)
complex(complex(5, float('inf')))
(5+infj)
complex(complex(5, float('-inf')))
(5-infj)
complex(complex(float('nan'), float('nan')))
(nan+nanj)
虚部 (imag) に complex を渡した場合
complex(imag=complex(-1, 5))
(-5-1j)
complex(imag=complex(-0.0, 5))
(-5-0j)
complex(imag=complex(0, 5))
(-5+0j)
complex(imag=complex(1, 5))
(-5+1j)
complex(imag=complex(float('nan'), 5))
(-5+nanj)
complex(imag=complex(float('inf'), 5))
(-5+infj)
complex(imag=complex(float('-inf'), 5))
(-5-infj)
complex(imag=complex('-inf', 5))
TypeError: complex() can't take second arg if first is a string
complex(imag=complex(5, '-inf'))
TypeError: complex() second arg can't be a string
complex(imag=complex(5, '7'))
TypeError: complex() second arg can't be a string
complex(imag=complex(5, -1))
(1+5j)
complex(imag=complex(5, -0.0))
5j
complex(imag=complex(5, 0))
5j
complex(imag=complex(5, 1))
(-1+5j)
complex(imag=complex(5, float('nan')))
(nan+5j)
complex(imag=complex(5, float('inf')))
(-inf+5j)
complex(imag=complex(5, float('-inf')))
(inf+5j)
complex(imag=complex(float('nan'), float('nan')))
(nan+nanj)
実部と虚部に complex を渡した場合
complex(complex(-1, 5), complex(-1, 5))
(-6+4j)
complex(complex(-0.0, 5), complex(-0.0, 5))
(-5+5j)
complex(complex(0, 5), complex(0, 5))
(-5+5j)
complex(complex(1, 5), complex(1, 5))
(-4+6j)
complex(complex(float('nan'), 5), complex(float('nan'), 5))
(nan+nanj)
complex(complex(float('inf'), 5), complex(float('inf'), 5))
(inf+infj)
complex(complex(float('-inf'), 5), complex(float('-inf'), 5))
(-inf-infj)
complex(complex('-inf', 5), complex('-inf', 5))
TypeError: complex() can't take second arg if first is a string
complex(complex(5, '-inf'), complex(5, '-inf'))
TypeError: complex() second arg can't be a string
complex(complex(5, '7'), complex(5, '7'))
TypeError: complex() second arg can't be a string
complex(complex(5, -1), complex(5, -1))
(6+4j)
complex(complex(5, -0.0), complex(5, -0.0))
(5+5j)
complex(complex(5, 0), complex(5, 0))
(5+5j)
complex(complex(5, 1), complex(5, 1))
(4+6j)
complex(complex(5, float('nan')), complex(5, float('nan')))
(nan+nanj)
complex(complex(5, float('inf')), complex(5, float('inf')))
(-inf+infj)
complex(complex(5, float('-inf')), complex(5, float('-inf')))
(inf-infj)
complex(complex(float('nan'), float('nan')), complex(float('nan'), float('nan')))
(nan+nanj)