复数操作:
>>>complex(2,2)
(2+2j)
>>> 1+2j
(1+2j)
>>> _*2
(2+4j)
>>> a=_
>>> a.real
2.0
>>> a.imag
4.0
>>> abs(a)
4.47213595499958
>>>
字符操作: ASCII编码是1个字节,而Unicode编码通常是2个字节。但是英文用两个字节编码浪费空间,于是又出现了把Unicode编码转化为“可变长编码”的UTF-8编码,UTF-8编码把一个Unicode字符根据不同的数字大小编码成1-6个字节,常用的英文字母被编码成1个字节,汉字通常是3个字节,只有很生僻的字符才会被编码成4-6个字节。如果你要传输的文本包含大量英文字符,用UTF-8编码就能节省空间:
>>> 'feng douzhong'
'feng douzhong'
>>> 'doesn\'t'
"doesn't"
>>> "doesn't"
"doesn't"
>>> print '"Isn\'t,"she said.'
File "<stdin>", line 1
print '"Isn\'t,"she said.'
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('"Isn\'t,"she said.')?
>>> print ('"Isn\'t,"she said.')
"Isn't,"she said.
>>> s='First line.\nSecond line.'
>>> print s
File "<stdin>", line 1
print s
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(s)?
>>> print(s)
First line.
Second line.
>>> print (r'C:\some\name)
File "<stdin>", line 1
print (r'C:\some\name)
^
SyntaxError: EOL while scanning string literal
>>> print r'(C:\some\name)
File "<stdin>", line 1
print r'(C:\some\name)
^
SyntaxError: EOL while scanning string literal
>>> print (r'C:\some\name')
C:\some\name
>>> print('''\''')
...
...
...
... print('''\''')
File "<stdin>", line 5
print('''\''')
^
SyntaxError: unexpected character after line continuation character
>>> print '''\
... Usage: thingy [OPtions]
... -h Disply
... -H hostname Hostname to connect to
... '''
File "<stdin>", line 5
print '''\
Usage: thingy [OPtions]
-h Disply
-H hostname Hostname to connect to
'''
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('''\
Usage: thingy [OPtions]
-h Disply
-H hostname Hostname to connect to
''')?
>>> print '''\
... Usage: thingy [OPtions]
... -h Disply
... -H hostname Hostname to connect to
... ''')
File "<stdin>", line 5
print '''\
Usage: thingy [OPtions]
-h Disply
-H hostname Hostname to connect to
''')
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('''\
Usage: thingy [OPtions]
-h Disply
-H hostname Hostname to connect to
'''))?
>>> print ('''\
... first, read
... second, lestean
... third, write
... ''')
first, read
second, lestean
third, write
>>> a=3*'us'+'imu'
>>> print(a)
usususimu
>>> b='Py''thon'
>>> print(b)
Python
>>> c='Py'
>>> c+'thon'
'Python'
>>> text=('put the apple '
... 'into the table.')
>>> text
'put the apple into the table.'
>>> word = 'Python'
>>> word[0]
'P'
>>> word[3]
'h'
>>> word[-3]
'h'
>>> word[1:5]
'ytho'
>>> word[:2]+word[2:]
'Python'
>>> world[2:]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'world' is not defined
>>> word[2:]
'thon'
>>> word[2:55]
'thon'
>>> word[9:]
''
>>> word[0]=1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> 'J'+word[1:]
'Jython'
>>> word[:2]+th
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'th' is not defined
>>> word[:2]+'th'
'Pyth'
>>> len(word)
6
纯英文的str可以用ASCII编码为bytes,内容是一样的,含有中文的str可以用UTF-8编码为bytes。含有中文的str无法用ASCII编码,因为中文编码的范围超过了ASCII编码的范围,Python会报错。
>>> '中午'.encode('utf-8')
b'\xe4\xb8\xad\xe5\x8d\x88'
>>> '中午'.encode('ascii')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
>>>
反过来,如果我们从网络或磁盘上读取了字节流,那么读到的数据就是bytes。要把bytes变为str,就需要用decode()方法,如果bytes中包含无法解码的字节,decode()方法会报错,
>>> b'\xe4\xb8\xad\xe5\x8d\x88'.decode('ascii')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
>>> b'\xe4\xb8\xad\xe5\x8d\x88'.decode('utf-8')
'中午'
>>>
len()函数计算的是str的字符数,如果换成bytes,len()函数就计算字节数:
>>> len(b'ABC')
3
>>> len(b'\xe4\xb8\xad\xe6\x96\x87')
6
>>> len('中文'.encode('utf-8'))
6
>>>
格式化字符串
>>> 'Hello,%s'%'world'
'Hello,world'
>>> 'Hi,%s,you have $%d.'%('Michael',10000)
'Hi,Michael,you have $10000.'
>>> 'Age:%s.Gender:%s'%(25,True)
'Age:25.Gender:True'
>>> 'growth rate:%d %%'%7
'growth rate:7 %'
字符串的format()方法格式化字符串
>>> 'Hello,{0},成绩提高了 {1:.1f}%'.format('小明',17.25)
'Hello,小明,成绩提高了 17.2%'
>>>
前面带有u说明创建unicode字符
>>> u'中午'
'中午'
列表操作
>>> list = [1,2,3,4,5,6,7,8,9]
>>> list
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list[1]
2
>>> list[-3]
7
>>> list[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list+[3,4,5,6]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 4, 5, 6]
>>> list[0]=90
>>> list
[90, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list.append(888)
>>> list
[90, 2, 3, 4, 5, 6, 7, 8, 9, 888]
>>> list[2:6]=[]
>>> list
[90, 2, 7, 8, 9, 888]
>>> list[:]=[]
>>> ;ist
File "<stdin>", line 1
;ist
^
SyntaxError: invalid syntax
>>> list
[]
>>> len(list)
0
>>> a=[1,2,3,4]
>>> b=[7,8,9,]
>>> x=[a,b]
>>> x
[[1, 2, 3, 4], [7, 8, 9]]
>>> x[0]
[1, 2, 3, 4]
>>> x[1][1]
8
第一个程序:python 最具特色的就是用缩进来写模块。缩进的空白数量是可变的,但是所有代码块语句必须包含相同的缩进空白数量,这个必须严格执行。以下实例缩进为四个空格:
>>>a,b =0,1
>>> while b<1000:
... print(b),
... a,b=b,a+b
...
1
(None,)
1
(None,)
2
(None,)
3
(None,)
5
(None,)
8
(None,)
13
(None,)
21
(None,)
34
(None,)
55
(None,)
89
(None,)
144
(None,)
233
(None,)
377
(None,)
610
(None,)
987
(None,)
函数之间或类的方法之间用空行分隔,表示一段新的代码的开始。类和函数入口之间也用一行空行分隔,以突出函数入口的开始。 空行与代码缩进不同,空行并不是Python语法的一部分。书写时不插入空行,Python解释器运行也不会出错。但是空行的作用在于分隔两段不同功能或含义的代码,便于日后代码的维护或重构。
|