我们来尝试一些简单的 Python 命令。启动解释器然后等待主提示符 3.1.1. 数字解释器表现得就像一个简单的计算器:可以向其录入一些表达式,它会给出返回值。表达式语法很直白:运算符 >>> 2+2
4
>>> # This is a comment
... 2+2
4
>>> 2+2 # and a comment on the same line as code
4
>>> (50-5*6)/4
5
>>> # Integer division returns the floor:
... 7/3
2
>>> 7/-3
-3
等号( >>> width = 20
>>> height = 5*9
>>> width * height
900
一个值可以同时赋给几个变量: >>> x = y = z = 0 # Zero x, y and z
>>> x
0
>>> y
0
>>> z
0
变量在使用前必须“定义”(赋值),否则会出错: >>> # try to access an undefined variable
... n
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
浮点数有完整的支持;与整型混合计算时会自动转为浮点数: >>> 3 * 3.75 / 1.5
7.5
>>> 7.0 / 2
3.5
复数也得到支持;带有后缀 >>> 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)
复数的实部和虚部总是记为两个浮点数。要从复数 z 中提取实部和虚部,使用 >>> a=1.5+0.5j
>>> a.real
1.5
>>> a.imag
0.5
浮点数和整数之间的转换函数( >>> 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
交互模式中,最近一个表达式的值赋给变量 >>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
此变量对于用户是只读的。不要尝试给它赋值 —— 你只会创建一个独立的同名局部变量,它屏蔽了系统内置变量的魔术效果。 3.1.2. 字符串相比数值,Python 也提供了可以通过几种不同方式传递的字符串。它们可以用单引号或双引号标识: >>> '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.'
Python 解释器按照字符串被输入的方式打印字符串结果:为了显示准确的值,字符串包含在成对的引号中,引号和其他特殊字符要用反斜线( \ )转译。 如果字符串只包含单引号( ‘ )而没有双引号( ” )就可以用双引号( ” )包围,反之用单引号( ‘ )包围。 再强调一下, 字符串文本有几种方法分行。可以使用反斜杠为行结尾的连续字符串,它表示下一行在逻辑上是本行的后续内容: 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 hello
需要注意的是,还是需要在字符串中写入 This is a rather long string containing
several lines of text just as you would do in C.
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
"""
得到如下输出: Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
如果我们生成一个“原始”字符串, 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.
字符串可以由 >>> word = 'Help' + 'A'
>>> word
'HelpA'
>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'
相邻的两个字符串文本自动连接在一起,前面那行代码也可以写为 >>> 'str' 'ing' # <- This is ok
'string'
>>> 'str'.strip() + 'ing' # <- This is ok
'string'
>>> 'str'.strip() 'ing' # <- This is invalid
File "<stdin>", line 1, in ?
'str'.strip() 'ing'
^
SyntaxError: invalid syntax |
Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )
GMT+8, 2024-9-21 03:35 , Processed in 0.071039 second(s), 27 queries .