0%

python编程基础复习

Jupyter命令模式快捷键:

  • dd:删除选中的单元格
  • a:在选中的单元格上方加入一个空白单元格
  • b:在选中的单元格下方加入一个空白单元格
  • m:将选中的单元格转化为Markdown格式
  • y:将选中的单元格转化为代码格式

Python输出

Python普通输出

1
print('hello world!')
hello world!

Python格式化输出

1
2
a = 2
b = 3

使用%进行格式化输出

  • 整数:%d
  • 浮点数:%f
  • 字符串:%s
1
2
# 输出a+b的和是??
print('%d+%d=%d' % (a, b, a+b))
2+3=5

使用format方法进行格式化输出

1
print('{}+{}={}'.format(a, b, a+b))
2+3=5

使用f’’进行格式化输出

1
print(f'{a}+{b}={a+b}')
2+3=5

常用的字符串操作

定义字符串

1
2
3
4
# 使用单双引号进行定义
a = 'hello world!'
b = "hello world!"
c = """hello world!"""
1
2
print(a == b)  
print(a == c) # 说明在Python中用单引号、双引号和单行的三引号定义的字符串没有任何区别
True
True
1
2
3
# 使用三引号定义具有换行符的字符串
d = """hello
world!"""
1
print(d)
hello 
world!
1
print(a == d)  # 具有换行符的字符串和原始的字符串就不相等了
False

字符串的索引和切片

1
2
str1 = 'Hi, world!'
print(str1)
Hi, world!

索引(取出指定的字符)

1
2
3
# 取出,
print(str1[2]) # 正向索引
print(str1[-8])
,
,

切片(取出指定的一块字符)

1
2
3
# 取出world
print(str1[4:9])
print(str1[4:-1]) # 正向索引和反向索引可以混合使用
world
world
1
2
3
# 取出hi
print(str1[0:2])
print(str1[:2]) # 当切片时,是从第一个元素开始取值的时候,初始下标可以省略
Hi
Hi
1
2
3
# 取出world!
print(str1[4:10])
print(str1[4:]) # 当切片时,如果一直需要取到最后一个元素时,结束下标可以省略
world!
world!
1
2
3
# 取出整个字符串
print(str1)
print(str1[:]) # 省略开始和结束下标
Hi, world!
Hi, world!
1
2
# 取出奇数项字符
print(str1[::2])
H,wrd
1
2
# 取出偶数项字符
print(str1[1::2])
i ol!

字符串的特殊函数

字符串的拼接

1
2
# 将两个字符串进行合并
a + str1
'hello world!Hi, world!'
1
2
# 将某个字符串重复n次
str1 * 3
'Hi, world!Hi, world!Hi, world!'

字符串的替换

1
2
3
# 将str1中的Hi换乘hello
str1 = str1.replace('Hi', 'hello')
print(str1)
hello, world!

字符串的切分

1
2
# 按照,切分字符串
str1.split(',')
['hello', ' world!']

删除字符串前后的指定字符

1
str2 = '!!你好,!!世界!!!'
1
str2.strip('!')
'你好,!!世界'

删除字符串中所有的某个字符

1
str2.replace('!', '')
'你好,世界'

常用的列表操作

定义一个列表

1
2
list1 = [1, 2, 3, 4, True, 'ABC']
print(list1)
[1, 2, 3, 4, True, 'ABC']

列表的索引和切片

1
2
# 取出True
list1[-2]
True
1
2
# 取出2,3,4,True
list1[1:-1]
[2, 3, 4, True]
1
2
# 取出列表的奇数项
list1[::2]
[1, 3, True]

列表中常用的函数和操作

列表的增删改查

1
2
3
# 在列表的指定位置增加某一个元素值
# 在2和3之间增加一个'insert'字符串
list1.insert(2, 'insert')
1
list1
[1, 2, 'insert', 3, 4, True, 'ABC']
1
2
3
4
# 在列表末尾追加单个元素
# 在列表末尾加上一个'end'
list1.append('end')
list1
[1, 2, 'insert', 3, 4, True, 'ABC', 'end']
1
2
3
4
# 在列表末尾追加多个元素
# 在列表末尾追加 201, 202, 203
list1.extend([201, 202, 203])
list1
[1, 2, 'insert', 3, 4, True, 'ABC', 'end', 201, 202, 203]

1
2
3
4
# 按照值进行删除
# 删除'ABC'
list1.remove('ABC')
list1
[1, 2, 'insert', 3, 4, True, 'end', 201, 202, 203]
1
2
3
4
# 按照下标位置进行删除
# 删除下标为2所在的元素
list1.pop(2)
list1
[1, 2, 3, 4, True, 'end', 201, 202, 203]

1
2
# 把True修改为False
list1[4] = False
1
list1
[1, 2, 3, 4, False, 'end', 201, 202, 203]

常用的字典操作

定义一个字典

1
dict1 = {'name': 'Tony', 'job': 'Students'}

利用字典进行取值

1
dict1['name']
'Tony'

字典的增删改查

1
dict1['sex'] = '男'
1
dict1
{'name': 'Tony', 'job': 'Students', 'sex': '男'}

1
dict1.pop('sex')
'男'
1
dict1
{'name': 'Tony', 'job': 'Students'}

1
dict1['job'] = 'Teachers'
1
dict1
{'name': 'Tony', 'job': 'Teachers'}

其他函数

查看所有的键名称

1
dict1.keys()
dict_keys(['name', 'job'])

查看所有的值

1
dict1.values()
dict_values(['Tony', 'Teachers'])

查看所有的键值对

1
dict1.items()
dict_items([('name', 'Tony'), ('job', 'Teachers')])

Python中的循环

for循环

1
2
3
4
# 输出1-10这10个数的平方
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
# 例子:已知现在有一个列表,列表中的元素为1-20.现在要获得一个  所有元素+1  然后取平方构成  的列表
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
get_int(list1)
[1, 2, 3, 4, 201, 202, 203]
1
get_int2(list1)
[1, 2, 3, 4, 201, 202, 203]

文件操作

文件的存储

1
2
3
4
# 将list1中的元素写入到本地文件
f = open('list.txt', 'w')
f.write(str(list1))
f.close()

文件的读取

1
2
3
4
# 读取本地的list.txt文件
f = open('list.txt', 'r')
list_1 = f.read()
f.close()
1
list_1
"[1, 2, 3, 4, False, 'end', 201, 202, 203]"
1
eval(list_1)
[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]