zoukankan      html  css  js  c++  java
  • Python 5 行代码的神奇操作

    Python 语言实现功能直接了当,简明扼要,今天咱们就来一起看看 Python 5 行代码的神奇操作!

    1、古典兔子问题

    很多人学习python,不知道从何学起。
    很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。
    很多已经做案例的人,却不知道如何去学习更加高深的知识。
    那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代码!
    QQ群:1097524789

    有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

    def count(n):
        if (1 == n or 2 == n):
            return 1
        elif (n >= 2):
            return count(n - 2) + count(n - 1)
    print(count(36) * 2)

    2、加法计算器

    num1 = input("第一个数:")
    num2 = input("第二个数:")
    new_num1 = int(num1)
    new_num2 = int(num2)
    print(new_num1 + new_num2)

    3、循环问答

    while(True):
        question = input()
        answer = question.replace('吗', '呢')
        answer = answer.replace('?', '!')
        print(answer)

    输出:

    在吗
    在呢
    吃饭了吗
    吃饭了呢
    要下班了吗
    要下班了呢
    最近好吗
    最近好呢

    4、实现一个简单的服务器

    from http import server
    from http.server import SimpleHTTPRequestHandler
    server_address = ('127.0.0.1', 8888)
    httpd = server.HTTPServer(server_address, SimpleHTTPRequestHandler)
    httpd.serve_forever()

    5、九九乘法表1

    for i in range(1, 10):
        for j in range(1, i+1):
            print('{}x{}={}	'.format(j, i, i*j), end='')
        print()

    输出:

    1x1=1	
    1x2=2	2x2=4	
    1x3=3	2x3=6	3x3=9	
    1x4=4	2x4=8	3x4=12	4x4=16	
    1x5=5	2x5=10	3x5=15	4x5=20	5x5=25	
    1x6=6	2x6=12	3x6=18	4x6=24	5x6=30	6x6=36	
    1x7=7	2x7=14	3x7=21	4x7=28	5x7=35	6x7=42	7x7=49	
    1x8=8	2x8=16	3x8=24	4x8=32	5x8=40	6x8=48	7x8=56	8x8=64	
    1x9=9	2x9=18	3x9=27	4x9=36	5x9=45	6x9=54	7x9=63	8x9=72	9x9=81

    6、九九乘法表2

    for i in range(1, 10):
        for j in range(i, 10):
            print(f'{i}x{j}={i*j}',end='	')
        print(" ")
    print("
    ")

    输出:

    1x1=1	1x2=2	1x3=3	1x4=4	1x5=5	1x6=6	1x7=7	1x8=8	1x9=9	 
    2x2=4	2x3=6	2x4=8	2x5=10	2x6=12	2x7=14	2x8=16	2x9=18	 
    3x3=9	3x4=12	3x5=15	3x6=18	3x7=21	3x8=24	3x9=27	 
    4x4=16	4x5=20	4x6=24	4x7=28	4x8=32	4x9=36	 
    5x5=25	5x6=30	5x7=35	5x8=40	5x9=45	 
    6x6=36	6x7=42	6x8=48	6x9=54	 
    7x7=49	7x8=56	7x9=63	 
    8x8=64	8x9=72	 
    9x9=81

    7、逆序打印数字

    给一个不多于5位的正整数,逆序打印出各位数字,实现思路如下:

    def nixu(n):
        l = str(n)
        l_str = l[::-1]
        print("逆序:%s" % ( l_str))
    nixu(2020)

    输出:

    逆序:0202

    8、生成词云

    from wordcloud import WordCloud
    import PIL.Image as image
    with open('wordcloud.txt') as fp:
        text = fp.read()
        wordcloud = WordCloud().generate(text)
        img = wordcloud.to_image()
        img.show()

    9、快速生成二维码

    以百度为例,生成二维码

    from MyQR import myqr
    myqr.run(
        words='https://www.baidu.com/',
        colorized=True,
        save_name='baidu_code.png')

    10、实现批量抠图

    抠图具体教程详见 Python装逼指南–五行代码实现批量抠图

    import os, paddlehub as hub
    huseg = hub.Module(name='deeplabv3p_xception65_humanseg') # 加载模型
    path = './imgs/' # 文件目录
    files = [path + i for i in os.listdir(path)] # 获取文件列表
    results = huseg.segmentation(data={'image': files}) # 抠图
    

    总结

    今天文章安利一些小技巧,希望对大家有一定的帮助,继续向前吧!

  • 相关阅读:
    自制2048小游戏(附源码)
    PyQt5设计思路(长期更新,每写一篇新博客都会更新一次)
    Orthogonal table 实现理论
    Convex Hull 实现理论
    elasticsearch 命令操作
    Springboot
    2018/3/8错题解析
    初始MyBatis
    如何做好微信订阅号
    在路上,三线城市互联网创业记录
  • 原文地址:https://www.cnblogs.com/shann001/p/13096393.html
Copyright © 2011-2022 走看看