内置函数

一.    作用域相关(2个)

locals():返回本地作用域中所有名字

globals():返回全局作用域中所有名字

 

1 a=5
2 def f():
3     b=5
4     print(locals())     #{'b': 5}
5 
6 f()
7 print(globals())

 二.    迭代器相关(3个)

next(迭代器):__next__(迭代器)

iter(可迭代的):__iter__(可迭代的)

rang():是一个可迭代的,不是迭代器,可以进行切片

 三.    其他(12个)

 1、输入输出:

 input

 output

 2、文件操作:

 open

 3、内存相关:

 (1)hash(参数):返回一个可hash变量的哈希值,不可hash的变量被hash之后会进行报错(用于获取取一个对象(字符串或者数值等)的哈希值。

1 print(hash(1))           #1
2 print(hash('asdfg'))   #-864129659387839257
3 print(hash([1,2,3]))  #unhashable type: 'list'

可哈希的值是不可变的

(2)id(参数):返回一个变量的内存地址

  1 a=5 2 print(id(a)) #1487602256 

4、查看内置属性:

dir(参数):默认查看全局空间内的属性,也接受一个参数,查看这个参数内的方法或变量

 1 b=5 2 print(dir(b)) 

5、模块相关:

__import__:导入模块 

1 import time
2 t = __import__('time')
3 print(t.time())

6、帮助方法:

help:

(1)在控制台执行help()进入帮助模式。可以随意输入变量或者变量的类型。输入q退出

(2)help(参数):查看和参数有关的操作

7、调用相关:

callable(参数):查看这个变量是不是可以调用的,如果参数是一个函数名,就会返回True

1 def f():
2     print(a=5)
3 
4 print(callable(f))   #True

8、字符串类型代码的执行:

(1)eval(expression, globals=None, locals=None) 

       官方文档中的解释是,将字符串str当成有效的表达式来求值并返回计算结果。globals和locals参数是可选的,如果提供了globals参数,那么它必须是dictionary类型;如果提供了locals参数,那么它可以是任意的map对象。locals()对象的值不能修改,globals()对象的值可以修改。

 1 x = 1
 2 y = 1
 3 num1 = eval("x+y")
 4 print("num1=",num1)
 5 
 6 
 7 def g():
 8     x = 2
 9     y = 2
10     num3 = eval("x+y")
11     print("num3=",num3)
12     num2 = eval("x+y", globals())    #使用全局的X,Y
13     print("num2=", num2)
14     num4 = eval("x+y",globals(),locals())   #根据变量的使用原则,优先使用局部变量
15     print("num4=", num4)
16 
17 
18 g()
19 
20 print(locals()["x"])  #1
21 print(locals()["y"])  #1
22 print(globals()["x"]) #1
23 print(globals()["y"]) #1

 

 locals()对象的值不能修改,globals()对象的值可以修改

 1 z=0
 2 def f():
 3     z = 1
 4     print (locals())
 5     locals()["z"] = 2
 6     print (locals())
 7 f()
 8 globals()["z"] = 2  
 9 print (z)  #z的值未修改成功
10 
11 结果:
12 {'z': 1}
13 {'z': 1}
14 2

(2)exec

  官方声明This statement supports dynamic execution of Python code。exec语句用来执行储存在字符串或文件中的python语句。exec 是一个语法声明,不是一个函数.也就是说和if,for一样。exec函数是只执行语句代码,而不执行表达式代码,因为它没有任何返回值。exec可以执行:代码字符串、文件对象、代码对象、tuple

 (需要在看看视频)

 (3)compile

 http://www.cnblogs.com/Eva-J/articles/7266087.html

 

四.和数字相关(14个)

 

1、数据类型相关:bool,int,float,complex

 (1)   bool():

 (2)   int(): 函数用于将一个字符串或数字转换为整型

 (3)   float():函数用于将整数和字符串转换成浮点数。

 (4)   complex():函数用于创建一个值为 real + imag * j 的复数或者转化一个字符串或数为复数。如果第一个参数为字符串,则不需要指定第二个参数。

 

2、进制转换相关:bin,oct,hex

 (1)bin():二进制

 (2)oct():八进制

 (3)hex():十六进制

 

3、数学运算:abs,divmod,min,max,sum,round,pow

 (1)abs()绝对值

 (2)divmod():除余  div:除法    mod:取余

 (3) min():求最小值

 (4)max():求最大值

 (5)sum():求和

(6) round():保留小数点后几位

 (7)pow():幂运算

 1 print(bool(0))
 2 print(bool(1==2))
 3 print(bool(2))
 4 
 5 print(complex(1))
 6 print(complex("2"))
 7 
 8 print(bin(5))
 9 print(oct(1))
10 print(hex(7))
11 
12 print(pow(3,3))
13 print(round(3.141596,3))

 五.数据结构(24个)

1、序列:

(1)列表和元组:list()、tuple()

(2)字符串相关:str,format,bytes,bytearry,memoryview,ord,chr,ascii,repr

(3)序列:

  reversed:

1 l=[1,2,3,4,5,6,7]
2 l.reverse()
3 print(l)   #不会生成新的列表
4 
5 l2=reversed(l) #生成新的列表
6 print(list(l2))   #<list_reverseiterator object at 0x0000016E9F82FAC8>
7
8#结果:
[7, 6, 5, 4, 3, 2, 1]
[1, 2, 3, 4, 5, 6, 7]

 

1 l = (1,2,23,213,5612,342,49)
2 print(id(l))
3 print(id(list(reversed(l))))
4 
5 
6 结果:
7 2035675490408
8 2035675666760

    slice():函数实现切片对象,主要用在切片操作函数里的参数传递。

class slice(stop)
class slice(start, stop, [step])
参数说明:
start -- 起始位置
stop -- 结束位置
step -- 间距

 

1 l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
2 li=slice(5,20,3)
3 print(l[li])
4 
5 #结果:
6 [16, 26, 35, 43, 66]

 

2、数据集合:

(1)   字典:dict

(2)   集合:set、frozenset

3、其他:len,sorted,enumerate,all,any,zip,filter,map

filter() 和 map():

1 def f(x):
2     return x%2==0
3 li=[1,2,3,4,5,6]
4 
5 #print(list(filter(f(),li)))  #不是调用f()
6 print(list(filter(f,li)))   #[2, 4, 6]
7 print(list(map(f,li)))    #[False, True, False, True, False, True]

 

1 def not_null_empty(s):
2     return s and len(s.strip()) >0    #要记住.strip为字符串的方法,strip默认删除空白符(包括'\n'(换行), '\r'(制表符),  '\t'(回车) ,  ' ')
3 
4 print(filter(not_null_empty,[1,2,'',345]))   #<filter object at 0x000001F7F791FAC8>
5 print(list(filter(not_null_empty,['1','2','   ','345',None])))  #['1', '2', '345']
6 
7 def is_not_empty(s):
8     return s and len(s.strip()) > 0   #去除'  '后长度还大于0的。
9 print(list(filter(is_not_empty, ['test', None, '', 'str', '  ', 'END'])))

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

sorted:(sort)

 1 li=[1,2,3,4,5,-6]
 2 #l2=li.sort()
 3 #print(l2)   #None,因为sort是在原列表上进行排序
 4 li.sort()
 5 print(li)   #[-6, 1, 2, 3, 4, 5]
 6 
 7 
 8 l2=sorted(li)
 9 print(l2)    #[-6, 1, 2, 3, 4, 5],sorted生成新的列表
10 
11 
12 l3=sorted(li,key=abs)
13 print(l3) #  [1, 2, 3, 4, 5, -6]

zip():  函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。

1 a=[1,2,3,4]
2 b=[4,78,9,0,3]
3 
4 print(list(zip(a,b)))
5 
6 结果:
7 [(1, 4), (2, 78), (3, 9), (4, 0)]

enumerate():将可循环序列sequence以start开始分别列出序列数据和数据下标

 1 li=['pipi','niuniu','caicai']
 2 print(list(enumerate(li,start=1)))   
 3 
 4 li = ['pipi', 'niuniu', 'caicai']
 5 # print(list(enumerate(li)))
 6 
 7 for i,item in enumerate(li):
 8     print(i,item)
 9 
10 for i,item in enumerate(li,1):   #从1开始
11     print(i,item)
12 
13 结果:
14 [(1, 'pipi'), (2, 'niuniu'), (3, 'caicai')]
15 0 pipi
16 1 niuniu
17 2 caicai
18 1 pipi
19 2 niuniu
20 3 caicai

any(x):判断x对象是否为空对象,如果都为空、0、false,则返回false,如果不都为空、0、false,则返回true

all(x):如果all(x)参数x对象的所有元素不为0、''、False或者x为空对象,则返回True,否则返回False

 

posted on 2018-11-13 10:40  cherish-LL  阅读(146)  评论(0)    收藏  举报

导航