Python from a line to 30 lines of code within the fun of project recommendations! [continuous update]

Keywords: Python less

catalog

A line of code to realize the cardioid pattern

Two lines of code to make special dynamic QR code

Four lines of code generate exclusive word cloud:

Draw polygonal lines in five lines of code:

Ten lines of code to draw sunflower:

Twenty lines of code to realize the game of Jingzi chess

25 line code picture to character drawing

30 lines of code crawling any Baidu picture

Hello! Hello everyone! I'm a grey ape trying to make money to buy raw hair water!

Recently, I found a lot of interesting small projects in Python project development, and they are easy to start with with less code, so today I will share some interesting projects from one line of code to 30 lines of code with my little partners. After that, big gray wolf will continue to update more interesting projects here.

 

A line of code to realize the cardioid pattern

print('\n'.join([''.join([('lovelovelove'[(x-y)%12]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)]))

The effect is as follows:

 

 

Two lines of code to make special dynamic QR code

from MyQR import myqr
myqr.run(words='https://hao.360.com/',picture='Sources/gakki.gif',save_name='qr4.png',colorized=True)

The effect is as follows:

 

Four lines of code generate exclusive word cloud:

from wordcloud import WordCloud
cy = WordCloud()    # Create a word cloud object
cy.generate('HuiXiaoYuan look forward to making progress with you')    # Generative word cloud
cy.to_file('cy.png')    # Save word cloud

The effect is as follows:

 

Draw polygonal lines in five lines of code:

import turtle
t = turtle.Pen()
for x in range(360):
    t.forward(x)
    t.left(59)

The effect is as follows:

 

Ten lines of code to draw sunflower:

from turtle import *
color('red', 'yellow')
begin_fill()
while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()

The effect is as follows:

20 lines of code to realize the game of Jingzi chess

checkerboard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
                'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
                'low-L': ' ', 'low-M': ' ', 'low-R': ' '}

def printBoard(replace):
    print(checkerboard['top-L'] + '|' + checkerboard['top-M'] + '|' + checkerboard['top-R'])

    print('-' + '+' + '-')
    print(checkerboard['mid-L'] + '|' + checkerboard['mid-M'] + '|' + checkerboard['mid-R'])
    print('-' + '+' + '-')
    print(checkerboard['low-L'] + '|' + checkerboard['low-M'] + '|' + checkerboard['low-R'])
turn = 'X'
for i in range(9):
    printBoard(checkerboard)
    print("Now please" + turn + "Playing chess,Please enter your location:")
    move = input()
    checkerboard[move] = turn
    if turn == 'X':
        turn = 'O'
    else:turn = 'X'
    if i == 8:
        print("Game over, draw!")

The effect is as follows:

 

 

25 line code picture to character drawing

from PIL import Image
IMG = 't01b2a945701805d7f1.jpg' #Set picture file
WIDTH = 150 #Set the width of character painting
HEIGHT = 80 #Set character height
OUTPUT = 'output5.txt'  #Set the text file for character drawing
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")    #Set the character set displayed
def get_char(r,g,b,alpha = 256):
    if alpha == 0:
        return ' '
    length = len(ascii_char)
    gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
    unit = (255.0 + 1)/length
    return ascii_char[int(gray/unit)]
if __name__ == '__main__':
    im = Image.open(IMG)
    im = im.resize((WIDTH,HEIGHT), Image.NEAREST)
    txt = ""
    for i in range(HEIGHT):
        for j in range(WIDTH):
            txt += get_char(*im.getpixel((j,i)))
        txt += '\n'
    print(txt)
    with open(OUTPUT,'w') as f:
        f.write(txt)

The effect is as follows:

 

30 lines of code crawling any Baidu picture

import requests
import re
import time
url = "http://image.baidu.com/search/index?tn=baiduimage&word= Picachu“
urls = requests.get(url)    # Open link
urltext = urls.text     # Get all link text
urlre = re.compile('"objURL":"(.*?)"', re.S)    # Writing regular expressions
urllist = urlre.findall(urltext)    # Match by regular

with open("1.txt", "w") as txt:     # Write matching link to file
    for i in urllist:
        txt.write(i + "\n")
i = 0

# Loop through the list and download pictures
for urlimg in urllist:
    time.sleep(3)   # Program sleep for three seconds
    img = requests.get(urlimg, timeout = 5).content     # Open picture links in binary form
    if img:
        with open(str(i) + ".jpg", "wb") as imgs:   # Create a new jpg file to write in binary
            print("Downloading section%s Pictures %s" % (str(i+1), urlimg))
            imgs.write(img)     #Write picture to
            i += 1
        if i == 3:  #To avoid unlimited downloads, set the download picture as 3 here
            break
    else:
        print("Download failed!")

print("Download completed!")

The effect is as follows:

Individual project explanation link is attached:

Two lines of code to realize exclusive dynamic QR code

20 lines of code to realize the well character chessboard

25 lines of code to realize picture to character drawing

30 lines of code crawling any picture of Baidu

 

Think it's useful, remember to like it!

Meanwhile, you can also focus on my WeChat public official account of "wolf wolf cave", reply to "Python notes", get Python from entry to mastery of note sharing and common function method quick manual.

Wolf is looking forward to progress with you!

 

Posted by zoran on Sat, 27 Jun 2020 19:15:05 -0700