欢迎访问宙启技术站
智能推送

Python内置函数及推荐列表

发布时间:2023-06-10 19:54:39

Python内置函数是指Python语言已经提供的函数,在运行程序时不需要再定义,就可以直接使用的函数。这些函数可以减少程序员的工作量,使代码更易读、更易维护。下面是Python内置函数的推荐列表。

1. print()

print()函数可以将用户要输出的信息打印到屏幕上,是Python编程中最常用的函数之一。例如:

print("Hello World")

输出结果:

Hello World

2. type()

type()函数可以返回一个变量的数据类型。例如:

x = 10

print(type(x))

输出结果:

<class 'int'>

3. len()

len()函数可以返回一个序列的长度。例如:

s = "abcde"

print(len(s))

输出结果:

5

4. str()

str()函数可以将其他数据类型转换为字符串。例如:

x = 123

s = str(x)

print(type(s))

输出结果:

<class 'str'>

5. int()

int()函数可以将其他数据类型转换为整数。例如:

s = "123"

x = int(s)

print(type(x))

输出结果:

<class 'int'>

6. float()

float()函数可以将其他数据类型转换为浮点数。例如:

s = "3.14"

x = float(s)

print(type(x))

输出结果:

<class 'float'>

7. list()

list()函数可以将其他数据类型转换为列表。例如:

s = "abcde"

lst = list(s)

print(type(lst))

输出结果:

<class 'list'>

8. dict()

dict()函数可以将其他数据类型转换为字典。例如:

lst = [('a', 1), ('b', 2), ('c', 3)]

dct = dict(lst)

print(type(dct))

输出结果:

<class 'dict'>

9. range()

range()函数可以生成一个数列。例如:

r = range(1, 10)

print(list(r))

输出结果:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

10. sorted()

sorted()函数可以对列表进行排序。例如:

lst = [3, 1, 4, 2, 5]

lst_sorted = sorted(lst)

print(lst_sorted)

输出结果:

[1, 2, 3, 4, 5]

11. sum()

sum()函数可以对序列进行求和。例如:

lst = [1, 2, 3, 4, 5]

s = sum(lst)

print(s)

输出结果:

15

12. max()

max()函数可以得到序列中的最大值。例如:

lst = [1, 2, 3, 4, 5]

m = max(lst)

print(m)

输出结果:

5

13. min()

min()函数可以得到序列中的最小值。例如:

lst = [1, 2, 3, 4, 5]

m = min(lst)

print(m)

输出结果:

1

14. abs()

abs()函数可以得到一个数的绝对值。例如:

x = -10

a = abs(x)

print(a)

输出结果:

10

15. pow()

pow()函数可以得到一个数的幂。例如:

x = 2

p = pow(x, 3)

print(p)

输出结果:

8

以上就是Python内置函数的推荐列表。当然,Python还提供了许多其他的内置函数,大家可以根据自己的需要来学习和使用。