作者:潮汐
來源:Python 技術
你們知道今天聊聊關于 Python 一行代碼的神奇之處!!!
十六進制轉十進制
decimal = int('1010', 5)
print(decimal) #130
輸出:
130
# 轉換大小寫字母
str = "hi Python".upper()
print(str) #HI PYTHON
輸出:
HI PYTHON
# 轉換小寫字母
str_lower1 = "HI PYTHON".lower()
print(str_lower1)
str_lower2 = "HI PYTHON".casefold()
print(str_lower2)
輸出:
hi python
hi python
import math
fact_5 = math.factorial(5)
print(fact_5)
輸出:
120
words = ['Hello', 'Python', 'Hello', 'world']
print(max(words, key=len))
輸出:
Python
print("Hello, World!", file=open('test.txt', 'w'))
import time;
print(time.ctime())
輸出:
Sun Oct 30 22:52:41 2021
test_str = ''.join(list(filter(lambda x: x.isalpha(), 'abc4532def4fg56vcg2')))
print(test_str)
輸出:
abcdeffgvcg
# 第一種方式
n = 50
sum_n1 = sum(range(0, n 1))
print(sum_n1)
#第二種方式
sum_n2 = n*(n 1)//2
print(sum_n2)
print("hello python".count('l')) # 2
list(set['p','y','t','h','o','n'])
# d = {'five': 5, 'one': 1, 'four': 4, 'eight': 8}
{key:d[key] for key in sorted(d.keys())}
# {'eight': 8, 'five': 5, 'four': 4, 'one': 1}
# x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
{k: v for k, v in sorted(x.items(), key=lambda item: item[1])}
# {0: 0, 2: 1, 1: 2, 4: 3, 3: 4}
list(filter(lambda x: x%2 == 0, [1, 2, 3, 4, 5, 6] ))
# [2, 4, 6]
關于 Python 小技巧-一行代碼的操作還很多,後面咱們慢慢探索,希望大家一起進步。
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!