Python一些简单的内置函数

1. import()

test.py里面

def say_hi():
    print('hello')
    
module_name='test'
m =__import__(module_name)
m.say_hi()
输出:
hello

2. locals()和vars()

msg=123
def test():
    msg='撒旦法阿萨德防撒旦浪费艾丝凡阿斯蒂芬'
    print(locals())
    print(vars())
test()
print(vars(int))
输出:
{'msg': '撒旦法阿萨德防撒旦浪费艾丝凡阿斯蒂芬'}
{'msg': '撒旦法阿萨德防撒旦浪费艾丝凡阿斯蒂芬'}
{'__repr__': <slot wrapper '__repr__' of 'int' objects>, '__hash__': <slot wrapper '__hash__' of 'int' objects>, '__str__': <slot wrapper '__str__' of 'int' objects>, ... 省略

3.排序和eval

l=[3,2,1,5,7]
l1=[3,2,'a',1,5,7]
print(sorted(l))
print('这是l:',l)
# print(sorted(l1)) #  排序本质就是在比较大小,不同类型之间不可以比较大小

people=[
    {'name':'alex','age':1000},
    {'name':'wupei','age':10000},
    {'name':'yuanhao','age':9000},
    {'name':'linhaifeng','age':18},
]
print(sorted(people,key=lambda dic:dic['age']))
name_dic={
    'abyuanhao': 11900,
    'alex':1200,
    'wupei':300,
}
print(sorted(name_dic))

print(sorted(name_dic,key=lambda key:name_dic[key]))

print(sorted(zip(name_dic.values(),name_dic.keys())))

print(type(str({'a':1})))
dic_str=str({'a':1})
print(type(eval(dic_str)))
输出:
[1, 2, 3, 5, 7]
这是l: [3, 2, 1, 5, 7]
[{'name': 'linhaifeng', 'age': 18}, {'name': 'alex', 'age': 1000}, {'name': 'yuanhao', 'age': 9000}, {'name': 'wupei', 'age': 10000}]
['abyuanhao', 'alex', 'wupei']
['wupei', 'alex', 'abyuanhao']
[(300, 'wupei'), (1200, 'alex'), (11900, 'abyuanhao')]
<class 'str'>
<class 'dict'>

4. all和any

print(all([1,2,'1']))
print(all([1,2,'1','']))
print(all('')) ## 这个要特别注意
输出 :
True
False
True

print(any([0,'']))
print(any([0,'',1]))
输出:
False
True

print(bin(4))

#空,None,0的布尔值为False,其余都为True
print(bool(''))
print(bool(None))
print(bool(0))
输出: 
0b100
False
False
False



name='你好'
print(bytes(name,encoding='utf-8'))
print(bytes(name,encoding='utf-8').decode('utf-8'))
print(bytes(name,encoding='gbk'))
print(bytes(name,encoding='gbk').decode('gbk'))

# print(bytes(name,encoding='ascii'))#ascii不能编码中文

print(chr(46))
print(dir(dict))
print(divmod(10,3))
输出:
b'\xe4\xbd\xa0\xe5\xa5\xbd'
你好
b'\xc4\xe3\xba\xc3'
你好
.
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
(3, 1)


print(help(all))
print(bin(10))#10进制->2进制
print(hex(12))#10进制->16进制
print(oct(12))#10进制->8进制
输出:
all(iterable, /)
    Return True if bool(x) is True for all values x in the iterable.
    
    If the iterable is empty, return True.

None
0b1010
0xc
0o14



name='哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈粥少陈'
print(globals())
print(__file__)
输出:
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000000000212C2E8>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'X:/python3.64/Problems_疑难问题/pycharm_file/days/python全栈s3  day17课上所有/匿名函数.py', '__cached__': None, 'name': '哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈粥少陈'}
X:/python3.64/Problems_疑难问题/pycharm_file/days/python全栈s3  day17课上所有/匿名函数.py



def test():
    age='1111111111111'
    print(globals())
    print(locals())

test()

输出:
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000000000261C2E8>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'X:/python3.64/Problems_疑难问题/pycharm_file/days/python全栈s3  day17课上所有/匿名函数.py', '__cached__': None, 'test': <function test at 0x0000000002875950>}
{'age': '1111111111111'}


4. zip函数

print(list(zip(('a','n','c'),(1,2,3))))
print(list(zip(('a','n','c'),(1,2,3,4))))
print(list(zip(('a','n','c','d'),(1,2,3))))

p={'name':'alex','age':18,'gender':'none'}
print(list(zip(p.keys(),p.values())))
print(list(p.keys()))
print(list(p.values()))

print(list(zip(['a','b'],'12345')))
输出:
[('a', 1), ('n', 2), ('c', 3)]
[('a', 1), ('n', 2), ('c', 3)]
[('a', 1), ('n', 2), ('c', 3)]
[('name', 'alex'), ('age', 18), ('gender', 'none')]
['name', 'age', 'gender']
['alex', 18, 'none']
[('a', '1'), ('b', '2')]

5. max

age_dic={'alex_age':18,'wupei_age':20,'zsc_age':100,'lhf_age':30}

print(max(age_dic.values()))
#
# #默认比较的是字典的key
print(max(age_dic))

for item in zip(age_dic.values(),age_dic.keys()): #[(18,'alex_age')  (20,'wupeiqi_age') () () ()]
    print(item)
#
print('=======>',list(max(zip(age_dic.values(),age_dic.keys()))))

l=[
    (5,'e'),
    (1,'b'),
    (3,'a'),
    (4,'d'),
]
# l1=['a10','b12','c10',100] #不同类型之间不能进行比较
l1=['a10','a2','a10'] #不同类型之间不能进行比较
print(list(max(l)))
print('--->',list(max(l1)))  注意此处,把结果再次list了
dic={'age1':18,'age2':10}
print(max(zip(dic.values(),dic.keys()))) #结合zip使用,去除最大的values

people=[
    {'name':'alex','age':1000},
    {'name':'wupei','age':10000},
    {'name':'yuanhao','age':9000},
    {'name':'linhaifeng','age':18},
]

print('周绍陈取出来没有',max(people,key=lambda dic:dic['age']))
输出:
100
zsc_age
(18, 'alex_age')
(20, 'wupei_age')
(100, 'zsc_age')
(30, 'lhf_age')
=======> [100, 'zsc_age']
[5, 'e']
---> ['a', '2']
(18, 'age1')
周绍陈取出来没有 {'name': 'wupei', 'age': 10000}```

6.ord和chr


print(chr(97))
print(ord('a'))
print(pow(3,3))  #3**3
print(pow(3,3,2))  #3**3%2
输出:
a
97
27
1

7.reverse

l=[1,9,3,4,6,2,0]
print(list(reversed(l)))
print(reversed(l))
print(l)
print(round(3.5))
print(set('hello'))
输出
[0, 2, 6, 4, 3, 9, 1]
<list_reverseiterator object at 0x000000000288AEF0>
[1, 9, 3, 4, 6, 2, 0]
4
{'e', 'l', 'h', 'o'}

7. slice

l='hello'
s1=slice(3,5)
s2=slice(1,4,2)
print(s1)
print(s2)
print(l[3:8])
print(l[s1])
print(l[s2])
print(s2.start)
print(s2.stop)
print(s2.step)
输出:
slice(3, 5, None)
slice(1, 4, 2)
lo
lo
el
1
4
2
posted @ 2021-05-10 15:07  ty1539  阅读(93)  评论(0编辑  收藏  举报