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

Python常用函数一览表,让你的编写更高效

发布时间:2023-06-03 06:57:36

Python是一种高级编程语言,已经成为了数据科学、机器学习、Web开发等领域的常用语言。Python拥有丰富的内置函数和标准库,可以实现各种计算、数据处理、文件操作等功能,使得编写程序更加高效。在本篇文章中,我们就来介绍Python常用函数一览表,让大家在编写Python程序时更加从容。

1. print()函数

print()函数是Python中最基本的函数之一,用于在控制台输出内容。可以输出字符串、数字、列表、字典等各种类型的数据。

例如:

print('Hello World!')
print(100)
print([1,2,3])
print({'name':'Tom', 'age':20})

2. len()函数

len()函数可以返回字符串、列表、元组、字典等容器中元素的数量。

例如:

s = 'Hello World!'
print(len(s))

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

tpl = (1, 2, 3)
print(len(tpl))

dic = {'name':'Tom', 'age':20}
print(len(dic))

3. input()函数

input()函数用于在控制台中获取用户输入的数据,返回的是一个字符串类型的变量。

例如:

name = input('请输入您的姓名:')
print('您的姓名是:', name)

4. range()函数

range()函数用于生成一个整数序列,通常与for循环结合使用。

例如:

for i in range(1, 10, 2):
    print(i)

5. type()函数

type()函数用于返回变量的类型。

例如:

s = 'Hello World!'
print(type(s))

lst = [1, 2, 3, 4, 5]
print(type(lst))

tpl = (1, 2, 3)
print(type(tpl))

dic = {'name':'Tom', 'age':20}
print(type(dic))

6. int()函数

int()函数用于将一个字符型变量转换为整型变量。

例如:

s = '100'
n = int(s)
print(type(n))

7. str()函数

str()函数用于将一个变量转换为字符串类型。

例如:

n = 100
s = str(n)
print(type(s))

8. float()函数

float()函数用于将一个字符型变量转换为浮点型变量。

例如:

s = '3.14'
f = float(s)
print(type(f))

9. list()函数

list()函数用于将一个序列或者元组转换为列表。

例如:

tpl = (1, 2, 3)
lst = list(tpl)
print(lst)

10. dict()函数

dict()函数用于将一个元组序列转换为字典。

例如:

lst = [('name', 'Tom'), ('age', 20)]
dic = dict(lst)
print(dic)

11. max()函数

max()函数用于返回列表或元组中的最大值。

例如:

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

12. min()函数

min()函数用于返回列表或元组中的最小值。

例如:

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

13. sum()函数

sum()函数用于返回列表或元组中的所有元素的和。

例如:

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

14. sorted()函数

sorted()函数用于对列表进行排序。

例如:

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

15. zip()函数

zip()函数用于将多个列表或元组打包成一个新的列表或元组。

例如:

lst1 = [1, 2, 3]
lst2 = ['a', 'b', 'c']
zipped_lst = zip(lst1, lst2)
print(list(zipped_lst))

16. join()函数

join()函数用于将一个序列中的元素以指定的字符连接起来。

例如:

lst = ['a', 'b', 'c']
s = '-'.join(lst)
print(s)

17. split()函数

split()函数用于按照指定的分隔符将字符串分割成多个字符串。

例如:

s = 'a,b,c'
lst = s.split(',')
print(lst)

18. append()方法

append()方法用于将一个元素追加到列表的末尾。

例如:

lst = [1, 2, 3]
lst.append(4)
print(lst)

19. insert()方法

insert()方法用于将一个元素插入到列表的指定位置。

例如:

lst = [1, 2, 3]
lst.insert(1, 4)
print(lst)

20. remove()方法

remove()方法用于删除列表中的指定元素。

例如:

lst = [1, 2, 3, 4]
lst.remove(2)
print(lst)

21. pop()方法

pop()方法用于删除列表中指定位置的元素。

例如:

lst = [1, 2, 3, 4]
lst.pop(2)
print(lst)

22. clear()方法

clear()方法用于清空列表。

例如:

lst = [1, 2, 3, 4]
lst.clear()
print(lst)

23. copy()方法

copy()方法用于复制列表。

例如:

lst1 = [1, 2, 3]
lst2 = lst1.copy()
print(lst2)

24. update()方法

update()方法用于将一个字典中的元素更新到另一个字典中。

例如:

dic1 = {'name':'Tom', 'age':20}
dic2 = {'name':'Jerry', 'sex':'male'}
dic1.update(dic2)
print(dic1)

25. keys()方法

keys()方法用于返回字典中所有的键。

例如:

dic = {'name':'Tom', 'age':20}
print(dic.keys())

26. values()方法

values()方法用于返回字典中所有的值。

例如:

dic = {'name':'Tom', 'age':20}
print(dic.values())

27. items()方法

items()方法用于返回字典中所有的键值对。

例如:

dic = {'name':'Tom', 'age':20}
print(dic.items())

28. get()方法

get()方法用于获取字典中指定键的值。

例如:

dic = {'name':'Tom', 'age':20}
print(dic.get('name'))

29. setdefault()方法

setdefault()方法用于设置字典中指定键的默认值。

例如:

dic = {'name':'Tom', 'age':20}
print(dic.setdefault('sex', 'male'))
print(dic)

30. strip()方法

strip()方法用于去掉字符串两端的空白字符。

例如:

s = '  Hello World!  '
print(s.strip())

31. replace()方法

replace()方法用于替换字符串中的指定字符。

例如:

s = 'Hello World!'
print(s.replace('World', 'Python'))

32. title()方法

title()方法用于将字符串中每个单词的首字母大写。

例如:

s = 'hello world'
print(s.title())

33. isdigit()方法

isdigit()方法用于判断字符串是否只包含数字字符。

例如:

s = '12345'
print(s.isdigit())

34. isalpha()方法

isalpha()方法用于判断字符串是否只包含字母字符。

例如:

s = 'hello'
print(s.isalpha())

35. join()方法

join()方法用于将一个序列中的元素以指定的字符连接起来。

例如:

lst = ['a', 'b', 'c']
s = '-'.join(lst)
print(s)

36. format()方法

format()方法用于格式化字符串。

例如:

`

name = 'Tom'

age = 20

print('My name