随机挑一个:random.choice()
模块作用 random.choice() 会从一组内容里随机选出一个。 import random seed_num = int(input()) colors = ["red"
随机整数:random.randint()
模块作用 random.randint(a, b) 会在 a 到 b 之间随机选一个整数。配合固定种子时,结果可以重复出现。 import random seed_num = int(input()
求最大公约数:math.gcd()
模块作用 math.gcd() 用来找两个整数共同拥有的最大公约数。 import math a, b = map(int, input().split()) print(math.gcd(a, b
向上取整:math.ceil()
模块作用 math.ceil() 会把小数变成不小于它的整数。 import math x = float(input()) print(math.ceil(x)) 小练习 输入内容是:2.1,程
向下取整:math.floor()
模块作用 math.floor() 会把小数变成不大于它的整数。 import math x = float(input()) print(math.floor(x)) 小练习 输入内容是:8.9
开平方:math.sqrt()
模块作用 math 模块里有很多数学工具,math.sqrt() 用来求一个数的平方根。 import math n = int(input()) print(math.sqrt(n)) 小练习
反转顺序:reversed()
函数作用 reversed() 用来按相反顺序查看内容。 s = input() print("".join(reversed(s))) 小练习 输入内容是:code,程序会输出
统计出现次数:count()
函数作用 count() 用来统计某个内容在字符串或列表中出现了多少次。 s = input() print(s.count("a")) 小练习 输入内容是:banana,程序会
拼接文字:join()
函数作用 join() 用来把多个字符串连接起来,并在中间放入指定内容。 parts = input().split() print("-".join(parts)) 小练习 输
分开文字:split()
函数作用 split() 用来把一整段字符串按规则拆开,变成多个小部分。 s = input() print(s.split()) 小练习 输入内容是:red blue green,程序会输出什么?