Table of contents
1. Format (5 points, basic operation questions) Edit
2. File read and write operations
4. Count the number of repeated text
The notes are organized from the personal space of Dr. Big Head, the UP master of Station B-Mr. Big Head's personal homepage-哔哩哔哩 Video Tutorial
1. Format (5 points, basic operation questions)
2. File read and write operations
File operation is related to opening
If the following error occurs, you can add encoding='utf-8' at the end. Normally, it is not necessary to add
Convert data to list type as much as possible
read operation
Read operations generally use read() or readlines()
1. Read method: the entire text is read in as a string
Improve the above code, separated by newlines, you will get a list
There is a newline at the end
The strip method removes the preceding spaces and newlines
Common code for reading files:
2、readlines()
write operation
write、writelines
3. Turtle library
4. Count the number of repeated text
5. List sorting
6. Jieba library
7. Text processing
8. Random library
Basic operation questions
# 请在______处使用一行代码或表达式替换
#
# 注意:请不要修改其他已给出代码
import jieba
txt = input("请输入一段中文文本:")
ls = jieba.lcut(txt)
print("{:.1f}".format(len(txt)/len(ls)))
# 请在______处使用一行代码或表达式替换
#
# 注意:请不要修改其他已给出代码
n = eval(input("请输入一个数字:"))
print("{:+^11}".format(chr(n - 1) + chr(n) + chr(n + 1)))
# 请在______处使用一行代码或表达式替换
#
# 注意:请不要修改其他已给出代码
n = eval(input("请输入正整数:"))
print("{:->20,}".format(n))
# 请在______处使用一行代码或表达式替换
#
# 注意:请不要修改其他已给出代码
import jieba
txt = input("请输入一段中文文本:")
ls = jieba.lcut(txt)
for i in ls[::-1]:
print(i, end = '')
# 请在...处使用一行或多行代码替换
# 请在______处使用一行代码替换
#
# 注意:请不要修改其他已给出代码
import random
brandlist = ['华为','苹果','诺基亚','OPPO','小米']
random.seed(0)
name = random.sample(brandlist, 1)
print(name)
# 请在______处使用一行代码或表达式替换
#
# 注意:请不要修改其他已给出代码
import jieba
s = input("请输入一个字符串")
n = len(s)
m = len(jieba.lcut(s))
print("中文字符数为{},中文词语数为{}。".format(n, m))
# 请在______处使用一行代码或表达式替换
#
# 注意:请不要修改其他已给出代码
ntxt = input("请输入4个数字(空格分隔):")
nls = ntxt.split()
x0 = eval(nls[0])
y0 = eval(nls[1])
x1 = eval(nls[2])
y1 = eval(nls[3])
r = pow(pow(x1-x0, 2) + pow(y1-y0, 2), 0.5)
print("{:.2f}".format(r))
# 请在______处使用一行代码或表达式替换
#
# 注意:请不要修改其他已给出代码
s = input("请输入一个字符串:")
print("{:=^20}".format(s))
# 请在...处使用一行或多行代码替换
#
# 注意:请不要修改其他已给出代码
n = eval(input("请输入数量:"))
if n == 1:
cost = 160 * n
elif n <= 4:
cost = 160 * n * 0.9
elif n <= 9:
cost = 160 * n * 0.8
else:
cost = 160 * n * 0.7
print("总额为:",cost)
# 请在______处使用一行代码或表达式替换
#
# 注意:请不要修改其他已给出代码
a, b = 0, 1
while a <= 100:
print(a, end=',')
a, b = b, a + b