网站首页 > 技术文章 正文
以下是一些有趣且富有创意的Python代码示例,涵盖不同领域(如图形、算法、游戏等):
生成艺术图案(ASCII艺术)
import math
for y in range(-12, 13):
row = []
for x in range(-60, 60):
c = complex(x/30, y/15)
z = 0
for i in range(16):
z = z*z + c
if abs(z) > 2:
break
row.append(' .,:;*%$@'[i % 10] if i < 15 else ' ')
print(''.join(row))
这段代码生成一个曼德勃罗分形图案,利用复数运算和循环控制字符输出。
用乌龟绘图模拟雪花
import turtle
def koch_curve(t, length, depth):
if depth == 0:
t.forward(length)
else:
for angle in [60, -120, 60, 0]:
koch_curve(t, length/3, depth-1)
t.left(angle)
t = turtle.Turtle()
t.speed(0)
t.penup()
t.goto(-200, 100)
t.pendown()
for _ in range(3):
koch_curve(t, 400, 4)
t.right(120)
turtle.done()
递归实现科赫雪花曲线,调整depth参数可以改变复杂度。
文字加密与解密
def caesar_cipher(text, shift, mode='encrypt'):
result = ""
for char in text:
if char.isalpha():
offset = 65 if char.isupper() else 97
new_pos = (ord(char) - offset + (shift if mode == 'encrypt' else -shift)) % 26
result += chr(new_pos + offset)
else:
result += char
return result
message = "Python is fun!"
encrypted = caesar_cipher(message, 5)
decrypted = caesar_cipher(encrypted, 5, 'decrypt')
print(f"Original: {message}\nEncrypted: {encrypted}\nDecrypted: {decrypted}")
实现凯撒密码的加密与解密功能,可自定义偏移量。
快速生成二维码
import qrcode
data = "https://www.python.org"
qr = qrcode.QRCode(version=1, box_size=10, border=4)
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color="navy", back_color="white")
img.save("python_qr.png")
需要安装qrcode库,可自定义颜色和尺寸。
模拟生命游戏
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def update(frameNum, img, grid, N):
new_grid = grid.copy()
for i in range(N):
for j in range(N):
total = int((grid[i, (j-1)%N] + grid[i, (j+1)%N] +
grid[(i-1)%N, j] + grid[(i+1)%N, j] +
grid[(i-1)%N, (j-1)%N] + grid[(i-1)%N, (j+1)%N] +
grid[(i+1)%N, (j-1)%N] + grid[(i+1)%N, (j+1)%N])/1)
if grid[i, j] == 1:
if (total < 2) or (total > 3):
new_grid[i, j] = 0
else:
if total == 3:
new_grid[i, j] = 1
img.set_data(new_grid)
grid[:] = new_grid[:]
return img
N = 100
grid = np.random.choice([0, 1], N*N, p=[0.85, 0.15]).reshape(N, N)
fig, ax = plt.subplots()
img = ax.imshow(grid, interpolation='nearest')
ani = animation.FuncAnimation(fig, update, fargs=(img, grid, N), frames=10, interval=50)
plt.show()
需要numpy和matplotlib库,模拟康威生命游戏的演化过程。
猜你喜欢
- 2025-08-01 「Python爬虫」:破解网站字体加密和反反爬虫
- 2025-08-01 3 行代码扒光 PDF 所有图片?Python 办公实战秘籍来了
- 2025-08-01 为你的python程序上锁:软件序列号生成器
- 2025-08-01 带了一个不熟练Python的女生,好崩溃
- 2025-08-01 Python的RSA操作(私钥与公钥)
- 2025-08-01 Python 3 加密简介
- 2025-08-01 解密Python时间测量迷雾:高精度计时器time.perf_counter的妙用
- 2025-08-01 如何用逻辑运算符写出更优雅的代码?深度剖析底层逻辑
- 2025-08-01 Python中PyPDF2库全解析:轻松玩转PDF文件处理
- 2025-08-01 Python提取极客时间-阿里云HLS加密视频解密过程分析
- 08-03Python列表方法append和extend的区别
- 08-03Python列表集合操作介绍?
- 08-03python数据类型之列表、字典、元组、集合及操作
- 08-03Python学不会来打我(11)列表list详解:用法、场景与类型转换
- 08-03Python骚操作从列表推导和生成器表达式开始
- 08-03Python中的列表详解及示例
- 08-03Python自动化办公应用学习笔记20—列表排序、列表推导式
- 08-03python入门012:复制列表
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)