40个计算机必备|一行代码可以做些什么?

cfd06d5e9ca85cf856909b233ce862a6.png

点击蓝字 关注我们

6b660622048db13aaadf6a88f869ca07.png

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

210a87f3f3203c0e004969f4a5ea0508.png

2662c51702341b8cf9e10095e02a7a15.png

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

e5e30170a6c93cee6ffd2d695af7e4d7.png

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

4cb89b9898a8128b36915087fce4be4f.png

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)//2

8、交换两个值

a,b = b,a

9、斐波纳契数列

lambda x: x if x<=1 else fib(x-1) + fib(x-2)]

10、运行 HTTP 服务

python3 -m http.server 8000

11、反转列表

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 99

16、if-else

print("even") if 4%2==0 else print("odd")

17、无限循环

while 1:0

18、使用 print 语句写入文件

print("Hello, World!", file=open('file.txt', 'w'))

19、计算字符串中字符频率

print("umbrella".count('l'))
# 2

20、合并两个列表

list1.extend(list2)
# contents of list 2 will be added to the list1

21、合并两个字典

dict1.update(dict2)
# contents of dictionary 2 will be added to the dictionary 1

22、合并两个集合

set1.update(set2)
# contents of set2 will be copied to the set1

23、时间戳

import time; print(time.time())

24、八进制转十进制

print(int('30', 8)) 
# 24

25、十六进制转为十进制

print(int('da9', 16))
# 3497

26、求商和余数

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)
# abcdefghijklmnopqrstuvwxyz

30、得到一串大写字符串

import string; print(string.ascii_uppercase)
# ABCDEFGHIJKLMNOPQRSTUVWXYZ

31、得到 0-9 的字符串

import string; print(string.digits)
# 0123456789

32、得到人可读的时间

import time; print(time.ctime())
# Thu Aug 13 20:16:23 2020

33、转换字符串列表为整数

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 cmatrix

38、创建一个服务器

python -m http.server

39、打印乘法口诀表

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)))

如想了解更多有关编程的小知识,欢迎加入蓝桥云课专属代码交流群~

▼扫码加入我们▼

47053907a0d381d422c41d5c76dcff83.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值