统计日期是周几:datetime

统计日期是周几:datetime

模块作用 datetime 模块可以处理日期和时间。weekday() 可以得到这一天是周几。 from datetime import datetime d = datetime.strptime

阅读更多 →
提取文件后缀:pathlib.PurePosixPath.suffix

提取文件后缀:pathlib.PurePosixPath.suffix

模块作用 pathlib 模块可以更方便地处理路径。suffix 用来得到文件后缀。 from pathlib import PurePosixPath p = PurePosixPath(inpu

阅读更多 →
路径前面部分:os.path.dirname()

路径前面部分:os.path.dirname()

模块作用 os.path.dirname() 用来取出路径中最后一段前面的目录部分。 import os path = input() print(os.path.dirname(path)) 小

阅读更多 →
路径最后名字:os.path.basename()

路径最后名字:os.path.basename()

模块作用 os.path.basename() 用来取出路径最后面的文件名或目录名。 import os path = input() print(os.path.basename(path))

阅读更多 →
取平均数:statistics.mean()

取平均数:statistics.mean()

模块作用 statistics.mean() 用来求一组数字的平均数。 import statistics nums = list(map(int, input().split())) print(

阅读更多 →
统计单词数量:collections.Counter

统计单词数量:collections.Counter

模块作用 collections.Counter 用来快速统计每个内容出现了多少次。 from collections import Counter words = input().split()

阅读更多 →
随机挑一个:random.choice()

随机挑一个:random.choice()

模块作用 random.choice() 会从一组内容里随机选出一个。 import random seed_num = int(input()) colors = ["red"

阅读更多 →
随机整数:random.randint()

随机整数:random.randint()

模块作用 random.randint(a, b) 会在 a 到 b 之间随机选一个整数。配合固定种子时,结果可以重复出现。 import random seed_num = int(input()

阅读更多 →
求最大公约数:math.gcd()

求最大公约数:math.gcd()

模块作用 math.gcd() 用来找两个整数共同拥有的最大公约数。 import math a, b = map(int, input().split()) print(math.gcd(a, b

阅读更多 →
向上取整:math.ceil()

向上取整:math.ceil()

模块作用 math.ceil() 会把小数变成不小于它的整数。 import math x = float(input()) print(math.ceil(x)) 小练习 输入内容是:2.1,程

阅读更多 →