目录
1. print
print的内容输出到文件
>>> fp = open('./text.txt', 'a+')
>>> print('hello world', file = fp)
>>> fp.close()
符号 | 含义 |
---|---|
\n | 换行 |
\r | 回车 |
\b | 退格 |
\t | tab |
>>> print('hello\nworld')
hello
world
>>> print('helloooo\rworld')
worldooo
>>> print('hello\bworld')
hellworld
>>> print('helloo\bworld')
helloworld
>>> print('hell\tworld')
hell world
>>> print('hello\tworld')
hello world
>>> print('helloo\tworld')
helloo world
>>> print('hellooo\tworld')
hellooo world
>>> print('helloooo\tworld')
helloooo world
r 表示不转义
>>> print(r'http:\\\\www.baidu.com')
http:\\\\www.baidu.com
>>> print(r'http:\\\\www.baidu.com\')
File "<stdin>", line 1
print(r'http:\\\\www.baidu.com\')
^
SyntaxError: EOL while scanning string literal
>>> print(r'http:\\\\www.baidu.com\\')
http:\\\\www.baidu.com\\
2. keyword
>>> import keyword
>>> print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
3. id
>>> name = '楚留香'
>>> print(name, id(name))
楚留香 4549840496
>>> print(name, type(name))
楚留香 <class 'str'>
>>> print(name)
楚留香
4. decimal
更精确
>>> n1 = 1.1
>>> print(n1)
1.1
>>> n2 = 2.2
>>> print(n2)
2.2
>>> print(n1 + n2)
3.3000000000000003
>>> import decimal
>>> decimal.getcontext().prec = 28
>>> print(decimal.getcontext().prec)
28
>>> m1 = decimal.Decimal('1.1')
>>> m2 = decimal.Decimal('2.2')
>>> print(m1 + m2)
3.3
5. str
>>> age = 20
>>> print('你的年龄是:' + str(age))
你的年龄是:20
6. input
返回值类型一定是str
>>> m1 = input('plese input the first word:')
plese input the first word:12
>>> m2 = input('plese input the second word:')
plese input the second word:13
>>> print(m1 + m2)
1213
>>> print(int(m1) + int(m2))
25
7. 交换变量
>>> x = 10
>>> y = 20
>>> print(x, y)
10 20
>>> x, y = y, x
>>> print(x, y)
20 10
8. is
两个变量的id是否相同
>>> a = 10
>>> b = 10
>>> c = 10.0
>>> print(a is b)
True
>>> print(a is c)
False
>>> print(a is not b)
False
>>> print(a is not c)
True
9. range
返回迭代器对象,无法直接输出。必须列表化之后才能输出。
>>> a = range(20)
>>> print(a)
range(0, 20)
>>> print(list(a))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
9.1 两个参数
>>> a = range(10, 20)
>>> print(list(a))
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
9.2 三个参数
>>> a = range(10, 20, 3)
>>> print(list(a))
[10, 13, 16, 19]
10. in not in
>>> print(10 in list(a))
True
>>> print(15 in list(a))
False
>>> print(10 in a)
True
>>> print(15 in a)
False
11. list
>>> list1 = ['hello', 'world', 95, 98.5]
>>> print(list1)
['hello', 'world', 95, 98.5]
>>> list2 = list(['hello', 'world', 95, 98.5])
>>> print(list2)
['hello', 'world', 95, 98.5]
>>> list3 = list(range(100))
>>> print(list3)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
11.1 step
>>> list4 = [0, 1, 2, 3, 4, 5, 6, 7]
>>> list5 = list4[2:6:1]
>>> list5
[2, 3, 4, 5]
>>> list5 = list4[2:6:2]
>>> list5
[2, 4]
11.1.1 step < 0
>>> list5 = list4[6:2:-1]
>>> list5
[6, 5, 4, 3]
11.1.2 逆序输出
>>> print(list4[::-1])
[7, 6, 5, 4, 3, 2, 1, 0]
11.2 添加
添加操作不改变id值,没有新列表产生
11.2.1 append
>>> list4
[0, 1, 2, 3, 4, 5, 6, 7]
>>> list4.append(8)
>>> list4
[0, 1, 2, 3, 4, 5, 6, 7, 8]
注意!这里将list作为一个元素
>>> list5 = [9, 10, 11, 12]
>>> list4.append(list5)
>>> list4
[0, 1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11, 12]]
11.2.2 extend
>>> list1 = [1, 2, 3, 4]
>>> list2 = [5, 6, 7, 8]
>>> list1.extend(list2)
>>> list1
[1, 2, 3, 4, 5, 6, 7, 8]
11.2.3 insert
>>> list1
[1, 2, 3, 4, 5, 6, 7, 8]
>>> list1.insert(0, 0)
>>> list1
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> list2 = [9, 10]
>>> list1.insert(0, list2)
>>> list1
[[9, 10], 0, 1, 2, 3, 4, 5, 6, 7, 8]
11.2.4 片选
一个片段用新的片段替代,元素个数不要求相等
>>> list1 = [0, 1, 2, 3, 4, 5]
>>> list2 = ['hello', 'world']
>>> list1[1:3:] = list2
>>> list1
[0, 'hello', 'world', 3, 4, 5]
>>> list1 = [0, 1, 2, 3, 4, 5]
>>> list2 = ['hello', 'world']
>>> list1[1:2:] = list2
>>> list1
[0, 'hello', 'world', 2, 3, 4, 5]
11.3 删除
11.3.1 正常移除
>>> list1 = [0, 1, 2, 3, 4, 5]
>>> list1.remove(5)
>>> list1
[0, 1, 2, 3, 4]
11.3.2 不存在
>>> list1.remove(6)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
11.3.3 重复
移除第一个元素
>>> list1 = [1, 1, 1]
>>> list1.remove(1)
>>> list1
[1, 1]
11.4 pop
11.4.1 正常pop
>>> list1 = [0, 1, 2, 3, 4, 5]
>>> list1.pop(2)
2
>>> list1
[0, 1, 3, 4, 5]
11.4.2 索引超限
>>> list1 = [0, 1, 2, 3, 4, 5]
>>> list1.pop(6)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop index out of range
11.4.3 切片
>>> list1 = [0, 1, 2, 3, 4, 5]
>>> list1[2:4] = []
>>> list1
[0, 1, 4, 5]
11.4.4 clear
空列表
11.4.5 del
删除了这个列表变量
>>> list1 = [0, 1, 2, 3, 4, 5]
>>> list1
[0, 1, 2, 3, 4, 5]
>>> del list1
>>> list1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'list1' is not defined
11.5 排序
11.5.1 列表自带sort()
不会产生新列表
>>> list1 = [0, 2, 1, 4, 6, 5, 3]
>>> list1.sort()
>>> list1
[0, 1, 2, 3, 4, 5, 6]
>>> list1.sort(reverse = True)
>>> list1
[6, 5, 4, 3, 2, 1, 0]
11.5.2 内置函数sorted()
产生新列表
>>> list1 = [0, 2, 1, 4, 6, 5, 3]
>>> sorted(list1)
[0, 1, 2, 3, 4, 5, 6]
>>> list1
[0, 2, 1, 4, 6, 5, 3]
>>> sorted(list1, reverse = True)
[6, 5, 4, 3, 2, 1, 0]
>>> list1
[0, 2, 1, 4, 6, 5, 3]
11.6 列表生成
>>> list1 = [2 * i for i in range(1, 6)]
>>> list1
[2, 4, 6, 8, 10]
>>> list1 = list(range(1, 10))
>>> list1
[1, 2, 3, 4, 5, 6, 7, 8, 9]
12. dict
12.1 创建
12.1.1 {}
>>> score = {'tom':85, 'jack':92, 'sam':'pass'}
>>> score
{'tom': 85, 'jack': 92, 'sam': 'pass'}
12.1.2 dict()
注意!这里是 =,而且没有引号
>>> student = dict(tom = 85, jack = 92, sam = 'pass')
>>> student
{'tom': 85, 'jack': 92, 'sam': 'pass'}
12.1.3 空
>>> empty = {}
>>> empty
{}
>>> empty = dict()
>>> empty
{}
12.2 字典元素获取
12.2.1 []
>>> print(score['tom'])
85
12.2.2 get()
>>> print(score.get('tom'))
85
12.3 字典元素增删改
12.3.1 元素修改 []
>>> score
{'tom': 85, 'jack': 92, 'sam': 'pass'}
>>> score['tom'] = 89
>>> score
{'tom': 89, 'jack': 92, 'sam': 'pass'}
12.3.2 元素删除 del
>>> del score['tom']
>>> score
{'jack': 92, 'sam': 'pass'}
12.3.3 元素增加
>>> score
{'jack': 92, 'sam': 'pass'}
>>> score['jerry'] = 99
>>> score
{'jack': 92, 'sam': 'pass', 'jerry': 99}
12.3.4 元素清空 clear()
12.4 字典视图
12.4.1 获取键
>>> score
{'jack': 92, 'sam': 'pass', 'jerry': 99}
>>> score.keys()
dict_keys(['jack', 'sam', 'jerry'])
列表化
>>> list(score.keys())
['jack', 'sam', 'jerry']
12.4.2 获取值
>>> score
{'jack': 92, 'sam': 'pass', 'jerry': 99}
>>> score.values()
dict_values([92, 'pass', 99])
列表化
>>> list(score.values())
[92, 'pass', 99]
12.4.3 获取键值对
>>> score
{'jack': 92, 'sam': 'pass', 'jerry': 99}
>>> score.items()
dict_items([('jack', 92), ('sam', 'pass'), ('jerry', 99)])
列表化
>>> list(score.items())
[('jack', 92), ('sam', 'pass'), ('jerry', 99)]
12.5 字典遍历
>>> for i in score:
... print(i, score[i], score.get(i))
...
jack 92 92
sam pass pass
jerry 99 99
12.6 字典生成 zip
>>> items = ['books', 'fruits', 'papers']
>>> prices = [98, 56, 233]
>>> a = {i:j for i,j in zip(items, prices)}
>>> a
{'books': 98, 'fruits': 56, 'papers': 233}
键值个数不一致的时候,以短的为准
>>> items = ['books', 'fruits', 'papers']
>>> prices = [98, 56, 233, 244]
>>> a = {i:j for i,j in zip(items, prices)}
>>> a
{'books': 98, 'fruits': 56, 'papers': 233}