Python 内置函数详解

Python 内置函数详解

Python 内置函数详解

1. 引言

Python 中,有许多内置的函数可用于执行各种任务。这些内置函数是 Python 解释器中的一部分,无需导入任何模块即可直接使用。本文将详细介绍一些常用的内置函数,并提供示例代码和运行结果。

2. 内置函数列表

下面是一些常用的 Python 内置函数:

  • print(): 打印输出信息。
  • len(): 返回对象的长度或元素个数。
  • type(): 返回对象的类型。
  • int(): 将一个对象转换为整数。
  • float(): 将一个对象转换为浮点数。
  • str(): 将一个对象转换为字符串。
  • list(): 将一个可迭代对象转换为列表。
  • dict(): 创建一个字典或将可迭代对象转换为字典。
  • set(): 创建一个集合或将可迭代对象转换为集合。
  • range(): 创建一个指定范围的整数序列。
  • input(): 获取用户输入。

3. 示例代码

3.1. print()

print() 函数用于将指定的对象打印到标准输出。可以传入多个对象,并用逗号分隔。示例代码如下:

print("Hello, world!")
print("The answer is", 42)
Python

运行结果:

Hello, world!
The answer is 42

3.2. len()

len() 函数返回指定对象的长度或元素个数。它适用于字符串、列表、元组、字典、集合等。示例代码如下:

s = "Hello, world!"
print(len(s))  # 输出:13

lst = [1, 2, 3, 4, 5]
print(len(lst))  # 输出:5

d = {"name": "Alice", "age": 20}
print(len(d))  # 输出:2
Python

运行结果:

13
5
2

3.3. type()

type() 函数返回指定对象的类型。示例代码如下:

s = "Hello, world!"
print(type(s))  # 输出:<class 'str'>

x = 42
print(type(x))  # 输出:<class 'int'>

lst = [1, 2, 3, 4, 5]
print(type(lst))  # 输出:<class 'list'>
Python

运行结果:

<class 'str'>
<class 'int'>
<class 'list'>

3.4. int()

int() 函数将指定的对象转换为整数。如果对象是浮点数或字符串,它将被转换为整数;如果对象是其他类型,将引发 TypeError。示例代码如下:

x = 42.5
print(int(x))  # 输出:42

s = "123"
print(int(s))  # 输出:123

lst = [1, 2, 3, 4, 5]
print(int(lst))  # 引发 TypeError
Python

运行结果:

42
123
Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

3.5. float()

float() 函数将指定的对象转换为浮点数。如果对象是整数或字符串,它将被转换为浮点数;如果对象是其他类型,将引发 TypeError。示例代码如下:

x = 42
print(float(x))  # 输出:42.0

s = "3.14"
print(float(s))  # 输出:3.14

lst = [1, 2, 3, 4, 5]
print(float(lst))  # 引发 TypeError
Python

运行结果:

42.0
3.14
Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
TypeError: float() argument must be a string or a number, not 'list'

3.6. str()

str() 函数将指定的对象转换为字符串。示例代码如下:

x = 42
print(str(x))  # 输出:"42"

lst = [1, 2, 3, 4, 5]
print(str(lst))  # 输出:"[1, 2, 3, 4, 5]"

d = {"name": "Alice", "age": 20}
print(str(d))  # 输出: "{'name': 'Alice', 'age': 20}"
Python

运行结果:

42
[1, 2, 3, 4, 5]
{'name': 'Alice', 'age': 20}

3.7. list()

list() 函数用于将可迭代对象转换为列表。示例代码如下:

s = "Hello"
lst = list(s)
print(lst)  # 输出:['H', 'e', 'l', 'l', 'o']

t = (1, 2, 3, 4, 5)
lst = list(t)
print(lst)  # 输出:[1, 2, 3, 4, 5]
Python

运行结果:

['H', 'e', 'l', 'l', 'o']
[1, 2, 3, 4, 5]

3.8. dict()

dict() 函数用于创建字典或将可迭代对象转换为字典。示例代码如下:

d = dict()  # 创建一个空字典
print(d)  # 输出:{}

lst = [("name", "Alice"), ("age", 20)]
d = dict(lst)
print(d)  # 输出:{'name': 'Alice', 'age': 20}
Python

运行结果:

{}
{'name': 'Alice', 'age': 20}

3.9. set()

set() 函数用于创建集合或将可迭代对象转换为集合。示例代码如下:

s = set()  # 创建一个空集合
print(s)  # 输出:set()

lst = [1, 2, 2, 3, 3, 3, 4, 5]
s = set(lst)
print(s)  # 输出:{1, 2, 3, 4, 5}
Python

运行结果:

set()
{1, 2, 3, 4, 5}

3.10. range()

range() 函数用于创建一个指定范围的整数序列。示例代码如下:

r = range(5)
print(list(r))  # 输出:[0, 1, 2, 3, 4]

r = range(1, 6)
print(list(r))  # 输出:[1, 2, 3, 4, 5]

r = range(1, 10, 2)
print(list(r))  # 输出:[1, 3, 5, 7, 9]
Python

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册