Python常用内置函数大全,附带使用方法和示例
Python是一种强大且易于学习的编程语言,具有许多内置函数。这些函数是Python语言中的基本组成部分,可用于执行各种任务,例如数学计算、字符串处理、文件操作等。在本篇文章中,我将介绍Python的常用内置函数,并提供使用方法和示例,希望对学习Python的初学者或者Python程序员有所帮助。
一、数学函数
1. abs(x)
作用:返回x的绝对值
使用方法:abs(x)
示例:
print(abs(-5)) # 5
2. divmod(x, y)
作用:返回x除以y的商和余数
使用方法:divmod(x, y)
示例:
print(divmod(10, 3)) # (3, 1)
3. max(iterable)
作用:返回可迭代对象中的最大值
使用方法:max(iterable)
示例:
print(max([1, 2, 4, 3])) # 4
4. min(iterable)
作用:返回可迭代对象中的最小值
使用方法:min(iterable)
示例:
print(min([1, 2, 4, 3])) # 1
5. pow(x, y)
作用:返回x的y次方
使用方法:pow(x, y)
示例:
print(pow(2, 3)) # 8
6. round(x, n)
作用:返回x的四舍五入值,n表示小数点后保留的位数
使用方法:round(x, n)
示例:
print(round(3.1415926, 2)) # 3.14
二、字符串函数
1. len(s)
作用:返回字符串或序列s的长度
使用方法:len(s)
示例:
print(len("hello,world!")) # 12
2. str(object)
作用:将对象转换为字符串
使用方法:str(object)
示例:
print(str(123)) # "123"
3. upper(s)
作用:将字符串s转换为大写
使用方法:upper(s)
示例:
print("hello,world!".upper()) # "HELLO,WORLD!"
4. lower(s)
作用:将字符串s转换为小写
使用方法:lower(s)
示例:
print("HELLO,WORLD".lower()) # "hello,world"
5. capitalize(s)
作用:将字符串s的 个字母大写,其他字母小写
使用方法:capitalize(s)
示例:
print("hello,world!".capitalize()) # "Hello,world!"
6. join(iterable)
作用:将可迭代对象中的元素用字符串连接起来
使用方法:join(iterable)
示例:
print(",".join(["apple", "mango", "banana"])) # "apple,mango,banana"
7. replace(old, new)
作用:将字符串中的old替换为new
使用方法:replace(old, new)
示例:
print("hello,world!".replace("world", "python")) # "hello,python!"
8. split(sep)
作用:将字符串按照sep分隔成列表
使用方法:split(sep)
示例:
print("apple,mango,banana".split(",")) # ["apple", "mango", "banana"]
三、文件操作函数
1. open(file, mode)
作用:打开文件,mode可以指定文件打开方式
使用方法:open(file, mode)
示例:
f = open("test.txt", "w")
f.write("hello,world!")
f.close()
2. read(size)
作用:从文件中读取size个字符或者字节
使用方法:read(size)
示例:
f = open("test.txt", "r")
print(f.read(5)) # "hello"
f.close()
3. readline()
作用:从文件中读取一行数据
使用方法:readline()
示例:
f = open("test.txt", "r")
print(f.readline()) # "hello,world!
"
f.close()
4. write(str)
作用:将字符串写入文件中
使用方法:write(str)
示例:
f = open("test.txt", "w")
f.write("hello,world!")
f.close()
四、列表函数
1. append(obj)
作用:在列表末尾添加元素
使用方法:append(obj)
示例:
a = [1, 2, 3]
a.append(4)
print(a) # [1, 2, 3, 4]
2. insert(index, obj)
作用:在指定位置插入元素
使用方法:insert(index, obj)
示例:
a = [1, 2, 3]
a.insert(1, 4)
print(a) # [1, 4, 2, 3]
3. pop(index)
作用:移除指定位置的元素,并返回该元素
使用方法:pop(index)
示例:
a = [1, 2, 3]
print(a.pop(1)) # 2
print(a) # [1, 3]
4. remove(obj)
作用:移除列表中指定的元素
使用方法:remove(obj)
示例:
a = [1, 2, 3]
a.remove(2)
print(a) # [1, 3]
5. sort()
作用:对列表进行排序
使用方法:sort()
示例:
a = [1, 3, 2]
a.sort()
print(a) # [1, 2, 3]
六、字典函数
1. keys()
作用:返回字典的键值列表
使用方法:keys()
示例:
d = {"a": 1, "b": 2, "c": 3}
print(d.keys()) # ["a", "b", "c"]
2. values()
作用:返回字典的值列表
使用方法:values()
示例:
d = {"a": 1, "b": 2, "c": 3}
print(d.values()) # [1, 2, 3]
3. items()
作用:返回字典的键值对列表
使用方法:items()
示例:
d = {"a": 1, "b": 2, "c": 3}
print(d.items()) # [("a", 1), ("b", 2), ("c", 3)]
4. get(key, default)
作用:返回字典中指定key的值,如果key不存在,返回default
使用方法:get(key, default)
示例:
d = {"a": 1, "b": 2, "c": 3}
print(d.get("a", 0)) # 1
print(d.get("d", 0)) # 0
五、其他函数
1. range(stop)
作用:生成一个不可变序列,包含0到stop-1之间的整数
使用方法:range(stop)
示例:
print(range(5)) # range(0, 5)
2. len(s)
作用:返回字符串或序列s的长度
使用方法:len(s)
示例:
print(len("hello,world!")) # 12
3. type(object)
作用:返回对象的类型
使用方法:type(object)
示例:
print(type("hello")) # <class 'str'>
print(type(123)) # <class 'int'>
4. id(object)
作用:返回对象的 标识符
使用方法:id(object)
示例:
a = 1
b = 1
print(id(a)) # 140711206574768
print(id(b)) # 140711206574768
以上是Python常用内置函数的一些简单介绍,每个函数都提供了可操作性极高的实现方式。对于初学者,应当多加注意函数的使用,以加深理解,让自己能够熟练运用这些内置函数,使程序更加的规范和实用。
