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))
相关推荐
winfredzhang40 分钟前
用 MediaPipe 手势数字识别一键打开下载夹里的图片(Python + OpenCV 实战)
人工智能·python·opencv·google·mediapipe
某人辛木5 小时前
Web自动化测试
前端·python·pycharm·pytest
C+++Python5 小时前
详细介绍一下Java泛型的通配符
java·windows·python
能喵烧香5 小时前
深度解析:Linux 与 Windows 超级权限账户的本质差异
linux·windows
pixcarp6 小时前
知识库系统的内容资产闭环怎么设计
服务器·数据库·后端·golang
小帅热爱难回头6 小时前
编写Skill生成AI落地项目系统架构
python
diving deep7 小时前
脚本速览-python
开发语言·python
2601_951643778 小时前
Python第一,Java跌出前三,C语言杀回来了
java·c语言·python·编程语言排行·技术趋势
莫名的好感°8 小时前
手机RAR解压怎么选?2026年二季度四款产品问答
服务器·网络·智能手机
caimouse9 小时前
Reactos 第 7 章 视窗报文 — 7.5 视窗报文的发送
windows