abc(*args, **kwargs)

取绝对值

def add(a,b,f):
return f(a)+f(b)
res = add(3,-6,abs)
print(res)

all(*args, **kwargs)

如果可迭代对象里面所有的元素都为真(非0),返回True
可迭代对象为空,也返回True
print( all([1,-5,3]) )
print( all([0,-5,3]) )
print(all([])) 运行结果:
True
False
True

any(*args, **kwargs)

如果可迭代对象里面任意的元素都为真(非0),返回True
可迭代对象为空,也返回False
print( any([]) )
print( any([1,-5,3]) )
print( any([0,-5,3]) ) 运行结果:
False
True
True

ascii(*args, **kwargs)

Return an ASCII-only representation of an object.
a= ascii([1,2,"开外挂开外挂"])
print(type(a),[a])
运行结果:
<class 'str'> ["[1, 2, '\\u5f00\\u5916\\u6302\\u5f00\\u5916\\u6302']"]

bin(*args, **kwargs)

Return the binary representation of an integer.
十进制转二进制
print(bin(1)) print(bin(3))
print(bin(8))
print(bin(255)) 运行结果:
0b1
0b11
0b1000
0b11111111

bool(x)

判断真假

bytearray()

字节和字符串不可修改,要修改,创建一个新的,在原有修改会覆盖

bytearray()可修改的字节格式

a = bytes("abcde",encoding="utf-8")
b = bytearray("abcde",encoding="utf-8")
print(a.capitalize(),a)
print( b[1] )
b[1]= 50
print( b[1] )
print(b) 运行结果:
b'Abcde' b'abcde'
98
50
bytearray(b'a2cde')

callable()

是否可以调用

def sayhi():pass
print( callable(sayhi) )
print( callable([]) ) 运行结果:
True
False

chr(*args, **kwargs)

返回ASCII码对应值
print(chr(98))
print(chr(87)) 运行结果:
b
W

ord()

输入的是ASCII码,与chr() 相反

print(ord('?'))
print(ord('@'))
运行结果:
63
64

complie(),exec()

code = '''
def fib(max): #10
n, a, b = 0, 0, 1
while n < max: #n<10
#print(b)
yield b
a, b = b, a + b
#a = b a =1, b=2, a=b , a=2,
# b = a +b b = 2+2 = 4
n = n + 1
return '---done---'
g = fib(6)
for i in g:
print(i)
''' py_obj = compile(code,"err.log","exec")
exec(py_obj)
exec(code)

dir()

看字典里有什么方法
>>> a= {}
>>> dir(a)
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get
attribute__', '__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', 'valu
es']

divmod(x, y)

Return the tuple (x//y, x%y) ,(商,余数)

>>> divmod(1,4)
(0, 1)

eval(*args, **kwargs)

可以把字符变字典,字符里有控制语句的(如for),就得用exec
a= "{1:'sadas'}"
print(type(eval(a)),eval(a))
x=1
print(eval("x+1+3")) 运行结果:
<class 'dict'> {1: 'sadas'}
5

filter(function,iterable)

先学习一下 匿名函数

def sayhi(n):
print(n) sayhi(5) (lambda n:print(n))(5)
calc = lambda n:3 if n<4 else n
print(calc(2))
运行结果: filter()一组数据中过滤出你想要的 res = filter(lambda n:n>5,range(10))
print(res) #变成一个迭代器
for i in res:
print(i) 运行结果:
<filter object at 0x0000018F692BB080> map() res = map(lambda n:n*2,range(10)) # [i * 2 for i in range(10)] , res = [ lambda i:i*2 for i in range(10)]
print(res)
for j in res:
print(j)
运行结果:
0
2
4
6
8
10
12
14
16
18

frozenset()

>>> set2 = frozenset(set1)
>>> set2
frozenset({3, 4, 5})
>>> set2.remove(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'remove'
不可变集合
a = frozenset([1,4,333,212,33,33,12,4])

globals(*args, **kwargs)

返回当前程序所有变量的key,value
Return the dictionary containing the current scope's global variables. print(globals()) 运行结果:
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001E64F79B048>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/PycharmProjects/python_code_scq/04_week_code/内置方法.py', '__cached__': None, '__author__': 'sunchengquan', '__mail__': '1641562360@qq.com'}

hex()

转十六进制
>>> hex(2)
'0x2'
>>> hex(10)
'0xa'
>>> hex(15)
'0xf'
>>> hex(600)
'0x258'

id()

返回内存地址

locals(*args, **kwargs)

def test():
local_var =333
print(locals())
print(globals())
test()
print(globals())
print(globals().get('local_var')) 运行结果:
{'local_var': 333}
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001E7C63BB048>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/PycharmProjects/python_code_scq/04_week_code/内置方法.py', '__cached__': None, '__author__': 'sunchengquan', '__mail__': '1641562360@qq.com', 'test': <function test at 0x000001E7C6013E18>}
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001E7C63BB048>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/PycharmProjects/python_code_scq/04_week_code/内置方法.py', '__cached__': None, '__author__': 'sunchengquan', '__mail__': '1641562360@qq.com', 'test': <function test at 0x000001E7C6013E18>}
None

oct()

转八进制
>>> oct(7)
'0o7'
>>> oct(8)
'0o10'
>>> oct(9)
'0o11'

pow()

Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
>>> pow(3,3)
27
>>> pow(2,3)
8
>>> pow(2,8)
256

sorted(*args, **kwargs)

a = {6:2,8:0,1:4,-5:6,99:11,4:22}
print(sorted(a))
print(sorted(a.items())) #按key排序
print(sorted(a.items(),key=lambda x:x[1]))# 按value 排序 运行结果:
[-5, 1, 4, 6, 8, 99]
[(-5, 6), (1, 4), (4, 22), (6, 2), (8, 0), (99, 11)]
[(8, 0), (6, 2), (1, 4), (-5, 6), (99, 11), (4, 22)]

zip()

a = [1,2,3,4,5,6]
b = ['a','b','c','d']
for i in zip(a,b):
print(i) 运行结果:
(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')

import()

__import__('decorator')

Json & pickle 数据序列化

序列化1:

a={'a':'a1','b':'b1'}
b=open('ceshi.txt','w')
b.write(str(a)) {'a': 'a1', 'b': 'b1'}

反序列化:

b=open('ceshi.txt','r')
a=b.read()
print a >>{'a': 'a1', 'b': 'b1'}

b=open('ceshi.txt','r')
print eval(b.read()) {'a': 'a1', 'b': 'b1'}

序列化2:

import json
a={'a':'a1','b':'b1'}
b=open('ceshi.txt','w')
b.write(json.dumps(a)) {"a": "a1", "b": "b1"}

反序列化:

import json
b=open('ceshi.txt','r')
print json.loads(b.read()) {u'a': u'a1', u'b': u'b1'}

 

 



python 内置方法、数据序列化的更多相关文章

  1. python-day4装饰器、生成器、迭代器、内置方法、序列化、软件目录

    @生成器generator a=(i*2 for i in range(10)) a.__next__()#等同于next(a),基本都不用,多用for循环a.send(m)#将m传为yield的值 ...

  2. 匿名函数 python内置方法(max/min/filter/map/sorted/reduce)面向过程编程

    目录 函数进阶三 1. 匿名函数 1. 什么是匿名函数 2. 匿名函数的语法 3. 能和匿名函数联用的一些方法 2. python解释器内置方法 3. 异常处理 面向过程编程 函数进阶三 1. 匿名函 ...

  3. Python内置方法详解

    1. 字符串内置方法详解 为何要有字符串?相对于元组.列表等,对于唯一类型的定义,字符串具有最简单的形式. 字符串往往以变量接收,变量名. 可以查看所有的字符串的内置方法,如: 1> count ...

  4. python内置方法

    1. 简介 本指南归纳于我的几个月的博客,主题是 魔法方法 . 什么是魔法方法呢?它们在面向对象的Python的处处皆是.它们是一些可以让你对类添加"魔法"的特殊方法. 它们经常是 ...

  5. 基于python内置方法进行代码混淆

    0x00 动态加载模块 在python脚本中,直接使用import os.import subprocess或from os import system这种方法很容易被规则检测,即使使用其它执行命令的 ...

  6. Python内置方法的时间复杂度(转)

    原文:http://www.orangecube.net/python-time-complexity 本文翻译自Python Wiki本文基于GPL v2协议,转载请保留此协议. 本页面涵盖了Pyt ...

  7. Python内置方法的时间复杂度

    转载自:http://www.orangecube.NET/Python-time-complexity 本页面涵盖了Python中若干方法的时间复杂度(或者叫"大欧"," ...

  8. Python 内置方法

    1. abs() 取绝对值函数 #!/usr/bin/env python # _*_ coding: UTF-8 _*_ # Author:taoke i = 100 print(abs(i)) i ...

  9. Python内置方法/函数

    abs() 返回数字的绝对值. abs(x) all() 用于判断给定的可迭代参数 iterable 中的所有元素是否都为 TRUE,如果是返回 True,否则返回 False. 元素除了是 0.空. ...

随机推荐

  1. Maven--排除依赖

    传递性依赖会给项目隐式地引入很多依赖,这极大地简化了项目依赖的管理,但是有些时候这种特性也会带来问题. 例如,当前项目有一个第三方依赖,而这个第三方的依赖由于某些原因依赖了另外一个类库的 SNAPSH ...

  2. ant design for vue select 数据回显问题

    例如: 想要回显id为1的温度, 结果直接在select框中显示了1,而不是选中了温度, 此时因为select中的value是string类型, 而我们设置的id是number类型, 对应不上, 所以 ...

  3. Python实现自动处理表格,让你拥有更多的自由时间!

    相信有不少朋友日常工作会用到 Excel 处理各式表格文件,更有甚者可能要花大把时间来做繁琐耗时的表格整理工作.最近有朋友问可否编程来减轻表格整理工作量,今儿我们就通过实例来实现 Python 对表格 ...

  4. 7.学完linux系统运维到底可以做什么?

    linux运维到底可以做什么?(略有改动原文.排版) 运维,很容易从字面理解为运营.维护. 很多朋友认为,在互联网公司中linux系统运维的工作就是安装系统,部署服务.处理紧急故障,为公司里的开发人员 ...

  5. 二分查找(python)

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/4/29 9:11 # @Author : Jackendoff # @Sit ...

  6. 为什么使用 document.write 需要将</script>拆分开

    福州SEO:细心点的朋友可能会注意到,有些网站使用document.write动态加载JS的时候需要把</script>拆分开来写?如下面的例子所示: <script type='t ...

  7. 在Orcl中通过SQL语句修改创建表

    1.创建表时定义唯一性约束 CREATE TABLE table_name ( column1 datatype null/not null, column2 datatype null/not nu ...

  8. poj 2342树形dp板子题1

    http://poj.org/problem?id=2342 #include<iostream> #include<cstdio> #include<cstring&g ...

  9. 非线程安全的HashMap 和 线程安全的ConcurrentHashMap

    在平时开发中,我们经常采用HashMap来作为本地缓存的一种实现方式,将一些如系统变量等数据量比较少的参数保存在HashMap中,并将其作为单例类的一个属性.在系统运行中,使用到这些缓存数据,都可以直 ...

  10. oracle ORA-01461 错误 can bind a LONG value only for insert into a LONG column

    我的ORACLE表里没有long字段,可是保存时报错:  ORA-01461 :仅可以为插入LONG列的LONG值赋值  本来我这张表里只有一个VARCHAR2(4000)的字段,一直没有这种错误发生 ...