Python 内置函数

内置函数

Python中有很多内置函数,可以直接使用,下面只介绍比较常用的内置函数,如果想了解更多,可以访问以下链接:

https://www.runoob.com/python3/python3-built-in-functions.html

1.abs()   函数返回数字的绝对值    -----传入的参数必须为数字(可以是整数、浮点数和负数),其他类型会报错

abs(-12)
print(abs(-12))
输出结果: 12

2.set() 集合的转化函数     ------实际工作当中,使用更多的功能是去重   (返回结果为新的集合对象)

list=[1,2,3,3,4,6,8,7,6,10,12,3,6,6,16]
print(set(list))

输出结果:
{1, 2, 3, 4, 6, 7, 8, 10, 12, 16}

3.dict()   用于创建一个字典  

dict={'姓名':'小明',12:1,True:False,(2,3):12}    #列表和字典不能做key值,因为是可变的
print(dict)

输出结果:
[22, 33, '你好', {12: 1, 'name': '小明'}, (22, 5)]
{'姓名': '小明', 12: 1, True: False, (2, 3): 12}

4.list()  用于创建一个列表   还可以将元组或者是字符串转换为列表

#将元组转换为列表
aTuple = (12,'唯品会',True,False,(2,3),[4,5],{'name':"小花"})
list1 = list(aTuple)
print ("列表元素1: ", list1)

#将字符串转换为列表
str1='他曾说的话有没有兑现,他现在又站在谁的对面'
list2=list(str1)
print('列表元素2:',list2)

输出结果:
列表元素1:  [12, '唯品会', True, False, (2, 3), [4, 5], {'name': '小花'}]
列表元素2: ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

5.max()  返回给定参数的最大值(参数可以为序列)

#输出列表中的最大值
list3=[1,2,4,6,8,90,4,65]
print(max(list3))
print(max(34,56,8))   #找出传入参数中的最大值

输出结果:
90
56

6.min()  返回给定参数的最小值(参数可以为序列)

#找出列表中的最小值
list3=[1,2,4,6,8,90,4,65]
print(min(list3))
print(min(34,56,8))

输出结果:
1
8

7.sorted()  对所有可迭代的对象进行排序操作

  • sort和sorted区别:
  1. sort是应用在list上的方法,sorted可以对所有可迭代的对象进行排序操作
  2. list的sort放大返回的是对已经存在的列表进行修改(也就是修改原有列表进行排序)
  3. 内建函数sorted方法返回的是一个新的list,而不是在原来的基础上进行修改
  4. sort和sorted使用方法不同:list.sort()    sorted(list)   ------面试容易被问到
#sort 和 sorted排序
new_list=[22,3,45,66,8,1,23,88]
print(sorted(new_list))
a=new_list.sort()
print(new_list)

输出结果:
[1, 3, 8, 22, 23, 45, 66, 88]  #sorted不改变原有的new_list列表返回一个新的列表
[22, 3, 45, 66, 8, 1, 23, 88]
[1, 3, 8, 22, 23, 45, 66, 88] #sort直接对原有的列表进行修改

8.enumerate()枚举   可以同时获取  索引,和对应的值,元组形式(如果传入两个值进行遍历,就是元组的解包)

#枚举,可以同时获取、索引,和对应的值,元组形式
aTuple = [12,'唯品会',True,False,(2,3),[4,5],{'name':"小花"}]
for i in enumerate(aTuple):
    print(i)

输出结果:
(0, 12)
(1, '唯品会')
(2, True)
(3, False)
(4, (2, 3))
(5, [4, 5])
(6, {'name': '小花'})
#如果是两个值,就是元组的解包
aTuple = [12,'唯品会',True,False,(2,3),[4,5],{'name':"小花"}]
for i,v in enumerate(aTuple):
    print(i,v)

输出结果:
0 12
1 唯品会
2 True
3 False
4 (2, 3)
5 [4, 5]
6 {'name': '小花'}

9.input()  控制台输入函数   ------输出结果为字符串类型

name=input('请输入你想说的话:')
print(name)

输出结果:
请输入你想说的话:nihao,zhongguo
nihao,zhongguo

10.bool() 布尔值类型转化

将给定的参数转换为布尔类型,如果没有参数,返回为False(零和不传参返回的结果均为:False)

print(bool())
print(bool(0))
print(bool([]))
print(bool({}))
print(bool('name'))
print(bool(1))
print(bool([1,2,3]))
print(bool({"name":"小明"}))

输出结果:
False
False
False
False
True
True
True
True

11.len()  返回对象(字符、列表、元组等)长度或者项目个数

#len()方法使用
str1='i love china'
list4=[1,4,5,66,7,9]
dict1={'name':'小花','work':"程序员"}
print(len(str1))
print(len(list4))
print(len(dict1))

输出结果:
12
6
2

12.format() 字符串格式化(使用方法如下)

#format() 字符串格式化
print('{}{}{}'.format('陕西','是古都','!'))

输出结果:
陕西是古都!

13.super() 该函数用于调用父类(超类)的一个方法

super用来解决多重继承的问题,直接用类名调用父类方法,在使用单个继承的时候没问题,但是如果使用多继承,会涉及到查找顺序、重复调用的问题

class FooParent(object):
    def __init__(self):
        self.parent = 'I\'m the parent.'
        print ('Parent')
    
    def bar(self,message):
        print ("%s from Parent" % message)
 
class FooChild(FooParent):
    def __init__(self):
        # super(FooChild,self) 首先找到 FooChild 的父类(就是类 FooParent),然后把类 FooChild 的对象转换为类 FooParent 的对象
        super(FooChild,self).__init__()    
        print ('Child')
        
    def bar(self,message):
        super(FooChild, self).bar(message)
        print ('Child bar fuction')
        print (self.parent)
 
if __name__ == '__main__':
    fooChild = FooChild()
    fooChild.bar('HelloWorld')

输出结果:
Parent
Child
HelloWorld from Parent
Child bar fuction
I'm the parent.
#该例子摘抄于https://www.runoob.com网站

14.open() 打开文件    用于打开文件处理

15.close() 关闭文件

 

posted @ 2020-11-12 20:56  紫陌红尘雪落无声  阅读(250)  评论(0编辑  收藏  举报