complex() 複素数の構築
最終更新:2026-09-22
complex() 複素数の構築
complex() は real + imag*j で複素数を構築する。引数を渡さない場合は 0j を返す。
| カテゴリ | 組み込み関数 |
|---|---|
| 種類 | 組み込み関数 |
| Python バージョン | all |
📝 構文
complex([real[, imag]])
⚙️ 引数
| real 任意 | Real part: a number or numeric string.(デフォルト値:None) |
|---|---|
| imag 任意 | Imaginary part (numeric).(デフォルト値:None) |
戻り値:complex. real + imag*j; 0j when called with no arguments.
💥 例外
TypeError— Raised when an argument cannot be converted to complex.ValueError— Raised when a string cannot be parsed as a complex number.
▶ 例
下のコードを編集し「実行」をクリックすると、出力パネルに結果が表示されます:
実行結果:
(3+4j)
(1+2j)
(5+0j)
💡 関連するケース
さらに多くのケースは近日公開予定です。
❓ よくある質問
Qcomplex() は引数なしで何を返しますか?
A0j を返します。実部と虚部がどちらも 0 の複素数です。
Qcomplex('1+2j') と complex(1, 2) に違いはありますか?
A結果は同じです(どちらも (1+2j))。ただし文字列形式は空白を含められません('1 + 2j' は ValueError)。また文字列と imag 引数を同時に渡すことはできません。
Qcomplex(5) の結果は何ですか?
A5+0j を返します。引数が1つのとき虚部はデフォルトで 0 になります。
Q複素数の実部と虚部を取り出すにはどうすればいいですか?
A.real と .imag 属性を使います。例:(3+4j).real は 3.0、.imag は 4.0 を返します。絶対値(大きさ)は abs() を使います。