当前位置:网站首页>What magic things can a line of Python code do?

What magic things can a line of Python code do?

2020-11-07 20:19:23 Python a meow

import requests

(1) A line of code starts a Web service

python -m SimpleHTTPServer 8080  # python2python3 -m http.server 8080  # python3

 

(2) One line of code to achieve variable value exchange

a, b = 1, 2; a, b = b, a

(3) One line of code to solve FizzBuzz problem :

 

If you have difficulties in learning , Looking for one python Learning communication environment , Can join us python circle , Skirt number 947618024, Can claim python Learning materials , It will save a lot of time , Reduce a lot of problems .

FizzBuzz problem : Print digit 1 To 100, 3 Multiple printing “Fizz”, 5 Multiple printing “Buzz”, both 3 again 5 Multiple of the print “FizzBuzz”

print(' '.join(["fizz"[x % 3 * 4:]+"buzz"[x % 5 * 4:] or str(x) for x in range(1, 101)]))

(4) A line of code outputs specific characters "Love" Heart shaped

print('\n'.join([''.join([('Love'[(x-y) % len('Love')] 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(30, -30, -1)]))

 

(5) One line of code output Mandelbrot Images

Mandelbrot Images : Each position in the image corresponds to the formula N=x+y*i One of the plurals 、

print('\n'.join([''.join(['*'if abs((lambda a: lambda z, c, n: a(a, z, c, n))(lambda s, z, c, n: z if n == 0 else s(s, z*z+c, c, n-1))(0, 0.02*x+0.05j*y, 40)) < 2 else ' ' for x in range(-80, 20)]) for y in range(-20, 20)]))

 

(6) One line of code prints the multiplication table

print('\n'.join([' '.join(['%s*%s=%-2s' % (y, x, x*y) for y in range(1, x+1)]) for x in range(1, 10)]))

 

(7) A line of code calculates 1-100 The prime between ( Two versions )

print(' '.join([str(item) for item in filter(lambda x: not [x % i for i in range(2, x) if x % i == 0], range(2, 101))]))print(' '.join([str(item) for item in filter(lambda x: all(map(lambda p: x % p != 0, range(2, x))), range(2, 101))]))

 

(8) One line of code output Fibonacci sequence

print([x[0] for x in [(a[i][0], a.append([a[i][1], a[i][0]+a[i][1]])) for a in ([[1, 1]], ) for i in range(30)]])

 

(9) One line of code to implement the fast sorting algorithm

qsort = lambda arr: len(arr) > 1 and qsort(list(filter(lambda x: x <= arr[0], arr[1:]))) + arr[0:1] + qsort(list(filter(lambda x: x > arr[0], arr[1:]))) or arr

(10) One line of code to solve the eight queens problem

[__import__('sys').stdout.write('\n'.join('.' * i + 'Q' + '.' * (8-i-1) for i in vec) + "\n========\n") for vec in __import__('itertools').permutations(range(8)) if 8 == len(set(vec[i]+i for i in range(8))) == len(set(vec[i]-i for i in range(8)))]

 

(11) One line of code to implement the array of flatten function : Convert multidimensional arrays to one dimension

flatten = lambda x: [y for l in x for y in flatten(l)] if isinstance(x, list) else [x]

(12) One line of code list, It's sort of like the anti function of the last function

array = lambda x: [x[i:i+3] for i in range(0, len(x), 3)]

(13) One line of code to solve 2 Of 1000 The sum of the numbers to the power print(sum(map(int, str(2**1000))))

One more sentence at the end , Want to learn Python Please contact Xiaobian , Here's my own set python Learning materials and routes , Anyone who wants this information can enter q skirt 947618024 receive .

The material of this article comes from the Internet , If there is infringement, please contact to delete .

版权声明
本文为[Python a meow]所创,转载请带上原文链接,感谢