当前位置:网站首页>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 ▼
边栏推荐
- string的底层实现
- R language for text mining Part4 text classification
- Reflection operation exercise
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- 基于InsightFace的高精度人脸识别,可直接对标虹软
- Search map website [quadratic] [for search map, search fan, search book]
- C# 如何在dataGridView里设置两个列comboboxcolumn绑定级联事件的一个二级联动效果
- 3D人脸重建:从基础知识到识别/重建方法!
- MySQL - 事务(Transaction)详解
- 爬虫实战(五):爬豆瓣top250
猜你喜欢
愛可可AI前沿推介(7.6)
Leetcode topic [array] -118 Yang Hui triangle
039. (2.8) thoughts in the ward
KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning
缓存更新策略概览(Caching Strategies Overview)
JPEG2000-Matlab源码实现
50个常用的Numpy函数解释,参数和使用示例
抖音将推独立种草App“可颂”,字节忘不掉小红书?
[in depth learning] pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
【力扣刷题】32. 最长有效括号
随机推荐
[Li Kou brush questions] 32 Longest valid bracket
通过数字电视通过宽带网络取代互联网电视机顶盒应用
The use method of string is startwith () - start with XX, endswith () - end with XX, trim () - delete spaces at both ends
Tiktok will push the independent grass planting app "praiseworthy". Can't bytes forget the little red book?
嵌入式开发的7大原罪
Internet News: Geely officially acquired Meizu; Intensive insulin purchase was fully implemented in 31 provinces
KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning
Is this the feeling of being spoiled by bytes?
Torch Cookbook
@GetMapping、@PostMapping 和 @RequestMapping详细区别附实战代码(全)
数字化转型挂帅复产复工,线上线下全融合重建商业逻辑
Nodejs tutorial let's create your first expressjs application with typescript
It's not my boast. You haven't used this fairy idea plug-in!
Guava: use of multiset
技术分享 | 抓包分析 TCP 协议
el-table表格——sortable排序 & 出现小数、%时排序错乱
R language for text mining Part4 text classification
快讯:飞书玩家大会线上举行;微信支付推出“教培服务工具箱”
Yuan Xiaolin: safety is not only a standard, but also Volvo's unchanging belief and pursuit
Four common ways and performance comparison of ArrayList de duplication (jmh performance analysis)