商/整数商//余数%乘方**绝对值abs(x)
x=eval(input())
y=x//9
print(y)
提取手机号3到7或
x=input()
y=x[3:7]
print(y)
截取手机号后n位
x=input()
n=(input())
y=x[-n:]
print(y)
ab替换成AB
s = input()
print(s.replace("ab", "AB"))
列表题
nums = [16,8,2,5,8,7,3,1,9,0]
n = int(input())
del nums[n-1]
print(nums)
计算E
m, v = map(float, input().split(','))
print("{0:.3f}".format(0.5 * m * v ** 2))
分段函数
import math
# 读取输入x并转为浮点数
x = float(input())
if x <= -1:
y = abs(x + 2)
elif -1 < x < 2:
y = 0.5 * x ** 2
else:
# x >= 2,math.sin参数为弧度
y = math.sin(3 * x)
# 保留2位小数格式化输出
print(f"y={y:.2f}")
数列计算第七题
n = int(input())
total = 0
for i in range(1, n+1):
total += i - 1/i
print(f"数列的和是{total:.2f}")
函数
# 定义指定名称函数func
def func(s):
letter_str = ""
digit_count = 0
for c in s:
if c.isalpha():
letter_str += c
elif c.isdigit():
digit_count += 1
# 返回字母子串、数字个数
return letter_str, digit_count
# 读取输入字符串
s_input = input()
# 调用函数
res_letter, res_num = func(s_input)
# 按格式输出
print(f"包含{res_num}个数字")
print("和字母")
print(res_letter)
第九题
# 题目已给出的读文件代码
f = open("phonenum.txt")
ls = f.readlines()
f.close()
# 1. 把ls里每行数据存入字典,姓名为键,电话为值
phone_dict = {}
for line in ls:
# 去除每行末尾换行符
line = line.strip()
# 按空格分割成姓名、电话
name, tel = line.split()
phone_dict[name] = tel
# 2. 获取用户输入姓名
search_name = input()
# 3. 查询并输出结果
if search_name in phone_dict:
print(f"{search_name}:{phone_dict[search_name]}")
else:
print("数据不存在")
字典
# 题目已给出的读文件代码
f = open("phonenum.txt")
ls = f.readlines()
f.close()
# 1. 把ls里每行数据存入字典,姓名为键,电话为值
phone_dict = {}
for line in ls:
# 去除每行末尾换行符
line = line.strip()
# 按空格分割成姓名、电话
name, tel = line.split()
phone_dict[name] = tel
# 2. 获取用户输入姓名
search_name = input()
# 3. 查询并输出结果
if search_name in phone_dict:
print(f"{search_name}:{phone_dict[search_name]}")
else:
print("数据不存在")