⼏个好玩有趣的Python ⼊门实例
⼏个好玩有趣的Python ⼊门实例
⼏个简单的Python 实例,好玩有趣,基础语法。turtle库开始⾃⼰的绘画写⼀个属于⾃⼰的⽂本进度条蒙特卡洛⽅法计算π分形⼏何,Koch雪花曲线输⼊⼀组数据,进⾏简单的统计⼩说词频统计,统计三国演义中谁出场率最⾼
轻松抓住⽂本主旨:⽂本词云⽣成
drawing with turtle
好玩有趣,⼊门级绘图库turtle,⽂档在此。
⽂档中的实例:太阳花。
画⼀个六芒星:from turtle import *color('red', 'yellow')begin_fill()while True: forward(200) left(170) if abs(pos()) < 1: break end_fill()done()
1
2
3
4
5
6
7
8
9
10
11
⽂本进度条
不停向下打印的版本:import turtle as t # size 传⼊最⼤的圆的半径def draw_Star(size): t.setup(1200, 1000) t.speed(11) t.pensize(2) t.pencolor('grey') for r, pen_size in [(size//7, 3), (size*6//7, 4), (size, 4)]: t.penup() t.goto(0, -r) t.pendown() t.pensize(pen_size) t.circle(r) t.penup() t.goto(0, 0) r = size * 5.5 // 7 for i in range(6): t.pendown() t.pensize(3) t.seth(i * 60 + 30) t.fd(r) t.goto(0, 0) t.goto(0, r) t.seth(-150) t.pensize(5) for i in range(6): t.fd(r) t.left(60) r = r - size // 50 t.goto(0, r) t.pensize(10) t.seth(-120) t.pencolor('orange') for i in range(3): t.fd(r * (3**0.5)) t.left(120) t.penup() t.goto(0, 0) t.seth(30) t.fd(r) t.seth(-180) t.pendown() for i in range(3): t.fd(r * (3 ** 0.5)) t.left(120) t.hideturtle() t.done()draw_Star(400)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
c语言指针的含义22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
纯css特效41
42
43
44
45
46
47
48
49
50
51
单⾏刷新版本:
单⾏刷新效果在cmd窗⼝中运⾏才有效,IDLE禁\r转义字符。
蒙特卡洛⽅法计算π
随机数⽣成使⽤random库,⽂档在此。思路即是随机⽣成点,落在正⽅形内。计算正⽅形内的圆内落点与正⽅形内落点之⽐,近似为⾯积之⽐,随机数越随机,数量越⼤越准确。
递归绘制Koch 雪花曲线
koch雪花曲线是分形⼏何中的⼀个经典曲线。import time def bar(scale): print('===========执⾏开始============') for i in range(scale + 1): a = '*' * i b = '.' * (scale - i) c = (i / scale) * 100 print('\r{:^3.0f}%[{}->{}]'.format(c, a, b), end = '') time.sleep(0.05) print('\n===========执⾏结束============')
1
2
3
4
5
6
7
8
9
10
11
12import time def pro_bar(scale): print('执⾏开始'.center(scale // 2, '=')) start = time.perf_counter()
for i in range(scale + 1): a = '*' * i b = '.' * (scale - i) c = (i / scale) * 100 dur = time.perf_counter() - start print('\r{:^3.0f}%[{}->{}] {:.2f}s'.format(c, a, b, dur), end = '') time.sleep(0.05) print('\n' + '执⾏结束'.center(scale // 2, '='))pro_bar(50)
1
2
3
4
5
6
7
8
9
10
11
domestic的名词12
13python基础代码100例
14
15
16from random import random from time import perf_counter def calPI(N = 100): hits = 0 start = perf_counter() for i in range(1, N*N+1): x, y = random(), random() dist = pow(x ** 2 + y ** 2, 0.5) if dist <= 1.0: hits += 1 pi = (hits * 4) / (N * N) use_time = perf_counter() - start return pi, use_time PI, use_time = calPI(1000)print('\nuse Monte Carlo method to calculate PI: {}'.format(PI))print('use time: {} s'.format(use_time))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
使⽤turtle库,简单的递归即可绘制。
简单统计
输⼊⼀组数据,计算均值,⽅差,中位数,绝对相对误差import turtle def koch(size, n): if n == 0: turtle.fd(size) else: for angle in [0, 60, -120, 60]: turtle.left(angle) koch(size/3, n-1)def main(): turtle.setup(1200, 1000) turtle.speed(11) turtle.penup() (-300, 200) turtle.pendown() turtle.pensize(2) level = 4 koch(600, level) # 3阶科赫曲线 turtle.right(120) koch(600, level) turtle.right(120) koch(600, level) turtle.hideturtle() turtle.done()main()
1
2
3
4
5
6小程序傻瓜式开发
7
8
9
10
11
12
13
14
15
16
17
18
19
20
冒泡排序c语言方法21
22
23
24
25
26
27
28
⼩说词频统计
策略:
中⽂⼩说:使⽤第三⽅库jieba切分,统计
英⽂⼩说:去掉特殊⼲扰字符,直接切分为单词,遍历统计统计莎⼠⽐亚经典名著:Hamlet。统计最⾼出现单词。# -*- coding: utf-8 -*-# 输⼊数据def getNum(): nums = [] iNumStr = input('please input a sequence of numbers (enter to exit): ') while iNumStr != '': nums.append(eval(iNumStr))
iNumStr = input('please input a sequence of numbers (enter to exit): ') return nums # 平均数def average(numbers): return sum(numbers) / len(numbers)# 标准差def dev(numbers, average): sdev = 0.0 for num in numbers: sdev += (num - average) ** 2 return pow(sdev / len(numbers), 0.5)# 中位数def median(numbers): sorted(numbers) size = len(numbers) if size % 2 == 0: return (numbers[size//2-1] + numbers[size//2]) / 2 else: return numbers[size//2]# 绝对与相对误差def rel_dev(numbers, average): _max = max(abs(max(numbers) - average), abs(min(numbers) - average)) return _max, _max / average def main(): nums = getNum() if len(nums) == 0: print('no data') else: ave = average(nums) devs = rel_dev(nums, ave) print('和:{:.4f},平均数:{:.4f},中位数:{:.4f},⽅差:{:.4f},绝对误差:{:4f},相对误差:{:.4f}' \ .format(sum(nums), ave, median(nums), dev(nums, ave), devs[0], devs[1]))if __name__ == '__main__': main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论