一天一点python03(数字和字符串1)

来源:互联网 发布:魔兽世界tcg卡牌淘宝 编辑:程序博客网 时间:2024/05/21 07:46

前两篇都是直接翻译的,以后可能会写成笔记。

 

1.解释器可以直接当做计算器使用:

>>> (50-5*6)/4

5.0

 

2.支持复数,复数的虚部可以用j或者J来表示。写成 (real+imagj),或者也可以用complex函数构造complex(real, imag)。访问实部,虚部用.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)

>>> a=1.5+0.5j

>>> a.real

1.5

>>> a.imag

0.5

 

3.字符串,字符串可以是被双引号或者单引号括起来的内容。字符串中间如果有单引号,那么整个串应该用双引号括起来。如果没有单引号,就可以用单引号。当然,转义符/也是可以用的。

>>> 'spam eggs'

'spam eggs'

>>> 'doesn/'t'

"doesn't"

>>> "doesn't"

"doesn't"

>>> '"Yes," he said.'

'"Yes," he said.'

>>> "/"Yes,/" he said."

'"Yes," he said.'

>>> '"Isn/'t," she said.'

'"Isn/'t," she said.'


超长字符串,这个跟C里面的一样。可以用/来连接。或者可以用一对 “三引号”括起来,这时,每行的'/'可以不用了。:

hello = "This is a rather long string containing/n/

several lines of text just as you would do in C./n/

    Note that whitespace at the beginning of the line is/

significant."

 

print("""
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")

 

我们可以把一个string当做“raw” string,也就是字符串里面是什么,打印出来就是什么:

hello = r"This is a rather long string containing/n/
several lines of text much as you would do in C."

print(hello)

 

结果是:

This is a rather long string containing/n/
several lines of text much as you would do in C.


原创粉丝点击