python基础知识需注意的知识点
函数
- round()函数是圆整到最接近的整数,当两个整数一样接近时候圆整到偶数。
>>> round(1.5)
2
>>> round(2.5)
2
>>> round(1.3)
1
>>> round(-1.3)
-1
>>> round(-1.3)
-1
模块
1.cmath和复数
如果要进行复数计算,需要引用cmath库
>>> import cmath
>>> cmath.sqrt(-1)
1j
>>> import math
>>> math.sqrt(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
需要注意的是:math库中和cmath库中都有sqrt的同名函数,所以在进行库的引用的时候,尽量使用 import module 的形式,避免使用from module import function
2.str和repr的区别
函数str( )将其转化成为适于人阅读的前端样式文本,而repr(object)就是原本未处理的用于编译器阅读的后台底层代码
>>> a
'hello, world!\n' #输出的a本来的模样
>>> print(a) #对a经过编译后的样式文本输出,将转义字符进行转义
hello,world!
>>> print(str(a)) #可以看到对str返回的值进行print处理,这将与直接print(a)得到相同的结果
hello,world!
>>> repr(a) #获得的是a原始输入的存在于存储器中用于机器底层编译的代码
"'hello, world!\n'"
>>> print(repr(a)) #对于repr返回的值进行print处理,可以看到这与直接在终端输入a,得到的是相同的结果 'hello,world!\n'
'hello, world!\n'
3.长字符串和原始字符串
- 长字符串
表示和长的字符串可以用三个单引号或三个双引号
>>> print("""What's your name?
He said "My name is cheng."
haha,hello,cheng.""")
What's your name?
He said "My name is cheng."
haha,hello,cheng.
使用长字符串的好处,可以跨多行,可以随便使用单引号和双引号,无须使用反斜杠进行转义。
- 语句过长需要换行的可以在末尾加上\
>>> print('hello,\
world')
hello,world
>>> 1 + 3 + 5 + 6 + 7\
+8
30
- 原始字符串(不以特殊方式处理反斜杠\)
在字符串前面加上r,该字符串中反斜杠不表示转义
但字符串结尾不能以\进行结尾,否则会报错
>>> print('c:\name')
c:
ame
>>> print(r'c:\name')
c:\name
>>> print(r"c:\name")
c:\name
>>> print(r'c:\name\')
SyntaxError: EOL while scanning string literal
遇到字符串结尾以\进行结尾的可以这么处理
>>> print(r'c:\name''\\')
c:\name\
>>> print(r'c:\name'+'\\')
c:\name\
3.编码和解码
- 使用encode 和decode
python中默认编码格式是UTF-8
>>> text = '武汉加油!'
>>> text.encode()
b'\xe6\xad\xa6\xe6\xb1\x89\xe5\x8a\xa0\xe6\xb2\xb9\xef\xbc\x81'
>>> b_text = text.encode()
>>> b_text.decode()
'武汉加油!'
也可以使用UTF-32进行编码
>>> b_text = text.encode('UTF-32')
>>> b_text.decode('UTF-32')
'武汉加油!'
- 直接创建bytes和str对象(这种方法更通用一些)
>>> text = '武汉加油!'
>>> b_text = bytes(text, encoding='UTF-8')
>>> b_text
b'\xe6\xad\xa6\xe6\xb1\x89\xe5\x8a\xa0\xe6\xb2\xb9\xef\xbc\x81'
>>> str(b_text, encoding='UTF-8')
'武汉加油!'
- bytearray(bytes的可变版本)
可以替换数据流中的字符,要插入字符必须使用ord获取其序数值
>>> str(b_text, encoding='UTF-8')
'武汉加油!'
>>> x = bytearray(b_text)
>>> x
bytearray(b'\xe6\xad\xa6\xe6\xb1\x89\xe5\x8a\xa0\xe6\xb2\xb9\xef\xbc\x81')
>>> x[0] = ord(b'c')
>>> x
bytearray(b'c\xad\xa6\xe6\xb1\x89\xe5\x8a\xa0\xe6\xb2\xb9\xef\xbc\x81') # 将第一个编码变为了c
列表
1.给切片赋值
在不替换原有元素的情况下插入新元素
>>> ls = [1, 2, 3, 4, 5, 6]
>>>> ls[1:1] = 'c'
>>> ls
[1, 'c', 2, 3, 4, 5, 6]
使用切片赋值删除切片
>>> numbers = [1, 2, 3, 4, 5]
>>> numbers[1:4] = []
>>> numbers
[1, 5]
2.列表方法
- append
将一个对象附加到列表末尾,直接修改旧列表
>>> lst = [1, 2, 3]
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]
>>> lst = [1, 2, 3]
>>> lst.append([1, 2])
>>> lst
[1, 2, 3, [1, 2]]```
- clear
**就地**清空列表的内容
```python
>>> lst
[1, 2, 3, 4]
>>> lst.clear()
>>> lst
[]
- copy
复制列表,建立副本
普通复制只是将另外一个名称关联到表
>>> lst = [1, 2]
>>> ls = lst
>>> ls[1] = 3
>>> lst
[1, 3]
>>> lst = [1, 2]
>>> ls = lst.copy()
>>> ls[1] = 3
>>> lst
[1, 2]
- count
计算指定的元素在列表中出现了多少次
>>> ls = [1, 2, 3, 5, [1, 2, 3]]
>>> ls.count(1)
1
>>> ls.count([1, 2, 3])
1
>>>
- extend
同时将多个值附加到列表末尾
注意append和extend的区别
>>> lst = [1, 2, 3]
>>> lst.append([1, 2])
>>> lst
[1, 2, 3, [1, 2]]
>>> lst = [1, 2, 3]
>>> lst.extend([1, 2])
>>> lst
[1, 2, 3, 1, 2]
- index
在列表中查找指定值第一次出现的索引
搜索不到会出现异常
>>> a
['h', 'e', 'l', 'l', 'o']
>>> a.index('l')
2
>>> a.index(1)
Traceback (most recent call last):
File "<pyshell#48>", line 1, in <module>
a.index(1)
ValueError: 1 is not in list
>>>
- insert
将一个对象插入列表
>>> a
['h', 'e', 'l', 'l', 'o']
>>> a.insert(2, 'a')
>>> a
['h', 'e', 'a', 'l', 'l', 'o']
>>> a.insert(2, [1, 2])
>>> a
['h', 'e', [1, 2], 'a', 'l', 'l', 'o']
- pop 从列表中删除一个元素,并返回这个元素
默认删除最后一个元素
pop可以实现一种常见的数据结构–栈
>>> a
['h', 'e', [1, 2], 'a', 'l', 'l', 'o']
>>> a.pop()
'o'
>>> a
['h', 'e', [1, 2], 'a', 'l', 'l']
>>> a.pop(2)
[1, 2]
>>> a
['h', 'e', 'a', 'l', 'l']
- remove删除第一个为指定值的元素
找不到要删除的值会报错
>>> a
['h', 'e', 'a', 'l', 'l']
>>> a.remove('a')
>>> a
['h', 'e', 'l', 'l']
>>> a.remove('b')
Traceback (most recent call last):
File "<pyshell#59>", line 1, in <module>
a.remove('b')
ValueError: list.remove(x): x not in list
- reverse按相反顺序排列的列表元素
修改列表但不返回任何值
reversed函数返回一个迭代器
>>> a
['h', 'e', 'l', 'l']
>>> a.reverse()
>>> a
['l', 'l', 'e', 'h']
- sort对列表就地排序
sort排序会对原列表进行修改
>>> a
['l', 'l', 'e', 'h']
>>> a.sort()
>>> a
['e', 'h', 'l', 'l']
不改变原来的序列排序
>>> b = [5, 1, 4, 3, 2]
>>> c = b.copy()
>>> c.sort()
>>> c
[1, 2, 3, 4, 5]
>>> b
[5, 1, 4, 3, 2]
获取排序后的列表副本,使用sorted()函数
>>> b
[5, 1, 4, 3, 2]
>>> d = sorted(b)
>>> d
[1, 2, 3, 4, 5]
- sort高级排序
sort方法有两个参数(key,reverse)
reverse=True是倒序
>>> ls
['p', 'y', 't', 'h', 'o', 'n']
>>> ls.sort(reverse=True)
>>> ls
['y', 't', 'p', 'o', 'n', 'h']
key=len表示用长度排序
>>> text = ['abcd', 'aa', 'abc']
>>> text.sort(key=len)
>>> text
['aa', 'abc', 'abcd']
3.元组:不可修改的序列
元组的创建:值用逗号分隔
>>> 3 * (4 + 3)
21
>>> 3 * (4 + 3),
(21,)
>>> 3 * (4 + 3,)
(7, 7, 7)
>>> 1,
(1,)
>>> 1, 2, 3
(1, 2, 3)