当前位置:网站首页>What can one line of code do?
What can one line of code do?
2022-07-06 21:40:00 【Laboratory building V】
Click on the blue words Pay attention to our
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 .
What's more, it's worth mentioning , Some tedious tasks , You can always use Python One line of code to solve .
here , I have collected the common 、 A problem solved with one line of code ( Including but not limited to Python).
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 ▼
边栏推荐
猜你喜欢
对话阿里巴巴副总裁贾扬清:追求大模型,并不是一件坏事
嵌入式开发的7大原罪
【滑动窗口】第九届蓝桥杯省赛B组:日志统计
Numpy download and installation
Fastjson parses JSON strings (deserialized to list, map)
【Redis设计与实现】第一部分 :Redis数据结构和对象 总结
Summary of cross partition scheme
Five wars of Chinese Baijiu
numpy 下载安装
guava:Collections.unmodifiableXXX创建的collection并不immutable
随机推荐
npm run dev启动项目报错 document is not defined
JS操作dom元素(一)——获取DOM节点的六种方式
JS学习笔记-OO创建怀疑的对象
Fastjson parses JSON strings (deserialized to list, map)
After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
Fzu 1686 dragon mystery repeated coverage
Redistemplate common collection instructions opsforhash (IV)
js中,字符串和数组互转(二)——数组转为字符串的方法
Redistemplate common collection instructions opsforlist (III)
语谱图怎么看
Leetcode topic [array] -118 Yang Hui triangle
ViT论文详解
ACdreamoj1110(多重背包)
Web开发小妙招:巧用ThreadLocal规避层层传值
R3live notes: image processing section
Numpy download and installation
JPEG2000-Matlab源码实现
038. (2.7) less anxiety
Forward maximum matching method
Reflection operation exercise