Jupyter命令模式快捷键:
- dd:删除选中的单元格
- a:在选中的单元格上方加入一个空白单元格
- b:在选中的单元格下方加入一个空白单元格
- m:将选中的单元格转化为Markdown格式
- y:将选中的单元格转化为代码格式
Python输出
Python普通输出
hello world!
Python格式化输出
使用%进行格式化输出
1 2
| print('%d+%d=%d' % (a, b, a+b))
|
2+3=5
1
| print('{}+{}={}'.format(a, b, a+b))
|
2+3=5
使用f’’进行格式化输出
2+3=5
常用的字符串操作
定义字符串
1 2 3 4
| a = 'hello world!' b = "hello world!" c = """hello world!"""
|
1 2
| print(a == b) print(a == c)
|
True
True
1 2 3
| d = """hello world!"""
|
hello
world!
False
字符串的索引和切片
1 2
| str1 = 'Hi, world!' print(str1)
|
Hi, world!
索引(取出指定的字符)
1 2 3
| print(str1[2]) print(str1[-8])
|
,
,
切片(取出指定的一块字符)
1 2 3
| print(str1[4:9]) print(str1[4:-1])
|
world
world
1 2 3
| print(str1[0:2]) print(str1[:2])
|
Hi
Hi
1 2 3
| print(str1[4:10]) print(str1[4:])
|
world!
world!
1 2 3
| print(str1) print(str1[:])
|
Hi, world!
Hi, world!
H,wrd
i ol!
字符串的特殊函数
字符串的拼接
'hello world!Hi, world!'
'Hi, world!Hi, world!Hi, world!'
字符串的替换
1 2 3
| str1 = str1.replace('Hi', 'hello') print(str1)
|
hello, world!
字符串的切分
['hello', ' world!']
删除字符串前后的指定字符
'你好,!!世界'
删除字符串中所有的某个字符
'你好,世界'
常用的列表操作
定义一个列表
1 2
| list1 = [1, 2, 3, 4, True, 'ABC'] print(list1)
|
[1, 2, 3, 4, True, 'ABC']
列表的索引和切片
True
[2, 3, 4, True]
[1, 3, True]
列表中常用的函数和操作
列表的增删改查
增
1 2 3
|
list1.insert(2, 'insert')
|
[1, 2, 'insert', 3, 4, True, 'ABC']
1 2 3 4
|
list1.append('end') list1
|
[1, 2, 'insert', 3, 4, True, 'ABC', 'end']
1 2 3 4
|
list1.extend([201, 202, 203]) list1
|
[1, 2, 'insert', 3, 4, True, 'ABC', 'end', 201, 202, 203]
删
1 2 3 4
|
list1.remove('ABC') list1
|
[1, 2, 'insert', 3, 4, True, 'end', 201, 202, 203]
[1, 2, 3, 4, True, 'end', 201, 202, 203]
改
[1, 2, 3, 4, False, 'end', 201, 202, 203]
常用的字典操作
定义一个字典
1
| dict1 = {'name': 'Tony', 'job': 'Students'}
|
利用字典进行取值
'Tony'
字典的增删改查
增
{'name': 'Tony', 'job': 'Students', 'sex': '男'}
删
'男'
{'name': 'Tony', 'job': 'Students'}
改
1
| dict1['job'] = 'Teachers'
|
{'name': 'Tony', 'job': 'Teachers'}
其他函数
查看所有的键名称
dict_keys(['name', 'job'])
查看所有的值
dict_values(['Tony', 'Teachers'])
查看所有的键值对
dict_items([('name', 'Tony'), ('job', 'Teachers')])
Python中的循环
for循环
1 2 3 4
| for i in range(1,11): print(i ** 2) print('-'*50)
|
1
--------------------------------------------------
4
--------------------------------------------------
9
--------------------------------------------------
16
--------------------------------------------------
25
--------------------------------------------------
36
--------------------------------------------------
49
--------------------------------------------------
64
--------------------------------------------------
81
--------------------------------------------------
100
--------------------------------------------------
1 2
| for i in list1: print(i)
|
1
2
3
4
False
end
201
202
203
while循环
1 2 3 4
| n = 50 while n >= 25: print(n ** 2) n -= 1
|
2500
2401
2304
2209
2116
2025
1936
1849
1764
1681
1600
1521
1444
1369
1296
1225
1156
1089
1024
961
900
841
784
729
676
625
Python中的分支结构
1 2 3 4
| for i in list1: if type(i) == int: print(i)
|
1
2
3
4
201
202
203
1 2 3 4 5 6
| for i in list1: if type(i) == int: print(i) else: print(0)
|
1
2
3
4
0
0
201
202
203
1 2 3 4 5 6 7 8
| for i in list1: if type(i) == int: print(i) elif type(i) == bool: print(i) else: print(0)
|
1
2
3
4
False
0
201
202
203
列表推导式
1 2 3 4 5
| res = [] for i in range(1, 21): res.append((i + 1) ** 2) res
|
[4,
9,
16,
25,
36,
49,
64,
81,
100,
121,
144,
169,
196,
225,
256,
289,
324,
361,
400,
441]
1 2 3
| res = [(i + 1) ** 2 for i in range(1, 21)] res
|
[4,
9,
16,
25,
36,
49,
64,
81,
100,
121,
144,
169,
196,
225,
256,
289,
324,
361,
400,
441]
函数的定义
1 2 3 4 5 6 7
| def get_int(list_): res = [] for i in list_: if type(i) == int: res.append(i) return res
|
1 2 3
| def get_int2(list_): return [i for i in list_ if type(i) == int]
|
[1, 2, 3, 4, 201, 202, 203]
[1, 2, 3, 4, 201, 202, 203]
文件操作
文件的存储
1 2 3 4
| f = open('list.txt', 'w') f.write(str(list1)) f.close()
|
文件的读取
1 2 3 4
| f = open('list.txt', 'r') list_1 = f.read() f.close()
|
"[1, 2, 3, 4, False, 'end', 201, 202, 203]"
[1, 2, 3, 4, False, 'end', 201, 202, 203]
1 2 3
| with open('list.txt', 'r') as f: list_2 = f.read() print(list_2)
|
[1, 2, 3, 4, False, 'end', 201, 202, 203]