三元运算符是 if-else 语句的简写。语法是value_if_true if condition else value_if_false。三元运算符是一行代码,可以替代多行 if-else 语句,使你的代码更加简洁。
【资料图】
a = 5b = 10max = a if a > b else b # value_if_true if condition else value_if_falseprint(max)# 10
上面的代码通过检查“a”是否大于“b”,如果为真则返回“a”,如果为假则返回“b”。
2.枚举函数enumerate()函数向可迭代对象添加一个计数器,并以枚举对象的形式返回。当你想要遍历列表并跟踪索引时,此函数很有用。
fruits = ["apple", "banana", "mango"]for index, fruit in enumerate(fruits):print(index, fruit)# 0 apple# 1 banana#2 mango3. 压缩函数
zip()函数聚合来自每个可迭代对象的元素并返回一个元组迭代器。当你想同时遍历两个或多个列表时,此函数很有用。
list1 = [1, 2, 3]list2 = ["a", "b", "c"]for x, y in zip(list1, list2):print(x, y)# 1 a# 2 b# 3 c4. 列表生成式
列表生成式是一种从现有列表或任何可迭代对象创建列表的简洁方法。这是一种可以替代 for 循环的单行代码,使你的代码更加高效,并使代码的可读性更强。
squared_numbers = [x**2 for x in range(1, 6)]print(squared_numbers)# [1, 4, 9, 16, 25]5. 匿名函数
Lambda 函数是使用lambda关键字定义的匿名函数。当你需要编写一次性的小函数并且不想使用关键字def来定义命名函数时,它们很有用。
add = lambda x, y: x + yresult = add(3, 4)print(result)# 76.any()和all()函数
any()函数和all()函数返回True或False基于 iterable 中元素的真实性。如果 iterable 中的任何元素为真,则函数any()返回True,如果 iterable 中的所有元素都为真,则函数all()返回True。
numbers = [1, 2, 3, 0, 4]result = any(numbers) # Trueresult = all(numbers) # False。0使结果为False7. 迭代模块
itertools模块提供了一组函数来处理迭代器。该模块中的函数包括chain、product和permutations。
import itertoolsnumbers = [1, 2, 3]result = list(itertools.permutations(numbers))# 输出所有排列组合# [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]8. 生成器
生成器是一种可迭代的类型,它可以即时生成值,而不是将它们存储在内存中。它是使用yield关键字定义的,用于创建自定义迭代器。
# 使用yield关键字创建生成器def fibonacci_series(n):a, b = 0, 1for i in range(n):yield aa, b = b, a + b# 输出迭代器中的值for number in fibonacci_series(10):print(number)# 0# 1# 1# 2# 3# 5# 8# 13# 21# 349.装饰器
装饰器是一种修改函数或类行为的方法。使用@符号进行定义,可用于向函数添加功能,例如日志记录、计时或身份验证。
def log_function(func):def wrapper(*args, **kwargs):print(f"Running {func.__name__}")result = func(*args, **kwargs)print(f"{func.__name__} returned {result}")return resultreturn wrapper@log_functiondef add(x, y):return x + yprint(add(5,7))# 运行add函数,返回值为1210. 使用多个函数参数
在 Python 中,可以使用*和**运算符来处理多个函数参数。*运算符用于将参数列表作为单独的位置参数进行传递,运算符**用于传递关键字参数的字典。
def print_arguments(*args, **kwargs):print(args)print(kwargs)print_arguments(1, 2, 3, name="John", age=30)# (1, 2, 3)# {"name": "John", "age": 30}11. 动态导入
当你想根据用户输入或配置导入模块时,可以使用模块动态导入模块importlib。
import importlibmodule_name = "math"module = importlib.import_module(module_name)result = module.sqrt(9)12. 字典生成式
字典生成式是一种从现有字典或任何可迭代对象创建字典的简洁方法。它是一种可以替代 for 循环的单行代码,使你的代码更加高效,代码可读性更强。
squared_numbers = {x: x**2 for x in range(1, 6)}print(squared_numbers)# {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}13. 可调用对象
在 Python 中,任何可以称为函数的对象都称为可调用对象,包括函数、方法、类,甚至是定义__call__方法的对象。
class Adder:def __call__(self, x, y):return x + yadder = Adder()result = adder(3, 4)print(result)#714.用下划线分隔大数字/字符
大数字很难一眼看出来是多大,在 Python 中可以用下划线来使数字更易读。
num_test = 100_345_405 # 一个大数字print(num_test)# 10034540515.快速合并两个字典
可以使用以下代码在 Python 中快速合并 2两个字典。
dictionary_one = {a: 1, b: 2}dictionary_two = {c: 3, d: 4}merged = {**dictionary_one, **dictionary_two}print(merged)# {"a": 1, "b": 2, "c": 3, "d": 4}16. 列表、集合和字典是可变的
可变意味着可以更改或更新对象(列表、集合或字典),而无需更改内存中对象的指针。实际效果可见如下示例。
在下面的示例中,通过添加一个新城市来更新城市列表,可以看到 ID(对象指针)保持不变,集合和字典也是如此。
cities = [Munich, Zurich, London]print(id(cities)) # 2797174365184cities.append(Berlin)print(id(cities)) # 2797174365184# 集合my_set = {1, 2, 3}print(id(my_set)) # 2797172976992my_set.add(4)print(id(my_set)) # 2797172976992# 字典thisdict = {brand: Ford,model: Mustang,year: 1964}print(id(thisdict)) #2797174128256thisdict[engine] = 2500ccprint(id(thisdict)) #2797174128256