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

Python内置函数介绍:常用函数示例

发布时间:2023-06-17 02:59:32

Python是一种功能强大、易于学习的编程语言。Python拥有大量的内置函数,这些函数可以完成很多基本的任务,也可以用于高级编程。

本文将介绍Python的常用内置函数,以及相应的示例。

1. print()

print()函数可以将字符串、数字以及变量等输出到屏幕上。

示例:

print("Hello, World!")

输出结果为:

Hello, World!

2. len()

len()函数可以返回一个对象的长度或元素个数。

示例:

list1 = [1, 2, 3, 4, 5]
print(len(list1))

输出结果为:

5

3. input()

input()函数可以接收用户输入的数据,返回值为字符串类型。

示例:

name = input("What's your name?")
print("Hello, " + name + "!")

输出结果为:

What's your name?Tom
Hello, Tom!

4. str()

str()函数可以将其他类型的对象转换为字符串类型。

示例:

print(str(123))

输出结果为:

123

5. int()

int()函数可以将一个字符串或浮点数转换为整数。

示例:

print(int(3.14))

输出结果为:

3

6. float()

float()函数可以将一个字符串或整数转换为浮点数。

示例:

print(float(3))

输出结果为:

3.0

7. type()

type()函数可以返回一个对象的类型。

示例:

x = 5
print(type(x))

输出结果为:

<class 'int'>

8. round()

round()函数可以将一个浮点数四舍五入为指定的小数位数。

示例:

print(round(3.14159, 2))

输出结果为:

3.14

9. max()

max()函数可以返回一个可迭代对象中的最大值。

示例:

list1 = [1, 2, 3, 4, 5]
print(max(list1))

输出结果为:

5

10. min()

min()函数可以返回一个可迭代对象中的最小值。

示例:

list1 = [1, 2, 3, 4, 5]
print(min(list1))

输出结果为:

1

11. sum()

sum()函数可以计算一个可迭代对象中的所有元素的和。

示例:

list1 = [1, 2, 3, 4, 5]
print(sum(list1))

输出结果为:

15

12. abs()

abs()函数可以返回一个数的绝对值。

示例:

print(abs(-5))

输出结果为:

5

13. pow()

pow()函数可以返回一个数的指定次幂。

示例:

print(pow(2, 3))

输出结果为:

8

14. range()

range()函数可以生成一个整数序列。

示例:

for i in range(5):
    print(i)

输出结果为:

0
1
2
3
4

15. reversed()

reversed()函数可以返回一个可迭代对象的反向迭代器。

示例:

list1 = [1, 2, 3, 4]
for i in reversed(list1):
    print(i)

输出结果为:

4
3
2
1

16. enumerate()

enumerate()函数可以将一个可迭代对象转换为一个枚举对象,同时返回索引和元素。

示例:

list1 = ['apple', 'banana', 'orange']
for index, item in enumerate(list1):
    print(index, item)

输出结果为:

0 apple
1 banana
2 orange

17. sorted()

sorted()函数可以对一个可迭代对象进行排序。

示例:

list1 = [3, 2, 4, 1, 5]
print(sorted(list1))

输出结果为:

[1, 2, 3, 4, 5]

18. zip()

zip()函数可以将多个可迭代对象打包成一个元组序列。

示例:

list1 = ['apple', 'banana', 'orange']
list2 = ['red', 'yellow', 'orange']
list3 = [1, 2, 3]
for item in zip(list1, list2, list3):
    print(item)

输出结果为:

('apple', 'red', 1)
('banana', 'yellow', 2)
('orange', 'orange', 3)

19. any()

any()函数可以返回一个可迭代对象中的任意一个元素是否为True。

示例:

list1 = [0, 1, 2, 3, 4, 5]
print(any(list1))

输出结果为:

True

20. all()

all()函数可以返回一个可迭代对象中的所有元素是否为True。

示例:

list1 = [0, 1, 2, 3, 4, 5]
print(all(list1))

输出结果为:

False

21. filter()

filter()函数可以对一个可迭代对象进行过滤,返回满足条件的元素组成的迭代器。

示例:

list1 = [1, 2, 3, 4, 5]
result = filter(lambda x: x % 2 == 0, list1)
for item in result:
    print(item)

输出结果为:

2
4

22. map()

map()函数可以对一个可迭代对象中的所有元素执行指定的函数,返回新的迭代器。

示例:

list1 = [1, 2, 3, 4, 5]
result = map(lambda x: x * x, list1)
for item in result:
    print(item)

输出结果为:

1
4
9
16
25

23. reduce()

reduce()函数可以对一个可迭代对象中的所有元素执行指定的函数,返回计算结果。

示例:

from functools import reduce
list1 = [1, 2, 3, 4, 5]
result = reduce(lambda x, y: x + y, list1)
print(result)

输出结果为:

15

24. dir()

dir()函数可以返回一个对象的所有属性和方法。

示例:

str1 = "Hello, World!"
print(dir(str1))

输出结果为:

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

25. chr()

chr()函数可以返回指定Unicode编码对应的字符。

示例:

print(chr(97))

输出结果为:

a

26. ord()

ord()函数可以返回指定字符对应的Unicode编码。

示例:

print(ord('a'))

输出结果为:

97

27.