python常用内置函数大全,python内置函数有哪些

大家好,本文将围绕python有哪些常用的内置函数展开说明,python常用内置函数大全是一个很多人都想弄明白的事情,想搞清楚python内置函数有哪些需要先了解以下几个事情。

一、数学函数

1.abs()函数:取绝对值

print(abs(-10))
>>>10

2.divmod()函数:同时取商和余数 🌟🌟

print(divmod(7,2))
>>>(3, 1)

3.sum()函数:求和计算

li=[1,2,3]
print(sum(li))
>>>6

4.round()函数:四舍五入

print(round(5.1))
print(round(5.5))
>>>5
>>>6

5.pow()函数:计算任意N次方值

print(pow(2,3)) #两种都可以
print(2**3)
>>>8
>>>8

6.min()函数:取最小值

li=[3,2,15,6,1]
print(min(li))
>>>1

7.max()函数:取最大值

li=[3,2,15,6,1]
print(max(li))
>>>15
二、数据转换函数

1.hex()函数:十进制转换为十六进制

print(hex(100))
print(hex(100)[2:])
>>>'0x64'
>>>'64'

2.oct()函数:十进制转换为八进制

print(oct(100))
print(oct(100)[2:])
>>>'0o144'
>>>'144'

3.bin()函数:十进制转换为二进制

print(bin(100))
print(bin(100)[2:])
>>>0b1100100
>>>1100100

 4.bool()函数:将指定的参数转换为布尔类型

print(bool(1))
print(bool(0))

#如果只有1和0的情况下可以这样转换
m=1
n=0
print(int(not m))
print(int(not n))
>>>True
>>>False

>>>0
>>>1

 5.ord()函数:获取单个字符的ASCII数值

print(ord('A'))
print(ord('a'))
>>>65
>>>97

 6.chr()函数:转换一个整数并返回所对应的字符

print(chr(65))
print(chr(97))
>>>A
>>>a

 7.float()函数:转换为浮点型

print(float(10))
>>>10.0

 8.list()函数:将可迭代对象转换为列表

print(list(range(1,10)))
>>>[1, 2, 3, 4, 5, 6, 7, 8, 9]
三、对象创建函数

 1.open()函数:打开文件并返回文件对象

f1 = open('E:\Hello.txt','r')
print(f1.readline())
>>>Hello Python

 2.range()函数:创建指定范围内的连续整数,从0开始,取不到5

for i in range(5):
	print(i)
0
1
2
3
4

 3.enumerate()函数:用于返回下标和数值,常用于需要排序,但要保留原来下标的操作

li=[2,3,6,4,1]
for i,num in enumerate(li):
    print(i,num)
>>>
0 2
1 3
2 6
3 4
4 1

 4.set()函数:创建一个无序无重复元素集合

li=[1,1,1,2,3,3,4,5,5,6,6]
print(set(li))
>>>{1, 2, 3, 4, 5, 6}
四、迭代器操作函数

 1.all()函数:判断指定序列中的所有元素是否都为True,则返回True,如果有一个False,则返回False。

a=[1,2,3]
b=[1,2,0]
print(all(a))
print(all(b))
>>>True
>>>False

 2.any()函数:判断指定序列中的所有元素是否都为False,如果有一个为True,则返回True

a=[0,0,0]
b=[1,0,0]
print(any(a))
print(any(b))
>>>False
>>>True

 3.sort()函数:对列表进行排序,默认的是升序,如果要倒序可以加(reverse=True)

a=[3,5,1,7]
a.sort() #升序
print(a)

a.sort(reverse=True) #降序
print(a)
>>>[1, 3, 5, 7]
>>>[7, 5, 3, 1]

 4.sorted()函数:与sort()使用方法类似,但sorted()可以对字符串,字典进行操作

a=[['a','b','B'],['c','e','A'],['c','f','D']]
print(sorted(a,key=lambda x:x[2])) #根据第三个元素进行排序

b={'Tom':20,'Mike':18,'anna':19}
#根据字典的键值对形式,按照第二个元素进行排序
print(sorted(b.items(),key=lambda x:x[1]))

c='fhjahfla'
print(sorted(c,reverse=True))#降序排序

>>>[['c', 'e', 'A'], ['a', 'b', 'B'], ['c', 'f', 'D']]
>>>[('Mike', 18), ('anna', 19), ('Tom', 20)]
>>>['l', 'j', 'h', 'h', 'f', 'f', 'a', 'a']

 5.len()函数:返回一个对象或一个项目的长度

print(len([1,2,3,4]))
>>>4

五、python中字符串操作

1.find()函数:用于检测某个子串是否在这个字符串中,如果在,则返回这个子串开始的位置下标(第一个),否则返回-1

语法:

字符串序列.find(子串,开始位置下标,结束位置下标)
mystr='hello world and itcast and itheima and Python'

print(mystr.find('and'))

print(mystr.find('and',15,len(mystr)-1))

print(mystr.find('ands'))
>>>12
>>>23
>>>-1

 2.index()函数:与find()函数类似,当子串不存在时报错

print(mystr.index('and'))
>>>12

 3.count()函数:用于返回某个子串在字符串中出现的次数

mystr='hello world and itcast and itheima and Python'
print(mystr.count('and')) # 在字符串中出现的次数

print(mystr.count('and',1,20)) # 在字符串(1,20)中出现的次数

print(mystr.count('ands')) #当不存在
>>>3
>>>1
>>>0

 4.rfind()函数:和find()功能相同,但查找方向为右侧开始python简单笑脸代码

 5.rindex()函数:和find()功能相同,但查找方向为右侧开始。

 6.replace()函数:用于字符串中的替换

 语法:

字符串序列.replace(旧子串,新子串,替换次数)
mystr='hello world and itcast and itheima and Python'
print(mystr.replace('and','he'))
>>>hello world he itcast he itheima he Python

 7.split()函数:按照指定字符分割字符串。(分割n次,返回n+1个值)

语法:

字符串序列.split(分割字符,num)
mystr='hello world and itcast and itheima and Python'

print(mystr.split('and')) #默认全分

print(mystr.split('and',2)) #分割两次

print(mystr.split(' '))

>>>['hello world ', ' itcast ', ' itheima ', ' Python']
>>>['hello world ', ' itcast ', ' itheima and Python']
>>>['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']

 8.join()函数:用一个字符或子串合并字符串,即是将多个字符串合并成一个新的字符串

语法:

字符或子串.join(多字符串组成的序列)
b=['A','B','C']
c=''.join(b)
print(c)

a=['AA','BB','CC']
q='...'.join(a)
print(q)
>>>'ABC'
>>>'AA...BB...CC'

9.capitalize()函数:将字符串首字母大写

mystr='hello world and itcast and itheima and Python'
mystr.capitalize()
>>>'Hello world and itcast and itheima and python'

10.title()函数:将字符串每个单词首字母大写

mystr='hello world and itcast and itheima and Python'
mystr.title()
>>>'Hello World And Itcast And Itheima And Python'

11.lower()函数:将字符串中大写转小写

mystr='hello world and itcast and itheima and Python'
print(mystr.lower())
>>>hello world and itcast and itheima and python

12.upper()函数:将字符串中小写转大写

mystr='hello world and itcast and itheima and Python'
print(mystr.upper())
>>>HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON

13.strip()函数:删除字符串两侧的空白字符

mystr='  hello world    '
print(mystr)
print(mystr.strip())
>>>'  hello world    '
>>>'hello world

14.lstrip()函数:删除字符串左侧的空白字符

15.rstrip()函数:删除字符串右侧的空白字符

16.ljust()函数:返回一个原字符串左对齐,并使用指定字符(默认空格)填充至对应长度的新字符串

语法:

字符串序列.ljust(长度,填充字符)
mystr='hello'
mystr.ljust(10,'.')
>>>'hello.....' #总共长度为10

17.rjust()函数:右填充

mystr='hello'
mystr.rjust(10,'.')
>>>'.....hello' #总共长度为10

18.center()函数:居中对齐

mystr.center(10,'.')
>>>'..hello...'

19.startswiith()函数:检查字符串是否以指定子串开头,是则返回True,否则返回False。如果设置开始和结束位置的下标,则在指定范围内检查。

语法:

mystr='hello world and itcast and itheima and Python'
print(mystr.startswith('hello'))
mystr='hello world and itcast and itheima and Python'
mystr.capitalize()
>>>True

20.endswith()函数:判断字符串是否以某个子串结尾

mystr='hello world and itcast and itheima and Python'
print(mystr.endswith('python'))

print(mystr.endswith('Python'))
>>>False #区分大小写
>>>True

21.isalpha()函数:判断是否全为字母

mystr1='hello'
mystr2='helo123'
print(mystr1.isalpha())
print(mystr2.isalpha())
>>>True
>>>False

22.isdigit()函数:判断是否全为数字

mystr1='aaa123'
mystr2='1321'
print(mystr1.isdigit())
print(mystr2.isdigit())
>>>False
>>>True

23.isalnum()函数:用于判断字符串是否由数字和字母组成,如果字符串至少有一个字符并且所有字符都是字母或者数字则返回True,否则返回False。

a1='aaa123'
a2='22121'
a3='2121_'
print(a1.isalnum())
print(a2.isalnum())
print(a3.isalnum())
>>>True
>>>True
>>>False

24.isspace()函数:判断字符串是否只包含空白

a1='21'
a2='    '
print(a1.isspace())
print(a2.isspace())
>>>False
>>>True
六、Python中math库的使用

1.导入math包

import math

2.ceil()函数:向上取整

print(math.ceil(3.1))
>>>4

3.floor()函数:向下取整

print(math.floor(3.1))
>>>3

4.pow()函数:乘方 开方

import math
math.pow(10,3)
math.pow(27,1/3)
>>>1000.0
>>>3.0
  • 16
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值