当前位置:网站首页>一行代码可以做些什么?
一行代码可以做些什么?
2022-07-06 13:26:00 【实验楼v】

点击蓝字 关注我们

众所周知,Python 是目前流行和易学的编程语言。2021 年,Python 获得了 TIOBE 编程指数的年度最受欢迎的编程语言。此外,它也连续蝉联了多个月的榜首。


更值得一提的是,一些繁琐的任务,往往都可以用 Python 的一行代码解决。

这里,我收集了常见的、用一行代码解决的问题(包括但不限于 Python)。

1、表白
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、判断字母易位词
print('anagram') if sorted(s1) == sorted(s2) else print('not an anagram')3、转换字符串为小写
"Hi my name is Allwin".lower()
# 'hi my name is allwin'
"Hi my name is Allwin".casefold()
# 'hi my name is allwin'4、转换字符串为大写
"hi my name is Allwin".upper()
# 'HI MY NAME IS ALLWIN'5、转换字符串为字节
"convert string to bytes using encode method".encode()
# b'convert string to bytes using encode method'6、复制文件
import shutil; shutil.copyfile('source.txt', 'dest.txt')7、计算 n 个连续数的和
sum(range(0, n+1))
#或者
sum_n = n*(n+1)//28、交换两个值
a,b = b,a9、斐波纳契数列
lambda x: x if x<=1 else fib(x-1) + fib(x-2)]10、运行 HTTP 服务
python3 -m http.server 800011、反转列表
numbers[::-1]12、数的阶乘
import math; fact_5 = math.factorial(5)13、使用 for 和 if 进行列表推导
even_list = [number for number in [1, 2, 3, 4] if number % 2 == 0]
# [2, 4]14、从列表中找出最长字符串
# words = ['This', 'is', 'a', 'list', 'of', 'words']
max(words, key=len)
# 'words'15、列表生成
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、无限循环
while 1:018、使用 print 语句写入文件
print("Hello, World!", file=open('file.txt', 'w'))19、计算字符串中字符频率
print("umbrella".count('l'))
# 220、合并两个列表
list1.extend(list2)
# contents of list 2 will be added to the list121、合并两个字典
dict1.update(dict2)
# contents of dictionary 2 will be added to the dictionary 122、合并两个集合
set1.update(set2)
# contents of set2 will be copied to the set123、时间戳
import time; print(time.time())24、八进制转十进制
print(int('30', 8))
# 2425、十六进制转为十进制
print(int('da9', 16))
# 349726、求商和余数
quotient, remainder = divmod(4,5)27、从列表中移除重复元素
list(set([4, 4, 5, 5, 6]))28、以降序进行排序
sorted([5, 2, 9, 1], reverse=True)
# [9, 5, 2, 1]29、得到一串小写字符串
import string; print(string.ascii_lowercase)
# abcdefghijklmnopqrstuvwxyz30、得到一串大写字符串
import string; print(string.ascii_uppercase)
# ABCDEFGHIJKLMNOPQRSTUVWXYZ31、得到 0-9 的字符串
import string; print(string.digits)
# 012345678932、得到人可读的时间
import time; print(time.ctime())
# Thu Aug 13 20:16:23 202033、转换字符串列表为整数
list(map(int, ['1', '2', '3']))
# [1, 2, 3]34、转置矩阵
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]]除此之外,一行代码还可以做很多有(qi)趣(guai)的事情。
35、删库跑路
sudo rm -rf /*36、电脑死机
:(){ :|: & };:37、上演黑客帝国
sudo apt-get install hollywood cmatrix38、创建一个服务器
python -m http.server39、打印乘法口诀表
print('\n'.join([' '.join(["%2s x%2s = %(j,i,i*j) for i in range (1,10)]))40、打印迷宫
print(''.join(_import_('random').choice('\u2572\u2572') for i in range(50*24)))如想了解更多有关编程的小知识,欢迎加入蓝桥云课专属代码交流群~
▼扫码加入我们▼

边栏推荐
- Forward maximum matching method
- Seven original sins of embedded development
- Nodejs教程之Expressjs一篇文章快速入门
- How do I remove duplicates from the list- How to remove duplicates from a list?
- C # use Oracle stored procedure to obtain result set instance
- 技术分享 | 抓包分析 TCP 协议
- Study notes of grain Mall - phase I: Project Introduction
- 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
- MySQL - transaction details
- Reference frame generation based on deep learning
猜你喜欢

【滑动窗口】第九届蓝桥杯省赛B组:日志统计

爬虫实战(五):爬豆瓣top250

967- letter combination of telephone number

Data Lake (VIII): Iceberg data storage format

【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug

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

JPEG2000-Matlab源码实现

KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning

中国白酒的5场大战

Quick news: the flybook players' conference is held online; Wechat payment launched "education and training service toolbox"
随机推荐
14年本科毕业,转行软件测试,薪资13.5K
guava:Collections.unmodifiableXXX创建的collection并不immutable
爱可可AI前沿推介(7.6)
MLP (multilayer perceptron neural network) is a multilayer fully connected neural network model.
Z function (extended KMP)
3D人脸重建:从基础知识到识别/重建方法!
el-table表格——sortable排序 & 出现小数、%时排序错乱
【力扣刷题】32. 最长有效括号
MySQL - 事务(Transaction)详解
Absolute primes (C language)
[Li Kou brushing questions] one dimensional dynamic planning record (53 change exchanges, 300 longest increasing subsequence, 53 largest subarray and)
Guava: three ways to create immutablexxx objects
抖音将推独立种草App“可颂”,字节忘不掉小红书?
Interviewer: what is the internal implementation of ordered collection in redis?
el-table表格——获取单击的是第几行和第几列 & 表格排序之el-table与sort-change、el-table-column与sort-method & 清除排序-clearSort
红杉中国,刚刚募资90亿美元
Web开发小妙招:巧用ThreadLocal规避层层传值
One line by line explanation of the source code of anchor free series network yolox (a total of ten articles, you can change the network at will after reading it, if you won't complain to me)
【力扣刷题】一维动态规划记录(53零钱兑换、300最长递增子序列、53最大子数组和)
Redistemplate common collection instructions opsforlist (III)