大爽Python入门公开课教案 点击查看教程总目录
1 概览
官网文档 DOC: Built-in Functions
The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.
Python 解释器内置了很多函数和类型,我们可以在任何时候使用它们。以下按字母表顺序列出它们。

上方截图展示的就是python的内置函数(图中共有69个)。
现在可以回顾下我们已经学了(基本认识)哪些内置函数。
罗列如下(共15个)
-
bool() -
dict() -
float() -
input() -
int() -
isinstance() -
len() -
list() -
open() -
print() -
range() -
round() -
str() -
tuple() -
type()
2 常用介绍
这里额外拓展介绍一些
常用的内置函数
enumerate
之前的遍历列表,有两种方法
for item in a_list: 遍历所有项for index in range(len(a_list)): 遍历所有项的索引
enumerate可以简单认为是这两者之和。
能同时遍历索引和值,
其常用语法为for index, item in enumerate(a_list)
使用示例
>>> for index, item in enumerate(seasons):
... print(index, item)
...
0 Spring
1 Summer
2 Fall
3 Winter
官方详细介绍如下(节选)
enumerate(iterable, start=0)
Return an enumerate object.
返回一个enumerate对象
The__next__()method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable.
返回值可遍历,每次遍历得到的项为一个元组
包含计数变量(从start开始,start默认值为0)和值(由传入的可迭代对象迭代出的)>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter'] >>> list(enumerate(seasons)) [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')] >>> list(enumerate(seasons, start=1)) [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
sorted
常用于对列表进行排序,
实际上也可以对iterable进行排序,
返回一个新的排好序的列表。
排序结果,默认是升序的,
可以通过设置关键字参数reverse为True(默认值为False),
来实现降序排序。
>>> arr = [5, 2, 3, 1, 4]
>>> sorted(arr)
[1, 2, 3, 4, 5]
>>> sorted(arr, reverse=True)
[5, 4, 3, 2, 1]
3 拓展了解
以下函数比较简单,
感兴趣可以自行了解。
本节课不做介绍
(未来如果用到了,到时候会介绍)
简单:
abs()min()max()pow()sum()
较难:
all()any()