Python基础day-11[内置函数(未完),递归,匿名函数]

时间:2022-10-08 19:16:43

内置函数:

abs() : 返回数字的绝对值。参数可以是整数或浮点数,如果参数是复数,则返回复数的模。

print(abs(0.2))
print(abs(1))
print(abs(-4))
print(abs(-0.2))
print(abs(3+4j))
执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
0.2
1
4
0.2
5.0

Process finished with exit code 0

all():如果 iterable 的所有元素都为真(或者如果可迭代为空),则返回 True

l = [1,2,3,4,5,6,7]
print(all(l))
l = []
print(all(l))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
True
True

Process finished with exit code 0

any():如果iterable的任何元素为真,则返回True。如果iterable为空,则返回False。

l = [1,2,3,4,5,6,7]
print(any(l))
l = []
print(any(l))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
True
False

Process finished with exit code 0

ascii():返回一个可打印的对象字符串方式表示,如果是非ascii字符就会输出\x,\u等字符来表示。

print(ascii('中文'))
print(ascii(10), ascii(9000000), ascii('b\10'), ascii('0x\001'))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
'\u4e2d\u6587'
10 9000000 'b\x08' '0x\x01'

Process finished with exit code 0

bin():将整数转换为二进制字符串。

print(bin(10))
print(bin(77))
print(bin(100))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
0b1010
0b1001101
0b1100100

Process finished with exit code 0

bool():返回布尔值,即True或False之一。

print(bool(0))
print(bool(None))
print(bool())

print(bool(1))
print(bool('abc'))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
False
False
False
True
True

Process finished with exit code 0

chr():输入unicode代码,返回对应的字符。

print(chr(25991))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
文

Process finished with exit code 0

ord():输入一个字符,返回其unicode代码。

print(ord(''))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
20013

Process finished with exit code 0

dict():定义一个新的字典。

d = dict({'name':'abc','age':123})
print(d)

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
{'name': 'abc', 'age': 123}

Process finished with exit code 0

divmod():以两个(非复数)数字作为参数,并在使用整数除法时返回由商和余数组成的一对数字。

print(divmod(73,23))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
(3, 4)

Process finished with exit code 0

enumerate():给列表,元组等可索引类型加上序号。

l=['a','b','c']

for i in enumerate(l):
    print(i)

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
(0, 'a')
(1, 'b')
(2, 'c')

Process finished with exit code 0

eval():从文件中读取字符串转换成命令执行。

with open(r'E:\Python\Exercise\user.txt','r',encoding='utf-8') as f:
    for i in f:
        d = eval(i)
        print(d,type(d))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
{'abc': '123', 'laochai': 'SB', 'xiaohe': 'bigSB'} <class 'dict'>

Process finished with exit code 0

float():将一个整数或者字符串类型的数字,变成浮点数返回。

x = float(1)
print(x,type(x))
i = float('123')
print(i,type(i))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
1.0 <class 'float'>
123.0 <class 'float'>

Process finished with exit code 0

format():格式化字符串

x = 'Name:{x},age:{y},sex:{z}' 
x = x.format(y=18,x='abc',z='male')
print(x)

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
Name:abc,age:18,sex:male

Process finished with exit code 0

frozenset():不可变的集合,没有add,remove方法

s = frozenset({1,2,3,4,5,6})
print(s)

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
frozenset({1, 2, 3, 4, 5, 6})

Process finished with exit code 0

globals():返回当前全局作用域。

print(globals())

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x03889510>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'E:/Python/DAY-11/tmp.py', '__cached__': None}

Process finished with exit code 0

hash():查看哈希值。

s1='hello123123'
s2='hello123123'

print(hash(s1))
print(hash(s2))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
-1538468614
-1538468614

Process finished with exit code 0

hex():转换十进制为十六进制。

print(hex(15))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
0xf

Process finished with exit code 0

id():查看数据的唯一标识。

print(id('abc'))
print(id('def'))
print(id('abc'))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
50389024
50435488
50389024

Process finished with exit code 0

input():等待用户输入并赋值给变量名。

username = input('Enter your username:')
print('Your name:',username)

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
Enter your username:test
Your name: test

Process finished with exit code 0

int():转换一个数字或者字符串类型的数字为整数类型。

i = int('123')
print(i,type(i))
x = int(1)
print(x,type(x))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
123 <class 'int'>
1 <class 'int'>

Process finished with exit code 0

isinstance():判断一个对象是不是,列表、元组等类型。

l = [1,2,3,4,5]
print(isinstance(l,list))
print(isinstance(l,tuple))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
True
False

Process finished with exit code 0

iter():转换可迭代对象为迭代器。

l = [1,2,3,4,5]
print(iter(l))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
<list_iterator object at 0x035F9550>

Process finished with exit code 0

len():返回对象的长度。参数可以是字符串,列表,元组,字典,集合

l = [1,2,3,4,5]
print(len(l))
print(len('abc'))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
5
3

Process finished with exit code 0

list():新建一个新的列表。

l = list([1,2,3,4,5,6,7])
print(l,type(l))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
[1, 2, 3, 4, 5, 6, 7] <class 'list'>

Process finished with exit code 0

locals():查看局部作用域。在全局作用域中使用局部作用域显示的还是全局作用域。

print(locals())
print(globals() is locals())

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x00C79510>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'E:/Python/DAY-11/tmp.py', '__cached__': None}
True

Process finished with exit code 0

map():传递一个function,和一个iterable,function必须处理iterable中的所有元素。

l=[1,2,3,4]
m=map(lambda x:x**2,l)
print(list(m))

names=['alex','wupeiqi','yuanhao']
print(list(map(lambda item:item+'_SB',names)))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
[1, 4, 9, 16]
['alex_SB', 'wupeiqi_SB', 'yuanhao_SB']

Process finished with exit code 0

max():两个值的最大值。

示例一:
salaries={
    'egon':3000,
    'alex':100000000,
    'wupeiqi':10000,
    'yuanhao':2000
}

print(max(salaries)) #默认只能读到字典的key,比较字符串是根绝a-z的顺序比较的,先比较第一位字母。注:z>a
print(max(salaries.values()))   #比较value

res=zip(salaries.values(),salaries.keys())
print(max(res)[-1])

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
yuanhao
100000000
alex

Process finished with exit code 0


示例二:
salaries={
    'egon':3000,
    'alex':100000000,
    'wupeiqi':10000,
    'yuanhao':2000
}
def func(x):
    return salaries[x]


print(max(salaries,key=lambda x:salaries[x]))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
alex

Process finished with exit code 0

min():同max一样用法,不过是返回时两者之间小的那个值。

next():从迭代器中读取一次值。

l = [1,2,3,4,5,6,7,8]
l = iter(l)
print(next(l))
print(next(l))
print(next(l))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
1
2
3

Process finished with exit code 0

oct():转换八进制。

print(oct(9))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
0o11

Process finished with exit code 0

open():打开一个文件进行文件操作的前提。注:open方式打开文件不会自动关闭,使用with open() as  可以自动关闭文件。

'r'    
打开阅读(默认)

'w'    
打开写入,首先截断文件

'x'    
打开以供独占创建,如果文件已存在则失败

'a'    
打开以写入,如果存在,则附加到文件的末尾

'b'    
二进制模式

't'    
文本模式(默认)

'+'    
打开磁盘文件进行更新(读写)

'U'    
universal newlines 模式(已弃用)

=====================================
f = open (r'E:\Python\Exercise\userinfo.txt','r',encoding='utf-8')
for i in f:
    print(i)
f.close()   #关闭文件

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
1,Alex Li,22,13651054608,IT,2013-04-01

2,Jack Wang,30,13304320533,HR,2015-05-03

3,Rain Liu,25,1383235322,Sales,2016-04-22

4,Mack Cao,40,1356145343,HR,2009-03-01

Process finished with exit code 0

pow(): 平方,取余。

print(pow(3,2))     #平方
print(pow(3,2,2))  #取余

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
9
1

Process finished with exit code 0

print():打印信息。

print('Hello Python!')

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
Hello Python!

Process finished with exit code 0

range():生成一个指定范围的数组。range特性顾头不顾尾

for i in range(1,5,2):   #从1开始到4结束,步长为2
    print(i)
print('----------------------')
for i in range(1, 5):
    print(i)

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
1
3
----------------------
1
2
3
4

Process finished with exit code 0

repr():将一个对象转成字符串显示,注:只是显示用

l = [1,2,3,4,5,6]
l_t = repr(l)
print(l_t,type(l_t))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
[1, 2, 3, 4, 5, 6] <class 'str'>

Process finished with exit code 0

zip():创建一个迭代器,聚合来自每个迭代器的元素。返回元组的迭代器。

l1 = [1,2,3,4]
l2 = ['a','b','c','d']
l = zip(l1,l2)
print(l)
print(list(l))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
<zip object at 0x0358A080>
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]

Process finished with exit code 0

round():保留多少位小数。

print(round(3.3456,3))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
3.346

Process finished with exit code 0

set():新建一个集合。

s = set({1,2,3,4,56})
print(type(s))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
<class 'set'>

Process finished with exit code 0

reversed():反转。

l = [1,2,3,4,5,6]
l1 = reversed(l)
print(l1)
for i in l1:
    print(i)

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
<list_reverseiterator object at 0x00989550>
6
5
4
3
2
1

Process finished with exit code 0

str():新建一个字符串。

s = str('abc')
print(s,type(s))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
abc <class 'str'>

Process finished with exit code 0

sum():求和运算。

l = [1,2,3,4,5,6,7]
print(sum(l))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
28

Process finished with exit code 0

tuple():新建一个元组。

t = (1,2,3,4,5,6,7)
print(t,type(t))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
(1, 2, 3, 4, 5, 6, 7) <class 'tuple'>

Process finished with exit code 0

type():查看一个值得类型。

t = (1,2,3,4,5,6,7)
print(type(t))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
<class 'tuple'>

Process finished with exit code 0

sorted():排序。

l=[1,2,4,9,-1]
print(sorted(l)) #从小到大
print(sorted(l,reverse=True)) #从大到小

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py
[-1, 1, 2, 4, 9]
[9, 4, 2, 1, -1]

Process finished with exit code 0

递归调用:

  在调用一个函数的过程中,直接或间接调用了该函数本身。

  递归效率低下,需要在进入下一次递归时保留当前的状态。

  Python没有尾递归,并且做了层级限制。

  必须有一个明确的结束条件。

示例:
l = [1,2,[3,[4,5,6,[7,8,[9,10,[11,12,13,[14,15]]]]]]] def readdate(list_name): for i in list_name: if isinstance(i,list): readdate(i) else: print(i) readdate(l)

匿名函数:

  自带return,无需函数名,一定有个返回值。匿名函数只能运行一次。

  冒号左边是参数,右边是函数体。

  使用lambda定义匿名函数:   

  lambda x:x**2        

示例:
l=[1,2,3,4]
m=map(lambda x:x**2,l)
print(list(m))

names=['alex','wupeiqi','yuanhao']
print(list(map(lambda item:item+'_SB',names)))