#! /usr/bin/env python
# -*- coding:utf-8 -*-
abs取絕對值
print(abs(-2))# 2
print(abs(2))# 2
all列表中所以元素為真篙梢,則為真,有一個為假則返回假
p = [1 , 2 , 3 , 0]#Flase
print(all(p))
any列表中只要有一個元素為真土辩,則全部為真
p = ['' , None , 0 , 1]#True
print(any(p))
bin取數值的二進制
print(bin(2))#0b10
bytes數值存儲的字節(jié)
name ="你好"
#encoding='utf-8' 以utf8編碼存儲數值
print(bytes(name, encoding='utf-8'))#b'\xe4\xbd\xa0\xe5\xa5\xbd'
#decode('utf-8')制圈,以utf8編碼格式來解碼葱峡,得到你好
print(bytes(name, encoding='utf-8').decode('utf-8'))#你好
#以什么格式的編碼存儲的字符必須以同編碼解碼谆膳,不然會出現亂碼
print(bytes(name, encoding='gbk'))#b'\xc4\xe3\xba\xc3'
print(bytes(name, encoding='gbk').decode('gbk'))#你好
dir查看函數下的方式或者方法
print(dir(bytes))
dirmod給出兩個數字责静,計算得到商和余數(分頁功能實現)
print(divmod(10 , 3))#(3, 1)
可hash的數據類型即不可變數據類型吟吝,不可hash的數據類型即可變數據類型
print(hash('12sdfdsaf3123123sdfasdfasdfasdfasdfasdfasdfasdfasfasfdasdf'))#-809812689698732392
print(hash('12sdfdsaf31231asdfasdfsadfsadfasdfasdf23'))#2452695028072014930
name='alex'
print(hash(name))#-5889667385052908400
print(hash(name))#-5889667385052908400
print('--->before',hash(name))#--->before -5889667385052908400
name='sb'
print('=-=>after',hash(name))#=-=>after 6161049198510667428
打印函數下有什么方法和使用的方法
print(help(all))
二進制
print(bin(10))#0b1010
#十六進制
print(hex(10))#0xa
#八進制
print(oct(10))#0o12
isinstance判斷字符是什么數據類型,如果不是返回False
print(isinstance(2 , str))#False
print(isinstance(2 , int))#True
name='哈哈'
#打印全局變量
print(globals())
'''
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001DF329053C8>, '__spec__': None, '__annotations__': {}, '__builtins__':
(built-in)>, '__file__': 'D:/python/day16/內置函數.py', '__cached__': None, 'name': '哈哈'}
'''
打印本文件的存放的地址
print(__file__)
'''
D:/python/day16/內置函數.py
'''
打印局部變量
def test():
age='1111111111111111111111111111111111111111111111111111111111111'
? ? # print(globals())
? ? print(locals())
test()
'''
以字典形式打印局部變量{'age': '1111111111111111111111111111111111111111111111111111111111111'}
'''
zip把兩個可迭代的對象以拉鏈形式連接到一起(一對一的拼接到一起)
a = ('a' , 'b' , 'c')
b = (1 , 2 , 3)
print(list(zip(a, b)))
'''
以列表形式打印[('a', 1), ('b', 2), ('c', 3)]
'''
字典用zip分割多個小元組
p = {'name':'alex','age':18,'gender':'none'}
print(list(zip(p.keys(), p.values())))
'''
[('name', 'alex'), ('age', 18), ('gender', 'none')]
'''
p =?[10 , 232 , 3 , 54]
#取最大值max
print(max(p))#232
#取最小值
print(min(p))#3
age_dic={'alex_age':18,'wupei_age':20,'zsc_age':100,'lhf_age':30}
print(max(age_dic))#默認比較的字典的key
print(max(age_dic.values()))#比較字典的valuse
print(max(zip(age_dic.values(), age_dic.keys())))#(100, 'zsc_age')
##不同類型不能比較
l=[1,3,100,-1,2]
print(max(l))
dic={'age1':18,'age2':10}
print(max(dic))#比較的是key
print(max(dic.values()))#比較的是key挣饥,但是不知道是那個key對應的
print(max(zip(dic.values(),dic.keys())))#結合zip使用
people=[
{'name':'alex','age':1000},
? ? {'name':'wupei','age':10000},
? ? {'name':'yuanhao','age':9000},
? ? {'name':'linhaifeng','age':18},
]
# max(people,key=lambda dic:dic['age'])
print(max(people,key=lambda dic:dic['age']))
ret=[]
for itemin people:
ret.append(item['age'])
print(ret)
max(ret)
ascci表
print(chr(97))#對應的字符
print(ord('a'))#對應的數字
#字符翻轉
l=[1,2,3,4]
print(list(reversed(l)))
print(l)
print(round(3.5))
print(set('hello'))
l='hello'
s1=slice(3,5)
s2=slice(1,4,2)
# print(l[3:5])
print(l[s1])
print(l[s2])
print(s2.start)
print(s2.stop)
print(s2.step)
l=[3,2,1,5,7]
l1=[3,2,'a',1,5,7]
print(sorted(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']))
排列
people1={
'abyuanhao':11900,
? ? 'alex':1200,
? ? 'wupei':300,
}
print(sorted(zip(people1.values(), people1.keys())))
print(str('1'))
print(type(str({'a':1})))
dic_str=str({'a':1})
print(type(eval(dic_str)))
l=[1,2,3,4]
print(sum(l))
print(sum(range(5)))
print(type(1))
msg='123'
if type(msg)is str:
msg=int(msg)
res=msg+1
? ? print(res)
def test():
msg='撒旦法阿薩德防撒旦浪費艾絲凡阿斯蒂芬'
? ? print(locals())
print(vars())
test()
print(vars(int))
import------>sys----->__import__()
import test
test.say_hi()
import 'test'#報錯
module_name='test'
m=__import__(module_name)
m.say_hi()