开平方:math.sqrt()
模块作用 math 模块里有很多数学工具,math.sqrt() 用来求一个数的平方根。 import math n = int(input()) print(math.sqrt(n)) 小练习 输入内容是:49,程序会输出什么? 提交
向下取整:math.floor()
模块作用 math.floor() 会把小数变成不大于它的整数。 import math x = float(input()) print(math.floor(x)) 小练习 输入内容是:8.9,程序会输出什么? 提交要求: 只提交最
向上取整:math.ceil()
模块作用 math.ceil() 会把小数变成不小于它的整数。 import math x = float(input()) print(math.ceil(x)) 小练习 输入内容是:2.1,程序会输出什么? 提交要求: 只提交最终输
求最大公约数:math.gcd()
模块作用 math.gcd() 用来找两个整数共同拥有的最大公约数。 import math a, b = map(int, input().split()) print(math.gcd(a, b)) 小练习 输入内容是:12 18,
随机整数:random.randint()
模块作用 random.randint(a, b) 会在 a 到 b 之间随机选一个整数。配合固定种子时,结果可以重复出现。 import random seed_num = int(input()) random.seed(seed_n
随机挑一个:random.choice()
模块作用 random.choice() 会从一组内容里随机选出一个。 import random seed_num = int(input()) colors = ["red", "blue",
统计单词数量:collections.Counter
模块作用 collections.Counter 用来快速统计每个内容出现了多少次。 from collections import Counter words = input().split() c = Counter(words) p
取平均数:statistics.mean()
模块作用 statistics.mean() 用来求一组数字的平均数。 import statistics nums = list(map(int, input().split())) print(statistics.mean(nums
路径最后名字:os.path.basename()
模块作用 os.path.basename() 用来取出路径最后面的文件名或目录名。 import os path = input() print(os.path.basename(path)) 小练习 输入内容是:/home/user
路径前面部分:os.path.dirname()
模块作用 os.path.dirname() 用来取出路径中最后一段前面的目录部分。 import os path = input() print(os.path.dirname(path)) 小练习 输入内容是:/home/user/