Python的内置函数可以大大简化代码,例如使用sum()来求和,max()和min()来找到最大值和最小值。
numbers = [1, 2, 3, 4, 5]
print(sum(numbers)) # 输出: 15列表推导式是一种简洁的方式来创建列表,它比循环更易于阅读和维护。
squares = [x**2 for x in range(1, 11)]
print(squares) # 输出: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]生成器可以节省内存,特别是当处理大量数据时。
def generate_numbers(n): for i in range(n): yield i
for number in generate_numbers(10): print(number) # 输出: 0 1 2 3 4 5 6 7 8 9在初始化变量时,可以使用解包来简化代码。
a, *b, c = [1, 2, 3, 4, 5]
print(a, b, c) # 输出: 1 [2, 3, 4] 5is比较对象身份使用is来比较对象的身份,而不是使用==来比较值。
a = [1, 2, 3]
b = a
print(a is b) # 输出: Trueall()和any()all()和any()可以用来测试条件是否对所有或任何元素都为真。
numbers = [1, 2, 3, 4, 5]
print(all(n % 2 == 0 for n in numbers)) # 输出: False
print(any(n % 2 == 0 for n in numbers)) # 输出: Truezip()来迭代多个序列zip()函数可以将多个序列组合成一个元组序列。
x = [1, 2, 3]
y = [4, 5, 6]
print(list(zip(x, y))) # 输出: [(1, 4), (2, 5), (3, 6)]enumerate()enumerate()函数可以同时提供索引和值。
for i, v in enumerate([1, 2, 3]): print(i, v) # 输出: 0 1 1 2 2 3 3 3map()和filter()map()和filter()可以应用于函数和可迭代对象。
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers))
filtered = list(filter(lambda x: x % 2 == 0, numbers))
print(squared) # 输出: [1, 4, 9, 16, 25]
print(filtered) # 输出: [2, 4]lambda函数lambda函数可以创建匿名函数。
add = lambda x, y: x + y
print(add(5, 3)) # 输出: 8defaultdictdefaultdict可以自动为不存在的键提供默认值。
from collections import defaultdict
d = defaultdict(int)
d['a'] += 1
d['b'] += 2
print(d) # 输出: defaultdict(, {'a': 1, 'b': 2}) CounterCounter可以用来计数可哈希对象。
from collections import Counter
words = 'python is great'.split()
print(Counter(words)) # 输出: Counter({'python': 1, 'is': 1, 'great': 1})setset可以用来存储不重复的元素。
numbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
unique_numbers = set(numbers)
print(unique_numbers) # 输出: {1, 2, 3, 4}f-string格式化字符串f-string提供了一种快速格式化字符串的方法。
name = "Alice"
age = 30
print(f"My name is {name} and I am {age} years old.") # 输出: My name is Alice and I am 30 years old.join()join()方法可以将字符串列表连接成一个字符串。
words = ['Python', 'is', 'awesome']
sentence = ' '.join(words)
print(sentence) # 输出: Python is awesomesplit()split()方法可以将字符串分割成列表。
sentence = "Hello, world!"
words = sentence.split()
print(words) # 输出: ['Hello,', 'world!']strip()strip()方法可以移除字符串开头和结尾的空白字符。
text = " Hello, world! "
cleaned_text = text.strip()
print(cleaned_text) # 输出: Hello, world!startswith()和endswith()startswith()和endswith()可以检查字符串是否以指定的前缀或后缀开始或结束。
text = "Python is great"
print(text.startswith("Python")) # 输出: True
print(text.endswith("great")) # 输出: Truelower()和upper()lower()和upper()可以将字符串转换为小写或大写。
text = "Python is great"
print(text.lower()) # 输出: python is great
print(text.upper()) # 输出: PYTHON IS GREATreplace()replace()方法可以替换字符串中的子串。
text = "Python is great"
print(text.replace("is", "was")) # 输出: Python was greatindex()和rindex()index()和rindex()可以找到子串的位置,如果没有找到,则抛出异常。
text = "Python is great"
print(text.index("is")) # 输出: 5
print(text.rindex("is")) # 输出: 5find()和rfind()find()和rfind()类似于index()和rindex(),但如果没有找到子串,则返回-1。
text = "Python is great"
print(text.find("is")) # 输出: 5
print(text.rfind("is")) # 输出: 5startswith()和endswith()startswith()和endswith()可以检查字符串是否以指定的前缀或后缀开始或结束。
text = "Python is great"
print(text.startswith("Python")) # 输出: True
print(text.endswith("great")) # 输出: Truesplit()split()方法可以将字符串分割成列表。
sentence = "Hello, world!"
words = sentence.split()
print(words) # 输出: ['Hello,', 'world!']strip()strip()方法可以移除字符串开头和结尾的空白字符。
text = " Hello, world! "
cleaned_text = text.strip()
print(cleaned_text) # 输出: Hello, world!startswith()和endswith()startswith()和endswith()可以检查字符串是否以指定的前缀或后缀开始或结束。
text = "Python is great"
print(text.startswith("Python")) # 输出: True
print(text.endswith("great")) # 输出: Truelower()和upper()lower()和upper()可以将字符串转换为小写或大写。
text = "Python is great"
print(text.lower()) # 输出: python is great
print(text.upper()) # 输出: PYTHON IS GREATreplace()replace()方法可以替换字符串中的子串。
text = "Python is great"
print(text.replace("is", "was")) # 输出: Python was greatsplit()split()方法可以将字符串分割成列表。
sentence = "Hello, world!"
words = sentence.split()
print(words) # 输出: ['Hello,', 'world!']strip()strip()方法可以移除字符串开头和结尾的空白字符。
text = " Hello, world! "
cleaned_text = text.strip()
print(cleaned_text) # 输出: Hello, world!