python内置函数

#! /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数值存储的字节

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()

你可能感兴趣的:(python内置函数)