为全司分享python

225 阅读2分钟

当我司让我做一场代码分享,以下是分享的文档原文

主题:带你走进python的大世界 分享人:世根 预计时间:45分钟

代码案例

推荐以兴趣引导式学习,任何编程语言都是“入门容易精通难”,“唯有热爱能抵漫长岁月”。接下来,我将以一些用python实现的炫酷效果带大家进入python的大世界,从激发兴趣开始。

代码地址:~/temp/python/数据可视化/cool_effert_demo.ipynb

案例包含:

  1. 控制台个性化字体
  2. 文字滚动
  3. 画彩色多边形
  4. 一行代码实现迷宫
  5. 一行代码实现爱心
  6. 丘比特之箭
  7. 黑客
  8. 几行代码实现web开发
  • 控制台个性化字体
def colorful_text(text):
    from termcolor import colored

    # 打印彩色文本
    print(colored(text, 'red'))

    # 打印带有背景颜色的文本 -蓝底白字
    print(colored(text, 'white', 'on_blue'))

    # 打印带有属性的文本-加粗、带下划线的绿色文本
    print('the words i say:', colored(text, 'green', attrs=['bold', 'underline']))
  • 文字滚动(走马灯)
# rotate text animation
import time

def rotate_text(text):
    for i in range(len(text)):
        print(text[i:] + text[:i], end="\r")
        time.sleep(0.1)

text = "Loading..."
rotate_text(text)
  • 画彩色多边形
# draw colorful pattern
import turtle

colors = ["red", "green", "blue", "purple"]
turtle.speed(10)

for i in range(360):
    turtle.pencolor(colors[i % 4])
    turtle.width(i / 100 + 1)
    turtle.forward(i)
    turtle.left(59)

turtle.done()
  • 控制台迷宫
python -c "while 1:import random;print(random.choice('|| __'), end='')"
  • Say love
print('\n'.join([''.join([('Love'[(x-y)%4]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else' ')for x in range(-30,30)])for y in range(15,-15,-1)]))
  • 丘比特
import turtle as t

def init():
    t.speed(2)
    t.pensize(2)
    t.screensize(480, 360)
    t.color('red', 'red')

def draw_heart_right():
    t.up()
    t.goto(50, 50)
    t.pendown()
    t.right(45)
    t.goto(100, 0)
    t.seth(45)
    t.fd(120)
    t.circle(50, 225)

def draw_heart_left():
    t.up()
    t.goto(0, 0)
    t.down()
    t.seth(45)
    t.fd(120)
    t.circle(50, 225)
    t.seth(90)
    t.circle(50, 225)
    t.fd(120)

def draw_arrow():
    t.up()
    t.seth(0)
    # 羽毛
    t.goto(-210, 40)
    t.pendown()
    t.goto(-215, 44)
    t.goto(-190, 49)
    t.goto(-175, 46)
    t.up()

    t.goto(-210, 40)
    t.pendown()
    t.goto(-213, 34)
    t.goto(-185, 39)
    t.goto(-175, 46)
    t.up()

    # 箭杆
    t.pendown()
    t.goto(0, 80)
    t.penup()
    t.goto(160, 110)
    t.pendown()
    t.goto(320, 140)

    # 箭羽
    t.left(160)
    t.begin_fill()
    t.fd(10)
    t.left(120)
    t.fd(10)
    t.left(120)
    t.fd(10)
    t.end_fill()

if __name__ == '__main__':
    init()
    name = t.textinput("爱心", "输入你的名字")
    if name == '宝贝':
        draw_heart_right()
        draw_heart_left()
        draw_arrow()
        t.color('pink')
        t.write("唯一的爱送你我爱的唯一,宝贝", font=('宋体', 15, 'normal'))
        t.hideturtle()
        t.done()
    else:
        # t.exitonclick()
        t.bye()
  • 黑客
def hei_ke():
    # 参数
    SCREENSIZE=(600,600)
    BLACK=(0,0,0,13)
    # 初始化
    pygame.init()
    font = pygame.font.SysFont('宋体', 20)
    screen = pygame.display.set_mode(SCREENSIZE)
    surface = pygame.Surface(SCREENSIZE, flags=pygame.SRCALPHA)
    pygame.Surface.convert(surface)
    surface.fill(BLACK)
    screen.fill(BLACK)
    # 内容
    lib=[chr(i) for i in range(48,48+10)] + [chr(i) for i in range(97,97+26)]   # [0-9 a-z]
    texts = [font.render(l, True, (0, 255, 0)) for l in lib]
    cols = list(range(40))  # 字体15, 窗口600
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
        pygame.time.delay(33)
        screen.blit(surface, (0, 0))
        for i in range(n:=len(cols)):
            text = random.choice(texts)
            # 随机闪烁
            x,y=random.randint(0,n-1),random.randint(0,n-1)
            screen.blit(text,(x*15,cols[y]*15))
        pygame.display.flip()
  • web开发
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run()

python的思考

  1. 为什么需要这样的语言
  2. 这个语言的诞生对人类社会的影响

  • 1989年的圣诞节期间,吉多*范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的解释程序,作为ABC语言的一种继承。
  • 之所以选中Python(蟒蛇)作为程序的名字,是因为他是BBC电视剧--蒙提*派森的飞行马戏团(Monty Python’s Flying Circus)的爱好者。
  • 同一样问题,用不同的语言解决,代码量差距还是很多的,一般情况下Python是Java的1/5,所以说人生苦短,我用Python。

和其他编程语言的对比

安装配置

安装Python环境的方式分为两种:

  1. 官网安装包,所占空间大约25M,常用于新手学习、实际生产(首推云镜像:Index of python-local);

  1. Anaconda安装包,由于其中内置了常用的科学计算工具、环境管理工具,所占空间大约600M,常用于开发。

开发工具

  • Pycharm

  • vscode

实际应用

  • 个人开发者-秒变职场大神

  • 行业应用

www.paddlepaddle.org.cn/customercas…

学习路线分享

学习某一项技术是一条漫长的路,我一直在思考广度和深度的问题。比较占据我内心主流的方式是:先广度再深度,学习的过程类似于启发式教育。任何技术语言都只是一种工具,重在于我们怎样去使用它们提升自身的效能,实现高速的发展。

以上选取的是某培训机构的课程教育大纲,基本符合我们自学的路线。

学习资源分享

python入门,包括:

  • 掌握面向过程编程的全部基础语法

  • 会安装第三方库

  • 会一些基础的读写文件操作

打一波广告,可以从我的学习笔记中的爬虫开始:shigen01- CSDN搜索

视频教程

选择一: 北京理工大学的嵩天老师的python入门课程(经典):www.icourse163.org/course/BIT-…

选择二:B站小甲鱼(幽默):www.bilibili.com/video/BV1c4… (入门部分:1-58集)

文字教程

选择三:如有别的编程语言的基础,可直接阅读文字版快速了解Python的特性:

参考文章

互动交流

自由提问,聊天框互动