python的内置函数

# print()
# input()
# len()
# type()
# open()
# tuple()
# list()
# int()
# bool()
# set()
# dir()
# id()
# str()


# print(locals())  #返回本地作用域中的所有名字
# print(globals()) #返回全局作用域中的所有名字
# global 变量
# nonlocal 变量

#迭代器.__next__()
# next(迭代器)
# 迭代器 = iter(可迭代的)
# 迭代器 = 可迭代的.__iter__()

# range(10)
# range(1,11)
# print('__next__' in dir(range(1,11,2)))

# dir 查看一个变量拥有的方法
print(dir([]))
print(dir(1))
print(dir(list))
help
help(str)

# 变量callable检测是不是一个函数
print(callable(print))
a = 1
print(callable(a))  #False
print(callable(globals))    #True
def func():pass
print(callable(func))   #True

f = open('文件.py')
print(f.writable())  #判断可不可写False
print(f.readable()) #判断可不可以读True

#hash - 对于相同可hash数据的hash值在一次程序的执行过程中总是不变的  #列表字典集合不可hash
print(hash(123456))     #123456
print(hash("lghjkopmn"))   #8688261886816884649
print(hash(("aa","bb")))    #-2995720226401146882

print('我们的祖国是花园',end='')  #指定输出的结束符不换行
print('我们的祖国是花园',end='')
print(1,2,3,4,5,sep='|') #指定输出多个值之间的分隔符
#我们的祖国是花园我们的祖国是花园1|2|3|4|5

# 打印到指定文件,不在解释器显示
# 创建一个file文件,并把aaaa写入
f = open('file','w')
print('aaaa',file=f)
f.close()

print(bin(10))  #二进制    0b1010
print(oct(10))  #八进制    0o12
print(hex(10))  #16进制   0xa

print(abs(-5)) #绝对值
print(abs(5))

print(divmod(7,2))   # div出发 mod取余
print(divmod(9,5))   # 除余

#reversed  反转

L1=[11,22,33,44]
L1.reverse()
print(L1)   #[44, 33, 22, 11]

l = [1,2,3,4,5]
l2 = reversed(l)
print(l2)   #<list_reverseiterator object at 0x000002778C05B390>
# 保留原列表,返回一个反向的迭代器


#bytes 转换成bytes类型
# 我拿到的是gbk编码的,我想转成utf-8编码
print(bytes('你好',encoding='GBK'))   # unicode转换成GBK的bytes b'xc4xe3xbaxc3'
print(bytes('你好',encoding='utf-8')) # unicode转换成utf-8的bytes   b'xe4xbdxa0xe5xa5xbd'

# k=1000
# while k>1:
#     print(k)
#     k=k/2
#
# L=range(100)
# print(list(L[:3]))
# print(L[98])
# print(L[-2])
# print(list(L[-10::]))
# print(list(L[90:]))
# L1=list(L[:])
# print(L1)
#
# d={"aa":11,"bb":22,'cc':33}
# for k in d:
#     print(k)
#     print(d[k])
# len函数

str="asdfghh"
print(len(str))
list1=[11,22,33]
print(len(list1))

# enumerate多用于在for循环中得到计数,利用它可以同时获得索引和值,
# 即需要index和value值的时候可以使用enumerate

list1=[11,22,33,44,55,66]
for index,value in enumerate(list1):    #从0开始
    print(index,value)
# 0 11
# 1 22
# 2 33
# 3 44
# 4 55
# 5 66

for index,value in enumerate(list1,1):  #从1开始
    print(index,value)
#zip拉链方法,拉倒一起

l = [1,2,3,4,5]
l2 = ['a','b','c','d']
l3 = ('*','**',[1,2])
d = {'k1':1,'k2':2}

for i in zip(l,l2):
    print(i)
    #(1, 'a')
    # (2, 'b')
    # (3, 'c')
    # (4, 'd')
for i in zip(l,l2,l3,d):
    print(i)
    #(1, 'a', '*', 'k1')
    #(2, 'b', '**', 'k2')

#sort排序
list1 = [1,-4,6,5,-10]
list1.sort()   # 在原列表的基础上进行排序
print(list1)
list1.sort(key = abs)#绝对值排序
print(list1)

print(sorted(list1,key=abs,reverse=True)) # 生成了一个新列表 不改变原列表 占内存
print(list1)

# lambda函数
ret = lambda x,y:x+y
print(ret(2,3))

#eval
# eval函数就是实现list、dict、tuple与str之间的转化
# str函数把list,dict,tuple转为为字符串
# 字符串转换成列表
a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
print(type(a))  #<class 'str'>
b = eval(a)
print(b)    #[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]

# 字符串转换成字典
a = "{1: 'a', 2: 'b'}"
print(type(a))  #<class 'str'>
b = eval(a)
print(type(b))  #<class 'dict'>
print(b)        #{1: 'a', 2: 'b'}

# 字符串转换成元组
a = "([1,2], [3,4], [5,6], [7,8], (9,0))"
print(type(a))  #<class 'str'>
b=eval(a)
print(type(b))  #<class 'tuple'>
print(b)        #([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))

# Python下一切皆对象,每个对象都有多个属性(attribute),Python对属性有一套统一的管理方案。
# __dict__与dir()的区别:
#
# dir()是一个函数,返回的是list;
# __dict__是一个字典,键为属性名,值为属性值;
# dir()用来寻找一个对象的所有属性,包括__dict__中的属性,__dict__是dir()的子集;
#
# 并不是所有对象都拥有__dict__属性。许多内建类型就没有__dict__属性,如list,此时就需要用dir()来列出对象的所有属性。

class MyName(object):
def __init__(self):
self.name = 'name'
self.sex = 'female'
me = MyName()
print(me.__dict__) #{'name': 'name', 'sex': 'female'}
print(dir(me))
#['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'sex']
 
原文地址:https://www.cnblogs.com/chvv/p/10137313.html