当前位置:网站首页>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 ▼

边栏推荐
- 50个常用的Numpy函数解释,参数和使用示例
- Proxy and reverse proxy
- 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
- 跨分片方案 总结
- Redistemplate common collection instructions opsforlist (III)
- Why does MySQL index fail? When do I use indexes?
- PostgreSQL 安装gis插件 CREATE EXTENSION postgis_topology
- Four common ways and performance comparison of ArrayList de duplication (jmh performance analysis)
- Comparison between multithreaded CAS and synchronized
- ICML 2022 | flowformer: task generic linear complexity transformer
猜你喜欢

uni-app App端半屏连续扫码
![[sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics](/img/2d/9a7e88fb774984d061538e3ad4a96b.png)
[sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics

Microsoft technology empowerment position - February course Preview
![Happy sound 2[sing.2]](/img/ca/1581e561c427cb5b9bd5ae2604b993.jpg)
Happy sound 2[sing.2]
![[redis design and implementation] part I: summary of redis data structure and objects](/img/2e/b147aa1e23757519a5d049c88113fe.png)
[redis design and implementation] part I: summary of redis data structure and objects

Seven original sins of embedded development

numpy 下载安装

Why do job hopping take more than promotion?

Aiko ai Frontier promotion (7.6)

ViT论文详解
随机推荐
In JS, string and array are converted to each other (II) -- the method of converting array into string
npm run dev启动项目报错 document is not defined
14 years Bachelor degree, transferred to software testing, salary 13.5k
JS操作dom元素(一)——获取DOM节点的六种方式
Why does MySQL index fail? When do I use indexes?
Study notes of grain Mall - phase I: Project Introduction
el-table表格——获取单击的是第几行和第几列 & 表格排序之el-table与sort-change、el-table-column与sort-method & 清除排序-clearSort
The underlying implementation of string
It's not my boast. You haven't used this fairy idea plug-in!
JPEG2000 matlab source code implementation
C language char, wchar_ t, char16_ t, char32_ Relationship between T and character set
[go][转载]vscode配置完go跑个helloworld例子
guava:Collections.unmodifiableXXX创建的collection并不immutable
Five wars of Chinese Baijiu
Absolute primes (C language)
启动嵌入式间:资源有限的系统启动
SQL:存储过程和触发器~笔记
红杉中国,刚刚募资90亿美元
Shake Sound poussera l'application indépendante de plantation d'herbe "louable", les octets ne peuvent pas oublier le petit livre rouge?
ICML 2022 | flowformer: task generic linear complexity transformer