+ -
当前位置:首页 → 问答吧 → [Help] 请教关於Python教程中的一些部分

[Help] 请教关於Python教程中的一些部分

时间:2013-05-23

来源:互联网

<最新问题的超连结>



Question 1 : (已解决)
电子书原文:

Complex numbers are also supported;
imaginary numbers are written with a suffix of j or J.
Complex numbers with a nonzero real component are written as (real+imagj),
or can be created
with the complex(real, imag) function. :

复数也得到支持;带有后缀j或J就被视为虚数。
带有非零实部的复数写为(real + imagj),
或者可以用complex(real, imag)函数创建:


>>> 1j * 1J
(-1+0j)

>>> 1j * complex(0,1)
(-1+0j)

>>> 3+1j*3
(3+3j)

>>> (3+1j)*3
(9+3j)

>>> (1+2j)/(1+1j)
(1.5+0.5j)


小弟不知道作者做紧什么,可以有人教我吗?
什么是j,J,虚数...?

个人背景资料:
小弟刚完成DSE考试,希望投入自己有兴趣的学科,向Programmer进发,
咁多种Languages,对Python兴趣最大,希望长期浸淫钻研 =]
如果可以,肯请各位高手长期地、乐意地解答小弟关於Programming的问题。
感谢各位帮助!!

[ 本帖最后由 rtrtrt2006 於 2013-5-23 02:51 PM 编辑 ]

作者: rtrtrt2006   发布时间: 2013-05-23

作者: lamjohnny2009   发布时间: 2013-05-23

引用:
原帖由 lamjohnny2009 於 23-5-2013 01:07 AM 发表
你系咪睇哩个?:
http://atedev.wordpress.com/2008 ... %E8%A8%88%E7%AE%97/
非也,我在看一本电子书:
Python Tutorial
Release 2.7 <October 26, 2010>
作者:March Liu

作者: rtrtrt2006   发布时间: 2013-05-23

睇埋下面两个应该就会明
http://f.dataguru.cn/thread-90572-1-1.html
http://f.dataguru.cn/thread-39999-1-1.html

复数的概念
在很久以前,数学家们被下面的等式困扰。
x2=-1
这是因为任何实数(无论正负)乘以自己总会得到一个非负数。一个数怎么可以乘以自己得到一负数?没有这样的实数存在。就这样18世纪,数学家们发了一个虚拟的数i(或者j,不同的教材不同)Python中的算数 虚数不能单独存在,它们总是和一个值为0.0的实数部分一起构成一个复数 表示虚数的语法:real+imagj 实数部分和虚数部分都是浮点数 虚数部分必须有j或J 下面是些得数: 64.23+1j 4.34-8.5j 0.23-8.33j 1.23e-0.45+6.5e+0.83j -1.23-3.5j -0.34-0j================================================================数学上的复数表示,实部+虚部i: C1=a+bi, C2=c+dipython上的复数表示,实部+虚部j,虚部用数字后+j代替:C1=a+bj C2=c+dj以下用python表示法举例说明虚数的四则运算法则。一、两虚数加减法结果为一虚数,等於两虚数的实部与实部、虚部与虚部分别相加减:   C1± C2=(a+c)±(b+d)j二、两虚数相乘提示: j^2代表数学上的虚数单位i的平方,等於-1C1*C2=(a+bj)*(c+dj)=ac+adj+bcj+ bdj^2=(ac-bd) +(ad+bc)j

[ 本帖最后由 lamjohnny2009 於 2013-5-23 01:17 AM 编辑 ]

作者: lamjohnny2009   发布时间: 2013-05-23

DISCUSS HK...出POST...格式走哂...冇得顶

作者: lamjohnny2009   发布时间: 2013-05-23

引用:
原帖由 lamjohnny2009 於 23-5-2013 01:13 AM 发表
睇埋下面两个应该就会明
http://f.dataguru.cn/thread-90572-1-1.html
http://f.dataguru.cn/thread-39999-1-1.html

复数的概念
在很久以前,数学家们被下面的等式困扰。
x2=-1
这是因为任何实数(无论正负 ...
我好像已经明解了,万分感激!!
如果Python用i,我会比较早明解,学校都教i。

[ 本帖最后由 rtrtrt2006 於 2013-5-23 01:26 AM 编辑 ]

作者: rtrtrt2006   发布时间: 2013-05-23

Question 2 : (已解决)
电子书原文:

The conversion functions to floating point and integer (float(), int() and long()) don't work
for complex numbers --- there is no one correct way to convert a complex number to a real number.
Use abs(z) to get its magnitude (as a float) or z.real to get its real part. :

浮点数和整数之间的转换函数( float() 和 int() 以及 long() ) 不能用于复数。
没有什么正确方法可以把一个复数转成一个实 数。
函数 abs(z) 用于获取其模(浮点数)或 z.real 获取其实部

>>> a=3.0+4.0j

>>> float(a)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: can't convert complex to float; use abs(z)

>>> a.real
3.0

>>> a.imag
4.0

>>> abs(a) # sqrt(a.real**2 + a.imag**2)
5.0


abs()是用来做什么?
求(3.0+4.0j)的绝对值?

[ 本帖最后由 rtrtrt2006 於 2013-5-23 02:38 PM 编辑 ]

作者: rtrtrt2006   发布时间: 2013-05-23

引用:
原帖由 rtrtrt2006 於 2013-5-23 01:47 AM 发表
Question 2 :
电子书原文:

The conversion functions to floating point and integer (float(), int() and long()) don't work
for complex numbers --- there is no one correct way to convert a complex n ...
abs() 用法:
http://pydoing.blogspot.hk/2011/02/python-abs.html

Google 一下吧= =

作者: lamjohnny2009   发布时间: 2013-05-23

看看 复数的绝对值 果栏
https://zh.wikipedia.org/wiki/%E7%BB%9D%E5%AF%B9%E5%80%BC

作者: lamjohnny2009   发布时间: 2013-05-23

abs 以数学表达就系 | x | , 在 physics 有时代表搵 magnitude.

对 complex number 来讲, 都是搵 magnitude. Complex number 对 magnitude 的定义就是如 comment 所示.
引用:
原帖由 rtrtrt2006 於 2013-5-23 01:47 发表
Question 2 :
电子书原文:

The conversion functions to floating point and integer (float(), int() and long()) don't work
for complex numbers --- there is no one correct way to convert a complex n ...

作者: a8d7e8   发布时间: 2013-05-23

引用:
原帖由 lamjohnny2009 於 23-5-2013 02:28 AM 发表


abs() 用法:
http://pydoing.blogspot.hk/2011/02/python-abs.html

Google 一下吧= =
print (abs(True))  =  1
print (abs(False)) =  0
呢2个我未知点用住,记左佢先?

[ 本帖最后由 rtrtrt2006 於 2013-5-23 02:01 PM 编辑 ]

作者: rtrtrt2006   发布时间: 2013-05-23

引用:
原帖由 rtrtrt2006 於 2013-5-23 01:31 PM 发表



print (abs(True))  =  1
print (abs(False)) =  0
呢2个我未知点用住,记左佢先?
呢两个冇咩意思
一般True都隐性替代1
False隐性替代0
所以abs 1 = 1和abs 0 = 0



作者: cheng_chai_fung   发布时间: 2013-05-23

引用:
原帖由 cheng_chai_fung 於 23-5-2013 02:16 PM 发表
呢两个冇咩意思
一般True都隐性替代1
False隐性替代0
所以abs 1 = 1和abs 0 = 0



明解了 Thx


Question 3 :
电子书原文:

The interpreter prints the result of string operations in the same way as they are typed for
input: inside quotes, and with quotes and other funny characters escaped by backslashes, to show
the precise value. The string is enclosed in double quotes if the string contains a single quote
and no double quotes, else it's enclosed in single quotes.
(The print statement, described
later, can be used to write strings without quotes or escapes.)

解释器打印的字符串操作结果与它们输入时的方式一致:
以括号标识,包含反斜 杠转义的有趣的字符,
以精确的显示值。
如果字符串包含单引号,不包含双引号,
它就以双引号标识。否则它以单引号标识。
(后面介绍的 print 语句,可以输出没有标识和转义的字符串。)

Strings can be concatenated (glued together) with the + operator, and repeated with *:
字符串可以由 + 操作符连接(粘到一起),可以由 * 重复

>>> word = 'Help' + 'A'
>>> word
'HelpA'

>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'


a. 「"」和「'」,它们是不是可被当为完全相同? 只要「""」、「''」的一起用就可以 (不能「"'」、「'"」)

b. 「'<'+」和「+'>'」中间的String就可以用「*5」来重复5次、「*20」来重复20次、「*100」来重复100次。
    我不太了解这个指令结构的打法。
    总之打'<'+String*number+'>'  or  "<"+String*number+">"就可行了?
e.g. (测试於Python 3.3.2)
>>> word = "love"+"you"
>>> word
'loveyou'

>>> "<"+word*2+">"
'<loveyouloveyou>'

>>> '<' + word*5 + '>'
'<loveyouloveyouloveyouloveyouloveyou>'

>>> "<" + word*5 + ">"
'<loveyouloveyouloveyouloveyouloveyou>'

>>> "<"+"Loveyou"*5+">"
'<LoveyouLoveyouLoveyouLoveyouLoveyou>'

>>> "<"+"Loveyou"*2+">"
'<LoveyouLoveyou>'

>>> "<"+"Loveyou"*2+">"
'<LoveyouLoveyou>'

>>> "<+word*2+>"
'<+word*2+>'

>>> <"+word*2+">
SyntaxError: invalid syntax

>>> "<"-word*2-">"
Traceback (most recent call last):
  File "<pyshell#51>", line 1, in <module>
    "<"-word*2-">"
TypeError: unsupported operand type(s) for -: 'str' and 'str'

>>> "<"+word*2+">'
SyntaxError: EOL while scanning string literal

[ 本帖最后由 rtrtrt2006 於 2013-5-23 02:54 PM 编辑 ]

作者: rtrtrt2006   发布时间: 2013-05-23

热门下载

更多