20个常用的Python脚本

以下是20个常用的Python脚本示例:

  1. 计算阶乘
python 复制代码
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

print(factorial(5))
  1. 斐波那契数列
python 复制代码
def fibonacci(n):
    if n <= 1:
        return n
    else:
        return fibonacci(n-1) + fibonacci(n-2)

print(fibonacci(10))
  1. 判断素数
python 复制代码
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

print(is_prime(7))
  1. 字符串反转
python 复制代码
def reverse_string(s):
    return s[::-1]

print(reverse_string("hello"))
  1. 字符串大小写转换
python 复制代码
def convert_case(s, case):
    if case == "upper":
        return s.upper()
    elif case == "lower":
        return s.lower()
    else:
        return s

print(convert_case("Hello", "lower"))
  1. 字符串替换
python 复制代码
def replace_string(s, old, new):
    return s.replace(old, new)

print(replace_string("hello world", "world", "python"))
  1. 列表排序
python 复制代码
def sort_list(lst):
    return sorted(lst)

print(sort_list([3, 1, 4, 2]))
  1. 列表去重
python 复制代码
def remove_duplicates(lst):
    return list(set(lst))

print(remove_duplicates([1, 2, 2, 3, 4, 4]))
  1. 列表合并
python 复制代码
def merge_lists(lst1, lst2):
    return lst1 + lst2

print(merge_lists([1, 2, 3], [4, 5, 6]))
  1. 列表切片
python 复制代码
def slice_list(lst, start, end):
    return lst[start:end]

print(slice_list([1, 2, 3, 4, 5], 1, 4))
  1. 字典键值对调换
python 复制代码
def swap_dict(d):
    return {v: k for k, v in d.items()}

print(swap_dict({"a": 1, "b": 2, "c": 3}))
  1. 计算圆的面积和周长
python 复制代码
import math

def circle_area_circumference(radius):
    area = math.pi * radius**2
    circumference = 2 * math.pi * radius
    return area, circumference

print(circle_area_circumference(5))
  1. 计算两个数的最大公约数和最小公倍数
python 复制代码
def gcd_lcm(a, b):
    gcd = a if b == 0 else gcd_lcm(b, a % b)
    lcm = a * b // gcd
    return gcd, lcm

print(gcd_lcm(12, 18))
  1. 计算矩阵乘法
python 复制代码
def matrix_multiply(A, B):
    return [[sum(a * b for a, b in zip(row_A, col_B)) for col_B in zip(*B)] for row_A in A]

print(matrix_multiply([[1, 2], [3, 4]], [[5, 6], [7, 8]]))
  1. 计算阶乘和
python 复制代码
def factorial_sum(n):
    return sum(range(1, n+1))

print(factorial_sum(5))
  1. 生成斐波那契数列的前n项
python 复制代码
def generate_fibonacci(n):
    return [fibonacci(i) for i in range(n)]

print(generate_fibonacci(10))
  1. 生成随机数列表
python 复制代码
import random

def generate_random_list(n, start, end):
    return [random.randint(start, end) for _ in range(n)]

print(generate_random_list(5, 1, 10))
  1. 生成九九乘法表
python 复制代码
def print_multiplication_table():
    for i in range(1, 10):
        for j in range(1, i+1):
            print(f"{j}x{i}={i*j}", end="\t")
        print()

print_multiplication_table()
  1. 生成随机密码
python 复制代码
import random
import string

def generate_password(length):
    return ''.join(random.choices(string.ascii_letters + string.digits, k=length))

print(generate_password(8))
  1. 生成随机字符串
python 复制代码
import random
import string

def generate_random_string(length):
    return ''.join(random.choices(string.ascii_letters, k=length))

print(generate_random_string(8))
相关推荐
happylifetree3 分钟前
Python06-08:Python开发工具PyCharm安装
python·pycharm
Tairitsu_H5 分钟前
[Linux系统] 基础IO核心 | 系统接口 | 文件描述符 | 重定向
linux·服务器·文件操作
玖石书6 分钟前
LVGL的windows开发环境安装指南
windows·数字人·lvgl·仿真
liuchangng11 分钟前
类Jev项目Kev从入门到实战(1):Kev 是什么——不开权重也能自训的决策模型
人工智能·python·jev·kev·决策模型
事已至此先睡覺吧21 分钟前
(三)Linux 基础指令(二):基础常用的一些指令
linux·运维·服务器
leihefeng29 分钟前
PX06-对比两个 Excel 文件的差异:从逐单元格到生产级实现
python·excel
qetfw1 小时前
Windows Server AD CS:根 CA、Web 证书模板与域内自动注册
前端·windows·windows-server
Ticnix1 小时前
MCP 上个月把自己推翻重写了:Session 没了、Sampling 废了——你学的教程还停在 2025
python·agent·全栈
“AI国潮设计-小江”1 小时前
[AIGC实战] 基于Stable Diffusion的潮汕非遗IP自动化生成工作流(附Python批量处理脚本)
开发语言·人工智能·python·prompt·aigc
不会写DN1 小时前
Go日志库工程选型与逃逸分析评测报告
java·服务器·golang