python-内置模块

                    内置函数


1.help()

 (1)概念:查看帮助信息

2.dir()

  (1)概念:当前文件支持的内置变量,列出key

 (2)例子:

代码
a = []
print dir(a)
结果
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

3.vars()

  (1)概念:当前文件支持的内置变量,列出key_value

  (2)例子:

代码
a = []
print vars()
结果
{'a': [], '__builtins__': <module '__builtin__' (built-in)>, '__file__': 'E:\\work\\test\\src\\test\\test.py', '__package__': None, '__name__': '__main__', '__doc__': "\ndef foo(name):\n    print name,'\xe5\x8e\xbb\xe7\xa0\x8d\xe6\x9f\xb4'\nfoo\n('\xe5\xbc\xa0\xe4\xb8\x89')\nfoo('\xe8\xb5\xb5\xe5\x9b\x9b')\n"}

4.type()

 (1)概念:查看变量的类型

 (2)例子:

代码
a = [1,2,3]
print type(a)
结果
<type 'list'>        #类型是list

5.import()

  (1)概念:导入模块 

  (2)语法:import 【模块名称】

6.reload ()

  (1)概念:之前导入了某一个模块,想重启导入一次

  (2)语法:remoload 【模块名称】

7.id()

8.abs()

  (1)概念:求绝对值

  (2)语法:abs(-9)

9.divmod()

  (1)概念:求商和余数

  (2)语法:divmod()

  (3)例子:

代码
print divmod(9,2)
结果
(4, 1)
----------------------
代码
print divmod(8,4)
结果
(2, 0)

10.pow

  (1)概念:求指数

  (2)语法:pow(2,2)

  (3)例子:

代码
print pow(2,2)
结果
4

11.len()

 (1)概念:字符的长度

 (2)语法:len()

12.zip() 

 (1)概念:把一个列表的字符按照竖列排序

 (2)例子

代码
x = [1,2,3]
y = [4,5,6]
z = [7,8,9]
结果:
print zip(x,y,z)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

13.以字符串的形式导入模块

temp = 'os'                #把os模块设置成变量
model = __import__(temp)   #导入模块
model.path

14.ramdon

 (1)概念:生成随机数字

 (2)例子

随机打印数字1-5其中的一个

import random
print random.randint(1,5)

随机打印大于1小于3的数字

import random
print random.randrange(1,3)

随机打印6个数字+字母

for i in range(6):                 #打印6个字符就得循环6次,0-5
    if i == random.randint(1,5):   #如果i=1-5其中的一个数字
        print random.randint(1,5)  #就打印出1-5中的其中一个数字
    else:
        tmp =  random.randint(65,90) #65到90之间是字母的ID,   
        print chr(tmp)               #chr是把数字转成字母

随机打印出6个字符(数字+字母)

code = []                        
for i in range(6):
    if i == random.randint(1,5):     #如果i=1-5其中的一个数字
        code.append(str(random.randint(1,5))) #就随机把一个数字写进code列表里
    else:
        tmp =  random.randint(65,90)   
        code.append(chr(tmp))        #否则就把65-90中的字母写进列表里
print ''.join(code)                  #最后把列表里的字符全部打印出来




你可能感兴趣的:(python,reduce,信息)