本人使用的Python版本在3.0以上。一些参考网站:
http://docs.pythontab.com/python/python3.4/index.html
http://www.pythoner.cn/
Python基本运算:
如:
<span style="font-size:18px;">>>> 2+3 #加法
5
>>> 2-3 #减法
-1
>>> 2*3 #乘法
6
>>> 4/2 #除法
2.0
>>> 4//2 #整除
2
>>> 4%3 #取余
1
>>> 2**5 #幂运算
32
>>> 2.0+3/1 #类型自动转换
5.0
>>> </span>
复数也得到支持;带有后缀 j 或 J 就被视为虚数。带有非零实部的复数写为 (real+imagj) ,或者可以用 complex(real, imag) 函数创建。
如:
>>> a=1.5+0.5j
>>> a.real #实部
1.5
>>> a.imag #虚部
0.5
>>> 1j*1j
(-1+0j)
>>> 1j*complex(0,1)
(-1+0j)
>>>
字符串可以用单引号、双引号、三引号标识。其中三引号中,不需要行属转义。字符串不可使用索引对其赋值
如:
>>> hello='''hello,ereryone
hello,world'''
>>> print(hello)
hello,ereryone
hello,world
>>> hello='hello,everyone\nhello'
>>> print(hello)
hello,everyone
hello
>>> hello="hello,everyone\'hello\'\nhello"
>>> print(hello)
hello,everyone'hello'
hello
>>>
字符串拼接(+)及重复(*):
>>> 'str'+'ing' #字符串拼接
'string'
>>> 'str'*3 #字符串重复
'strstrstr'
>>>
字符串索引
>>> word='helpA' #字符串检索
>>> word[0]
'h'
>>> word[-5]
'h'
>>> word[:3]
'hel'
>>> word[4:]
'A'
>>> word[5:]
''
>>> word [1:3]
'el'
>>>
索引边界:对本例word字符串:
h e l p A
0 1 2 3 4 索引
-5 -4 -3 -2 -1 负索引
注意:
- python中单引号和双引号使用完全相同。
- 使用三引号('''或""")可以指定一个多行字符串。
- 转义符 '\'
- 自然字符串, 通过在字符串前加r或R。 如 r"this is a line with \n" 则\n会显示,并不是换行。
- python允许处理unicode字符串,加前缀u或U, 如 u"this is an unicode string"。
- 字符串是不可变的。
- 按字面意义级联字符串,如"this " "is " "string"会被自动转换为this is string。
在交互模式中,最后被输出的表达式结果被赋值给变量 _ 。这能使您在把Python作为一个桌面计算器使用时使后续计算更方便,例如:
>>> price=4.5
>>> price * 5
22.5
>>> price + _
27.0