GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 首页
  • HTML
  • JavaScript
  • CSS
  • React
  • Python3
  • Java
  • C
  • C++
  • C#
  • Go
  • SQL
  • Linux
  • VS Code

This view

  • Original page
  • New address

GeezerVison is showing www.runoob.com — python 不支持复数转换为整数或浮点数

菜鸟教程 -- 学的不仅是技术,更是梦想!

  • Vue3 教程
  • Vue2 教程
  • Bootstrap3
  • Bootstrap4
  • Bootstrap5
  • 机器学习
  • PyTorch
  • TensorFlow
  • Sklearn
  • NLP
  • AI Agent
  • Ollama
  • Coding Plan

python 不支持复数转换为整数或浮点数

原文: Python3 数字(Number)

   我无所谓

python 不支持复数转换为整数或浮点数

>>> float(4.5+0j)
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    float(4.5+0j)
TypeError: can't convert complex to float

>>> int(4+0j)
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    int(4+0j)
TypeError: can't convert complex to int
>>>
更多解析

  LL_NUAA

针对前面有人提到复数不能强转为int或者float的问题:

其实在Python中,复数提供了2个函数,一个函数是real,返回复数的实数部分,另一个函数是imag,返回复数的虚数部分。因为实数跟复数是差异很大的类型,所以不支持强制转换是可以理解的。因为在强制转换过程中,虚数部分到底该怎么转换,是没有定义的,而int和float只有实数部分,虚数部分该如何取舍?

>>> a = 4.1+0.3j
>>> a
(4.1+0.3j)
>>> a.real
4.1
>>> a.imag
0.3
热门文章
  • 2024 年 04 月 JavaScript 框架排行榜
  • 免费、开源、好用的 SQL 客户端合集
  • 优雅的代码规范,提升代码质量
  • 命令行中的趣味世界,牛也能说话,有趣又好玩
  • 2024 年 05 月编程语言排行榜,Fortran 进入前 10 名,发生了什么?
  • 免费开源的 IDE、数据库管理工具、SSH客户端和FTP客户端
教程列表
ADO 教程 AI Agent 教程 AI 入门教程 AI 应用教程 AI 数学基础 Ajax 教程 Android 教程 Angular2 教程 AngularJS 教程 AppML 教程 ASP 教程 ASP.NET 教程 Bootstrap 教程 Bootstrap4 教程 Bootstrap5 教程 Bootstrap6 教程 C 教程 C 语言数据结构与算法 C# 教程 C++ 教程 Chart.js 教程 Claude Code 教程 CMake 教程 Codex 教程 CSS 参考手册 CSS 教程 CSS3 教程 Cursor 教程 Dart 教程 Dash 教程 DeepSeek Harness Django 教程 Docker 教程 DTD 教程 ECharts 教程 Eclipse 教程 Electron 教程 FastAPI 教程 Firebug 教程 Flask 教程 Flutter 教程 Font Awesome 图标 Foundation 教程 Git 教程 Go 语言教程 Google 地图 API 教程 Hermes Agent Highcharts 教程 HTML DOM 教程 HTML 参考手册 HTML 字符集 HTML 教程 HTTP 教程 ionic 教程 iOS 教程 Java 教程 JavaScript 参考手册 Javascript 教程 jQuery EasyUI 教程 jQuery Mobile 教程 jQuery UI 教程 jQuery 教程 JSON 教程 JSP 教程 Julia 教程 Jupyter Notebook 教程 Kotlin 教程 LangChain 教程 LaTeX 教程 Linux 教程 Lua 教程 Markdown 教程 Matplotlib 教程 Maven 教程 Memcached 教程 MongoDB 教程 MySQL 教程 Next.js 教程 NLP 教程 Node.js 教程 NumPy 教程 Obsidian 教程 Ollama 教程 OpenCode 教程 OpenCV 教程 Pandas 教程 Perl 教程 PHP 教程 Pi Agent Pillow 教程 Playwright 教程 PostgreSQL 教程 PowerShell Pycharm 教程 Python 3 教程 Python 基础教程 Python 设计模式 Python 量化交易 PyTorch 教程 R 教程 RDF 教程 React 教程 Redis 教程 RESTful API RSS 教程 Ruby 教程 Rust 教程 Sass 教程 Scala 教程 SciPy 教程 Selenium 教程 Servlet 教程 Skills 教程 Sklearn 教程 SOAP 教程 SQL 教程 SQLite 教程 SVG 教程 SVN 教程 Swagger 教程 Swift 教程 Tailwind CSS 教程 TCP/IP 教程 TensorFlow 教程 Three.js 教程 TypeScript 教程 uni-app 教程 Unity 教程 VBScript 教程 Vibe Coding 教程 VSCode 教程 Vue.js 教程 Vue3 教程 W3C 教程 Web Service 教程 WSDL 教程 XLink 教程 XML DOM 教程 XML Schema 教程 XML 教程 XPath 教程 XQuery 教程 XSLFO 教程 XSLT 教程 Zig 教程 数据结构 机器学习 正则表达式 汇编语言 测验 浏览器 网站品质 网站建设指南 网站服务器教程 网络协议 计算机组成原理 设计模式
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.