# set()
# setattr()
# slice()
# sorted()
# staticmethod()
# str()
# sum()
# super()
# set() 函数示例
fruits = ['apple', 'banana', 'cherry', 'apple', 'banana']
unique_fruits = set(fruits)
print(unique_fruits) # 输出: {'cherry', 'apple', 'banana'}
# setattr() 函数示例
class Person:
pass
person = Person()
setattr(person, 'name', 'lfsun')
print(person.name) # 输出: lfsun
# slice() 函数示例:slice(): slice() 函数用于创建切片对象。它接受三个参数:起始索引、结束索引和步长。
# 使用 slice() 函数可以创建一个可重复使用的切片对象,然后将其应用于序列(如列表、字符串等)以获取指定的切片。
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
sliced_numbers = numbers[slice(2, 7, 2)]
print(sliced_numbers) # 输出: [2, 4, 6]
# sorted() 函数示例
numbers = [5, 2, 8, 1, 6]
sorted_numbers = sorted(numbers)
print(sorted_numbers) # 输出: [1, 2, 5, 6, 8]
# staticmethod() 函数示例
class MathUtils:
@staticmethod
def add(x, y):
return x + y
result = MathUtils.add(2, 3)
print(result) # 输出: 5
# str() 函数示例
number = 42
number_str = str(number)
print(number_str) # 输出: '42'
# sum() 函数示例
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(total) # 输出: 15
# super() 函数示例
class Parent:
def __init__(self):
self.name = 'John'
class Child(Parent):
def __init__(self):
super().__init__()
child = Child()
print(child.name) # 输出: John
Python - 内置函数 - s
猜你喜欢
转载自blog.csdn.net/qq_43116031/article/details/131076484
今日推荐
周排行