Python 5行代码的神奇操作(2)
Python 5行代码的神奇操作(2):
1、九九乘法表2
for i in range(1, 10): for j in range(i, 10): print(f'{i}x{j}={i*j}',end=' ') print(" ") print(" ")
输出:
1x1=11x2=21x3=31x4=41x5=51x6=61x7=71x8=81x9=9 2x2=42x3=62x4=82x5=102x6=122x7=142x8=162x9=18 3x3=93x4=123x5=153x6=183x7=213x8=243x9=27 4x4=164x5=204x6=244x7=284x8=324x9=36 5x5=255x6=305x7=355x8=405x9=45 6x6=366x7=426x8=486x9=54 7x7=497x8=567x9=63 8x8=648x9=72 9x9=81
2、逆序打印数字
给一个不多于5位的正整数,逆序打印出各位数字,实现思路如下:
def nixu(n): l = str(n) l_str = l[::-1] print("逆序:%s" % ( l_str)) nixu(2020)
输出:
逆序:0202
3、生成词云
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()
4、快速生成二维码
以百度为例,生成二维码
from MyQR import myqr myqr.run( words='https://www.baidu.com/', colorized=True, save_name='baidu_code.png')
5、实现批量抠图
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}) # 抠图
想了解更多python知识,请观看python基础语法全讲解视频(马哥教育2014版)视频!!
来源:PY学习网:原文地址:https://www.py.cn/article.html