数学函数
Python的内置函数提供了一些基础的数学功能,而不需要导入任何模块。这些包括:
- abs(x): 返回x的绝对值
- divmod(x, y): 返回一对商和余数
- max(iterable, *[, default=obj, key=func]): 返回集合中的最大值
- min(iterable, *[, default=obj, key=func]): 返回集合中的最小值
- pow(x, y[, z]): 返回x的y次幂;如果提供了z,则返回x的y次幂对z取模的结果
- round(x[, n]): 返回x四舍五入到n个小数位的值
- sum(iterable[, start]): 对集合进行求和
这些函数不需要额外的导入,可以直接在Python代码中使用。例如:
# Absolute value
print(abs(-10)) # Output: 10
# Divmod
print(divmod(10, 3)) # Output: (3, 1)
# Max and Min
print(max([1, 2, 3, 4, 5])) # Output: 5
print(min([1, 2, 3, 4, 5])) # Output: 1
# Power
print(pow(2, 3)) # Output: 8
print(pow(2, 3, 3)) # Output: 2
# Round
print(round(3.14159, 2)) # Output: 3.14
# Sum
print(sum([1, 2, 3, 4, 5])) # Output: 15
字符串函数
Python提供了一系列内置的字符串方法,可以对字符串进行常见的操作。以下是一些常用的基础字符串函数:
- str.capitalize(): 将字符串的第一个字符转换为大写,其余字符转换为小写。
- str.upper(): 将字符串中的所有字符转换为大写。
- str.lower(): 将字符串中的所有字符转换为小写。
- str.title(): 将字符串中的每个单词的首字母转换为大写。
- str.swapcase(): 将字符串中的小写字母转换为大写,大写字母转换为小写。
- str.strip(): 从字符串的开始和结束处删除空格。
- str.lstrip(): 从字符串的开始处删除空格。
- str.rstrip(): 从字符串的结束处删除空格。
- str.find(sub[, start[, end]]): 返回子字符串sub在字符串中首次出现的索引,如果没有找到则返回-1。
- str.replace(old, new[, count]): 返回一个新字符串,其中old字符串被替换为new字符串,如果指定了count,则只替换前count次出现。
- str.split(sep=None, maxsplit=-1): 返回一个列表,其中包含字符串被sep分隔的部分,如果没有指定sep,则默认为任何空白字符。
- str.join(iterable): 将iterable中的字符串元素连接成一个字符串,使用调用字符串作为分隔符。
- str.startswith(prefix[, start[, end]]): 检查字符串是否以prefix开头,是则返回True,否则返回False。
- str.endswith(suffix[, start[, end]]): 检查字符串是否以suffix结尾,是则返回True,否则返回False。
- str.count(sub[, start[, end]]): 返回子字符串sub在字符串中出现的次数。
text = "hello world"
# 将字符串的第一个字符转换为大写
print(text.capitalize()) # 输出: "Hello world"
# 将字符串中的所有字符转换为大写
print(text.upper()) # 输出: "HELLO WORLD"
# 将字符串中的所有字符转换为小写
print(text.lower()) # 输出: "hello world"
# 将字符串中的每个单词的首字母转换为大写
print(text.title()) # 输出: "Hello World"
# 将字符串中的小写字母转换为大写,大写字母转换为小写
print(text.swapcase()) # 输出: "HELLO WORLD"
# 从字符串的开始和结束处删除空格
print(" hello world ".strip()) # 输出: "hello world"
# 从字符串的开始处删除空格
print(" hello world ".lstrip()) # 输出: "hello world "
# 从字符串的结束处删除空格
print(" hello world ".rstrip()) # 输出: " hello world"
# 查找子字符串的位置
print(text.find('world')) # 输出: 6
# 将字符串中的子字符串替换为另一个字符串
print(text.replace('world', 'Python')) # 输出: "hello Python"
# 将字符串分割成列表
print(text.split()) # 输出: ['hello', 'world']
# 将列表中的字符串元素连接成一个字符串
print(", ".join(['hello', 'world'])) # 输出: "hello, world"
# 检查字符串是否以某个子字符串开头
print(text.startswith('hello')) # 输出: True
# 检查字符串是否以某个子字符串结尾
print(text.endswith('world')) # 输出: True
# 计算子字符串在字符串中出现的次数
print(text.count('l')) # 输出: 3
列表函数
Python中的列表是一种可变的序列类型,提供了多种方法来进行列表操作。以下是一些常用的列表函数及其代码示例:
- list.append(x): 在列表的末尾添加一个元素。
- list.extend(iterable): 将一个可迭代对象的所有元素添加到列表的末尾。
- list.insert(i, x): 在指定位置插入一个元素。
- list.remove(x): 移除列表中第一个值为x的元素。
- list.pop([i]): 移除列表中指定位置的元素,并返回该元素的值。
- list.clear(): 移除列表中的所有元素。
- list.index(x[, start[, end]]): 返回列表中第一个值为x的元素的索引。
- list.count(x): 返回x在列表中出现的次数。
- list.sort(key=None, reverse=False): 对列表进行排序。
- list.reverse(): 反转列表中的元素。
- list.copy(): 返回列表的浅复制。
以下是使用这些列表函数的代码示例:
# 创建一个空列表
my_list = []
# 添加元素
my_list.append(1)
my_list.append(2)
my_list.append(3)
print(my_list) # 输出: [1, 2, 3]
# 扩展列表
my_list.extend([4, 5])
print(my_list) # 输出: [1, 2, 3, 4, 5]
# 在指定位置插入元素
my_list.insert(1, 'a')
print(my_list) # 输出: [1, 'a', 2, 3, 4, 5]
# 移除元素
my_list.remove('a')
print(my_list) # 输出: [1, 2, 3, 4, 5]
# 弹出元素
popped_element = my_list.pop(2)
print(popped_element) # 输出: 3
print(my_list) # 输出: [1, 2, 4, 5]
# 清空列表
my_list.clear()
print(my_list) # 输出: []
# 重新赋值列表
my_list = [1, 2, 3, 2, 4, 2]
# 查找元素索引
index_of_first_two = my_list.index(2)
print(index_of_first_two) # 输出: 1
# 计算元素出现次数
count_of_twos = my_list.count(2)
print(count_of_twos) # 输出: 3
# 排序列表
my_list.sort()
print(my_list) # 输出: [1, 2, 2, 2, 3, 4]
# 反转列表
my_list.reverse()
print(my_list) # 输出: [4, 3, 2, 2, 2, 1]
# 复制列表
new_list = my_list.copy()
print(new_list) # 输出: [4, 3, 2, 2, 2, 1]