SoFunction
Updated on 2024-10-29

A summary of some Python 5-line code magic operations

The Python language is straightforward and concise, so let's take a look at Python's magic in 5 lines of code today!

1. Classical rabbit problem

There is a pair of rabbits that gives birth to a pair of rabbits every month from the third month after birth, and a pair of rabbits every month after the young rabbits reach their third month of life. If none of the rabbits die, what is the total number of rabbits in each month?

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. Addition calculator

num1 = input("First number:")
num2 = input("Second number:")
new_num1 = int(num1)
new_num2 = int(num2)
print(new_num1 + new_num2)

3. Circular Question and Answer

while(True):
    question = input()
    answer = ('?', 'And')
    answer = ('?', '!')
    print(answer)

Output:

are you there?
at this point
Did you eat?
It's time to eat.
Getting off work?
It's time to go home.
How's it going?
How have you been?

4. Realize a simple server

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

5. Multiplication table 1

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

Output:

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. Multiplication table 2

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

Output:

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. Printing numbers in reverse order

Give a not more than 5 bits of positive integers, reverse order to print out all the digits, the realization of the idea is as follows:

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

Output:

Reverse order:0202

8、Generate word cloud

from wordcloud import WordCloud
import  as image
with open('') as fp:
    text = ()
    wordcloud = WordCloud().generate(text)
    img = wordcloud.to_image()
    ()

word cloud

9、Rapidly generate QR code

Baidu, for example, generates QR codes

from MyQR import myqr
(
    words='/',
    colorized=True,
    save_name='baidu_code.png')

Baidu QR code

10, the realization of batch keying

For a detailed tutorial on keying, see Python pretend guide - five lines of code to achieve batch keying

import os, paddlehub as hub
huseg = (name='deeplabv3p_xception65_humanseg') # Loading models
path = './imgs/' # File directory
files = [path + i for i in (path)] # Get a list of files
results = (data={'image': files}) # keying

summarize

To this article on the magic of some Python 5 lines of code to this article, more related to Python 5 lines of code content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!