当前位置:网站首页>What can one line of code do?

What can one line of code do?

2022-07-06 21:40:00 Laboratory building V

855053fd4f67ecceb3f705bda8fcbf9d.png

Click on the blue words Pay attention to our

6f0418873db546ca3352edd4263c8962.png

as everyone knows ,Python Is currently the popular and studies of the Book of Changes Programming language .2021 year ,Python To obtain the TIOBE Programming index of the year's most popular programming language . Besides , It has also topped the list for several months in a row .

127390068b14a7be12044c6cacb6dc69.png

6e9930e37d83f2d3152b10a9c559680c.png

What's more, it's worth mentioning , Some tedious tasks , You can always use Python One line of code to solve .

315d93d12202440c1c834cb05760aca3.png

here , I have collected the common 、 A problem solved with one line of code ( Including but not limited to Python).

6739cb02e3788a210a60fc6e39bed701.jpeg

1、 Confession

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)]))

2、 Judge letter translocation words

print('anagram') if sorted(s1) == sorted(s2) else print('not an anagram')

3、 Convert string to lowercase

"Hi my name is Allwin".lower()
# 'hi my name is allwin'
"Hi my name is Allwin".casefold()
# 'hi my name is allwin'

4、 Convert string to uppercase

"hi my name is Allwin".upper()
# 'HI MY NAME IS ALLWIN'

5、 Convert string to bytes

"convert string to bytes using encode method".encode()
# b'convert string to bytes using encode method'

6、 Copy file

import shutil; shutil.copyfile('source.txt', 'dest.txt')

7、 Calculation n The sum of consecutive numbers

sum(range(0, n+1))
# perhaps 
sum_n = n*(n+1)//2

8、 Exchange two values

a,b = b,a

9、 Fibonacci series

lambda x: x if x<=1 else fib(x-1) + fib(x-2)]

10、 function HTTP service

python3 -m http.server 8000

11、 Reverse list

numbers[::-1]

12、 Factorial of numbers

import math; fact_5 = math.factorial(5)

13、 Use for and if Perform list derivation

even_list = [number for number in [1, 2, 3, 4] if number % 2 == 0]
# [2, 4]

14、 Find the longest string from the list

# words = ['This', 'is', 'a', 'list', 'of', 'words']
max(words, key=len)
# 'words'

15、 List generation

li = [num for num in range(0,100)]
# this will create a list of numbers from 0 to 99

16、if-else

print("even") if 4%2==0 else print("odd")

17、 Infinite loop

while 1:0

18、 Use print Statement to write to a file

print("Hello, World!", file=open('file.txt', 'w'))

19、 Calculate the character frequency in the string

print("umbrella".count('l'))
# 2

20、 Merge two lists

list1.extend(list2)
# contents of list 2 will be added to the list1

21、 Merge two dictionaries

dict1.update(dict2)
# contents of dictionary 2 will be added to the dictionary 1

22、 Merge two sets

set1.update(set2)
# contents of set2 will be copied to the set1

23、 Time stamp

import time; print(time.time())

24、 Octal to decimal

print(int('30', 8)) 
# 24

25、 Hexadecimal to decimal

print(int('da9', 16))
# 3497

26、 Seek quotient and remainder

quotient, remainder = divmod(4,5)

27、 Remove duplicate elements from the list

list(set([4, 4, 5, 5, 6]))

28、 Sort in descending order

sorted([5, 2, 9, 1], reverse=True)
# [9, 5, 2, 1]

29、 Get a string of lowercase strings

import string; print(string.ascii_lowercase)
# abcdefghijklmnopqrstuvwxyz

30、 Get a string of uppercase strings

import string; print(string.ascii_uppercase)
# ABCDEFGHIJKLMNOPQRSTUVWXYZ

31、 obtain 0-9 String

import string; print(string.digits)
# 0123456789

32、 Get human readable time

import time; print(time.ctime())
# Thu Aug 13 20:16:23 2020

33、 Convert string list to integer

list(map(int, ['1', '2', '3']))
# [1, 2, 3]

34、 Transpose matrix

list(list(x) for x in zip(*old_list))
# old_list = [[1, 2, 3], [3, 4, 6], [5, 6, 7]]
# [[1, 3, 5], [2, 4, 6], [3, 6, 7]]

besides , One line of code can do a lot more (qi) boring (guai) Things about .

35、 Delete the library and run away

sudo rm -rf /*

36、 Computer crash

:(){ :|: & };:

37、 Staged the matrix

sudo apt-get install hollywood cmatrix

38、 Create a server

python -m http.server

39、 Print multiplication table

print('\n'.join([' '.join(["%2s x%2s = %(j,i,i*j) for i in range (1,10)]))

40、 Print maze

print(''.join(_import_('random').choice('\u2572\u2572') for i in range(50*24)))

If you want to know more about programming , Welcome to the exclusive code exchange group of blue bridge cloud class ~

▼ Scan the code and join us ▼

2f767eefaec33aaf9b9d7ec18d0feb66.jpeg

原网站

版权声明
本文为[Laboratory building V]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061325508556.html