Fundamentals 6 min read

Common Python3 Built-in Functions Overview

This article systematically introduces common Python 3 built-in functions—including mathematical operations, type conversions, and utility functions—providing usage examples and encouraging readers to practice each function to strengthen their programming fundamentals.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Common Python3 Built-in Functions Overview

Earlier we introduced some built‑in functions; now this article provides a systematic overview of common Python 3 built‑in functions, encouraging readers to practice.

Python3常用内置函数
1、数学相关
abs(a) : 求取绝对值。abs(-1)
max(list) : 求取list最大值。max([1,2,3])
min(list) : 求取list最小值。min([1,2,3])
sum(list) : 求取list元素的和。
sum([1,2,3])
>>> 6

sorted(list) : 排序,返回排序后的list。
len(list) : list长度,len([1,2,3])
divmod(a,b): 获取商和余数。
divmod(5,2)
>>> (2,1)

pow(a,b) : 获取乘方数。
pow(2,3)
>>> 8

round(a,b) : 获取指定位数的小数。
a代表浮点数,b代表要保留的位数。
round(3.1415926,2) >>> 3.14

range(a[,b]) : 生成一个a到b的数组,左闭右开。
range(1,10)
>>> [1,2,3,4,5,6,7,8,9]

2、类型转换
int(str) : 转换为int型。
int('1')
>>> 1

float(int/str) : 将int型或字符型转换为浮点型。
float('1')
>>> 1.0

str(int) : 转换为字符型。
str(1)
>>> '1'

bool(int) : 转换为布尔类型。
str(0)
>>> False 
str(None)
>>> False

bytes(str,code) : 接收一个字符串,与所要编码的格式,
返回一个字节流类型。
bytes('abc', 'utf-8')
>>> b'abc' 
bytes(u'爬虫', 'utf-8')
>>> b'\xe7\x88\xac\xe8\x99\xab'

list(iterable) : 转换为list。
list((1,2,3))
>>> [1,2,3]

iter(iterable): 返回一个可迭代的对象。
iter([1,2,3])
>>> <list_iterator object at 0x...>

dict(iterable) : 转换为dict。
 dict([('a',1), ('b',2), ('c',3)])
>>> {'a':1, 'b':2, 'c':3}

enumerate(iterable) : 返回一个枚举对象。
tuple(iterable) : 转换为tuple。
tuple([1,2,3])
>>> (1,2,3)
set(iterable) : 转换为set。
set([1,4,2,4,3,5])
>>> {1,2,3,4,5}
set({1:'a',2:'b',3:'c'})
>>> {1,2,3}

hex(int) : 转换为16进制。
hex(1024)
>>> '0x400'

oct(int) : 转换为8进制。
oct(1024)
>>> '0o2000'

bin(int) : 转换为2进制。
bin(1024)
>>> '0b10000000000'

chr(int) : 转换数字为相应ASCI码字符。
chr(65)
>>> 'A'

ord(str) : 转换ASCI字符为相应的数字。
ord('A')
>>> 65

3、相关操作
eval() : 执行一个表达式,或字符串作为运算。
eval('1+1')
>>> 2

exec() : 执行python语句。
exec('print("Python")')
>>> Python

filter(func, iterable) : 通过判断函数fun,
筛选符合条件的元素。
filter(lambda x: x>3, [1,2,3,4,5,6])
>>> <filter object at ...>

map(func, *iterable) : 将func用于每个iterable对象。
map(lambda a,b: a+b, [1,2,3,4], [5,6,7])
>>> [6,8,10]

zip(*iterable) : 将iterable分组合并。返回一个zip对象。
list(zip([1,2,3],[4,5,6]))
>>> [(1,4), (2,5), (3,6)]

type():返回一个对象的类型。
id(): 返回一个对象的唯一标识值。
hash(object):返回一个对象的hash值,具有相同值的object具有相同的hash值。
hash('python')
>>> 7070808359261009780

help():调用系统内置的帮助系统。
isinstance():判断一个对象是否为该类的一个实例。
issubclass():判断一个类是否为另一个类的子类。
globals() : 返回当前全局变量的字典。
next(iterator[, default]) : 接收一个迭代器,返回迭代器中的数值,如果设置了default,则当迭代器中的元素遍历后,输出default内容。
reversed(sequence) : 生成一个反转序列的迭代器。
reversed('abc')
>>> ['c','b','a']

Long‑press the QR code below to get more automation learning resources.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

programmingfundamentalsbuilt-in-functions
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.