当前位置:网站首页>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)//28、 Exchange two values
a,b = b,a9、 Fibonacci series
lambda x: x if x<=1 else fib(x-1) + fib(x-2)]10、 function HTTP service
python3 -m http.server 800011、 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 9916、if-else
print("even") if 4%2==0 else print("odd")17、 Infinite loop
while 1:018、 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'))
# 220、 Merge two lists
list1.extend(list2)
# contents of list 2 will be added to the list121、 Merge two dictionaries
dict1.update(dict2)
# contents of dictionary 2 will be added to the dictionary 122、 Merge two sets
set1.update(set2)
# contents of set2 will be copied to the set123、 Time stamp
import time; print(time.time())24、 Octal to decimal
print(int('30', 8))
# 2425、 Hexadecimal to decimal
print(int('da9', 16))
# 349726、 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)
# abcdefghijklmnopqrstuvwxyz30、 Get a string of uppercase strings
import string; print(string.ascii_uppercase)
# ABCDEFGHIJKLMNOPQRSTUVWXYZ31、 obtain 0-9 String
import string; print(string.digits)
# 012345678932、 Get human readable time
import time; print(time.ctime())
# Thu Aug 13 20:16:23 202033、 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 cmatrix38、 Create a server
python -m http.server39、 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 ▼

边栏推荐
- Dialogue with Jia Yangqing, vice president of Alibaba: pursuing a big model is not a bad thing
- Michael smashed the minority milk sign
- Redistemplate common collection instructions opsforset (V)
- 3D人脸重建:从基础知识到识别/重建方法!
- Sql: stored procedures and triggers - Notes
- JS学习笔记-OO创建怀疑的对象
- MySQL - transaction details
- Yuan Xiaolin: safety is not only a standard, but also Volvo's unchanging belief and pursuit
- 3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
- 快讯:飞书玩家大会线上举行;微信支付推出“教培服务工具箱”
猜你喜欢

Summary of cross partition scheme

20220211 failure - maximum amount of data supported by mongodb

2022 fields Award Announced! The first Korean Xu Long'er was on the list, and four post-80s women won the prize. Ukrainian female mathematicians became the only two women to win the prize in history
![[in depth learning] pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs](/img/66/4d94ae24e99599891636013ed734c5.png)
[in depth learning] pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs

Study notes of grain Mall - phase I: Project Introduction

Z function (extended KMP)

Set up a time server

Seven original sins of embedded development

Dialogue with Jia Yangqing, vice president of Alibaba: pursuing a big model is not a bad thing

ViT论文详解
随机推荐
愛可可AI前沿推介(7.6)
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
[redis design and implementation] part I: summary of redis data structure and objects
guava:Collections.unmodifiableXXX创建的collection并不immutable
Fastjson parses JSON strings (deserialized to list, map)
Reflection operation exercise
对话阿里巴巴副总裁贾扬清:追求大模型,并不是一件坏事
3D人脸重建:从基础知识到识别/重建方法!
在最长的距离二叉树结点
[interpretation of the paper] machine learning technology for Cataract Classification / classification
In JS, string and array are converted to each other (I) -- the method of converting string into array
el-table表格——获取单击的是第几行和第几列 & 表格排序之el-table与sort-change、el-table-column与sort-method & 清除排序-clearSort
数字化转型挂帅复产复工,线上线下全融合重建商业逻辑
Efficiency tool +wps check box shows the solution to the sun problem
JS学习笔记-OO创建怀疑的对象
039. (2.8) thoughts in the ward
string的底层实现
首批入选!腾讯安全天御风控获信通院业务安全能力认证
Search map website [quadratic] [for search map, search fan, search book]
Ravendb starts -- document metadata