Built-in Functions    
abs() delattr() hash() memoryview() set()
all() dict() help() min() setattr()
any() dir() hex() next() slice()
ascii() divmod() id() object() sorted()
bin() enumerate() input() oct() staticmethod()
bool() eval() int() open() str()
breakpoint() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()  
# abs(x)
print(abs(-10)) #
# 取绝对值 ###################################
# all(iterable)
print(all([1,2,3,4,5])) # True
print(all((0,1,2,3,4))) # False
# 可迭代对象中,如果存在一个以上bool为False的,则整体为False ###################################
# any(iterable)
print(any([None,False,(),[],{},])) # False
print(any([None,False,(),[],{},10])) # True
# 可迭代对象中,只要存在一个是True,则为True ###################################
# ascii(object)
print(ascii("abcd")) # 'abcd'
print(ascii("中国")) # '\u4e2d\u56fd' ###################################
# bin(x)
print(bin(3)) # 0b11
print(bin(-10)) # -0b1010
# 二进制 ###################################
# bool([x])
print(bool(None)) # False
print(bool(2)) # True ###################################
# breakpoint(*args, **kws)
# print(breakpoint()) # 3.6 ###################################
# bytearray([source[, encoding[, errors]]])
print(bytearray()) # bytearray(b'')
print(bytearray([1,2,3])) # bytearray(b'\x01\x02\x03')
print(bytearray('test', 'utf-8')) # bytearray(b'test')
# 如果 source 为整数,则返回一个长度为 source 的初始化数组;
# 如果 source 为字符串,则按照指定的 encoding 将字符串转换为字节序列;
# 如果 source 为可迭代类型,则元素必须为[0 ,255] 中的整数;
# 如果 source 为与 buffer 接口一致的对象,则此对象也可以被用于初始化 bytearray。
# 如果没有输入任何参数,默认就是初始化数组为0个元素。
# bytearray() 方法返回一个新字节数组。这个数组里的元素是可变的,并且每个元素的值范围: 0 <= x < 256。 ###################################
# bytes([source[, encoding[, errors]]])
print(bytes("中国","utf-8")) # b'\xe4\xb8\xad\xe5\x9b\xbd'
print(bytes("中国","gbk")) # b'\xd6\xd0\xb9\xfa'
print(bytes("hello world","utf-8")) # b'hello world'
print(bytes("hello world","gbk")) # b'hello world' ###################################
# callable(object)
def test():
pass class T():
pass
class T1():
def __call__(self):
pass print(callable([1,2,3])) # False
print(callable(test)) # True
print(callable(T())) # False
print(callable(T1())) # True
# 验证对象是否实现了__call__方法 ###################################
# chr(i)
print(chr(77)) # M
# 通过ascii码得出对应的字符
print(ord("M")) #
# 反向,查找出字符对应的ascii码 ###################################
# classmethod
# 类方法 ###################################
# compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)
str = "for i in range(0,10): print(i)"
c = compile(str,'','exec') # 编译为字节代码对象
exec(c)
#
#
#
#
#
#
#
#
#
#
# compile() 函数将一个字符串编译为字节代码。 ###################################
# complex([real[, imag]])
print(complex(1, 2)) # (1+2j)
# complex() 函数用于创建一个值为 real + imag * j 的复数或者转化一个字符串或数为复数。如果第一个参数为字符串,则不需要指定第二个参数。 ###################################
# delattr(object, name)
# 反射 ###################################
# dir([object])
# 查看对象的namespace ###################################
# divmod(a, b)
print(divmod(100,3)) # (33, 1)
# a被除数,b除数
# res:(商,余数) ###################################
# enumerate(iterable, start=0)
for index,value in enumerate([123,32,2],start=3):
print(index,value)
# 3 123
# 4 32
# 5 2 ###################################
# eval(expression, globals=None, locals=None)
print(eval("1+2+3*4")) #
print(eval("x+y",{"x":1,"y":2})) #
# 计算指定表达式的值 ###################################
# exec(object[, globals[, locals]])
class T():
pass
exec("x = T()")
print(x) # <__main__.T object at 0x05A81050> x = 10
expr = """
z = 30
sum = x + y + z #一大包代码
print(sum)
"""
def func():
y = 20
exec(expr) # 10+20+30
exec(expr,{'x':1,'y':2}) # 30+1+2
exec(expr,{'x':1,'y':2},{'y':3,'z':4}) # 30+1+3,x是定义全局变量1,y是局部变量
func()
#
#
# # 动态执行python代码,exec无返回值 ###################################
# filter(function,iterable)
print(list(filter(lambda x:x>4,[1,2,3,4,5,6,7,8,9]))) # [5, 6, 7, 8, 9]
# filter对象是一个迭代器,筛选 ###################################
# float([x])
print(float(-123)) # -123.0
print(float('1e-003')) # 0.001
print(float('+1E6')) # 1000000.0
print(float("-Infinity")) # -inf ###################################
# format(value[, format_spec])
print(format(3,'b')) #转换成二进制 '11'
print(format(314159267,'0.2E')) #科学计数法,指定保留2位小数,采用大写E表示 '3.14E+08' ###################################
# frozenset([iterable])
print(frozenset([1,2,3,4])) # frozenset({1, 2, 3, 4})
# 不能改变,没有add,remove ###################################
# getattr(object, name[, default])
# 反射 ###################################
# globals()
print(globals())
# {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x03B6B450>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/Python相关/过程project/爬虫复习/第一次review/tmp.py', '__cached__': None, 'test': <function test at 0x057F07C8>, 'T': <class '__main__.T'>, 'T1': <class '__main__.T1'>, 'index': 5, 'value': 2, 'x': 10, 'expr': '\nz = 30\nsum = x + y + z #一大包代码\nprint(sum)\n', 'func': <function func at 0x057F0780>}
# 全局变量 ###################################
# hasattr(object, name)
# 反射 ###################################
# hash(object)
print(hash('good good study')) # -724571439
print(hash(1.0)) #
print(hash(1)) #
print(hash(1.0000)) #
# hash() 用于获取取一个对象(字符串或者数值等)的哈希值。 ###################################
# help([object])
help('str')
# help() 函数用于查看函数或模块用途的详细说明。 ###################################
# hex(x)
print(hex(255)) # 0xff
print(hex(12)) # 0xc
print(type(hex(123))) # <class 'str'>
# hex() 函数用于将10进制整数转换成16进制,以字符串形式表示。 ###################################
# id([object])
# id() 函数用于获取对象的内存地址。 ###################################
# input([prompt])
# input("input:") # input:
# input() 函数接受一个标准输入数据,返回为 string 类型。 ###################################
# class int(x, base=10)
print(int(3.2)) #
print(int('',16)) #
print(int("",8)) #
# int() 函数用于将一个字符串或数字转换为整型。 ###################################
# isinstance(object, classinfo)
# isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()。 ###################################
# issubclass(class, classinfo)
# issubclass() 方法用于判断参数 class 是否是类型参数 classinfo 的子类。 ###################################
# iter(object[, sentinel])
l = iter([1,2,3])
print(next(l)) #
print(next(l)) #
# iter() 函数用来生成迭代器。 ###################################
# len( s )
# Python len() 方法返回对象(字符、列表、元组等)长度或项目个数。 ###################################
# list( seq )
# list() 方法用于将元组转换为列表。 ###################################
# locals()
# locals() 函数会以字典类型返回当前位置的全部局部变量。 ###################################
# map(function, iterable, ...)
print(list(map(lambda x:x**3,[1,2,3]))) # [1, 8, 27]
# map() 会根据提供的函数对指定序列做映射。 ###################################
# max(iterable, *[, key, default])
# max(arg1, arg2, *args[, key])
d = {"a":10,"b":9,"c":8,"d":7,"e":6}
print(max(d)) # e
print(max(d,key=lambda x:d[x])) # a ###################################
# memoryview(obj)
v = memoryview(bytearray("abcefg", 'utf-8'))
print(v[1]) #
print(v[-1]) #
print(v[1:4]) # <memory at 0x09F7D8B8>
print(v[1:4].tobytes()) # b'bce'
# memoryview() 函数返回给定参数的内存查看对象(Momory view)。
# 所谓内存查看对象,是指对支持缓冲区协议的数据进行包装,在不需要复制对象基础上允许Python代码访问。 ###################################
# min(iterable, *[, key, default])
# min(arg1, arg2, *args[, key])
d = {"a":10,"b":9,"c":8,"d":7,"e":6}
print(min(d)) # a
print(min(d,key=lambda x:d[x])) # e ###################################
# next(iterator[, default])
# next() 返回迭代器的下一个项目。 ###################################
# oct(x)
print(oct(10)) # 0o12
print(oct(20)) # 0o24
# oct() 函数将一个整数转换成8进制字符串。 ###################################
# open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) ###################################
# ord(c)
# ord() 函数是 chr() 函数(对于8位的ASCII字符串)或 unichr() 函数(对于Unicode对象)的配对函数,它以一个字符(长度为1的字符串)作为参数,返回对应的 ASCII 数值,或者 Unicode 数值,如果所给的 Unicode 字符超出了你的 Python 定义范围,则会引发一个 TypeError 的异常。 ###################################
# pow(x, y[, z])
print(pow(2,10)) #
print(pow(2,10,10)) # 4,取模,等价于pow(x,y) %z
# 返回 xy(x的y次方) 的值。 ###################################
# print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) ###################################
# property(fget=None, fset=None, fdel=None, doc=None) ###################################
# range(stop)
# range(start, stop[, step]) ###################################
# repr(object)
# repr() 函数将对象转化为供解释器读取的形式。 ###################################
# reversed(seq)
seqRange = range(5, 9)
print(list(reversed(seqRange))) # [8, 7, 6, 5]
# reversed 函数返回一个反转的迭代器。 ###################################
# round(number[, ndigits])
print ("round(70.23456) : ", round(70.23456)) # round(70.23456) : 70
print ("round(-100.000056, 3) : ", round(-100.000056, 3)) # round(-100.000056, 3) : -100.0
# round() 方法返回浮点数x的四舍五入值。 ###################################
# set([iterable]) ###################################
# setattr(object, name, value)
# 反射 ###################################
# slice(stop)
# slice(start, stop[, step])
myslice = slice(5) # 设置截取5个元素的切片
myslice1 = slice(2,9)
myslice2 = slice(2,9,3)
arr = range(10)
print(arr[myslice]) # range(0, 5)
print(arr[myslice1]) # range(2, 9)
print(arr[myslice2]) # range(2, 9, 3)
# slice() 函数实现切片对象,主要用在切片操作函数里的参数传递。 ###################################
# sorted(iterable, *, key=None, reverse=False)
print(sorted([5, 2, 3, 1, 4])) # [1, 2, 3, 4, 5]
print(sorted([5, 0, 6, 1, 2, 7, 3, 4], key=lambda x: x*-1)) # [7, 6, 5, 4, 3, 2, 1, 0]
# sorted() 函数对所有可迭代的对象进行排序操作。 ###################################
# staticmethod ###################################
# str(object='')
# str(object=b'', encoding='utf-8', errors='strict') ###################################
# sum(iterable[, start])
print(sum((2, 3, 4), 1)) # 10 元组计算总和后再加 1
print(sum((range(3,5)), 3)) # 10 元组计算总和后再加 3
# sum() 方法对系列进行求和计算。 ###################################
# super([type[, object-or-type]])
# super() 函数是用于调用父类(超类)的一个方法。 ###################################
# tuple([iterable])
# tuple 函数将列表转换为元组。。 ###################################
# type(object)
# type(name, bases, dict)
print(type(1)) # <class 'int'>
print(type("abc")) # <class 'str'>
# 三个参数
class X():
a = 1
z = type("X",(object,),dict(a=1))
print(z) # <class '__main__.X'> ###################################
# vars([object])
class X(object):
a = 1
print(vars(X)) # {'__module__': '__main__', 'a': 1, '__dict__': <attribute '__dict__' of 'X' objects>, '__weakref__': <attribute '__weakref__' of 'X' objects>, '__doc__': None}
# vars() 函数返回对象object的属性和属性值的字典对象。 ###################################
# zip(*iterables)
a = [1,2,3]
b = [4,5,6]
c = [7,8,9]
print(list(zip(a,b,c))) # [(1, 4, 7), (2, 5, 8), (3, 6, 9)] ###################################
# __import__(name, globals=None, locals=None, fromlist=(), level=0)
# __import__() 函数用于动态加载类和函数 。

逐个实现

参考or转发

 http://www.runoob.com/python/python-built-in-functions.html

Python基础_内置函数的更多相关文章

  1. 十六. Python基础(16)--内置函数-2

    十六. Python基础(16)--内置函数-2 1 ● 内置函数format() Convert a value to a "formatted" representation. ...

  2. 十五. Python基础(15)--内置函数-1

    十五. Python基础(15)--内置函数-1 1 ● eval(), exec(), compile() 执行字符串数据类型的python代码 检测#import os 'import' in c ...

  3. python基础(15):内置函数(一)

    1. 内置函数 什么是内置函数? 就是python给你提供的,拿来直接⽤的函数,比如print,input等等,截⽌到python版本3.6.2 python⼀共提供了68个内置函数.他们就是pyth ...

  4. python基础(内置函数+文件操作+lambda)

    一.内置函数 注:查看详细猛击这里 常用内置函数代码说明: # abs绝对值 # i = abs(-123) # print(i) #返回123,绝对值 # #all,循环参数,如果每个元素为真,那么 ...

  5. Python基础:内置函数

    本文基于Python 3.6.5的标准库文档编写,罗列了英文文档中介绍的所有内建函数,并对其用法进行了简要介绍. 下图来自Python官网:展示了所有的内置函数,共计68个(14*4+12),大家可以 ...

  6. 第六篇:python基础_6 内置函数与常用模块(一)

    本篇内容 内置函数 匿名函数 re模块 time模块 random模块 os模块 sys模块 json与pickle模块 shelve模块 一. 内置函数 1.定义 内置函数又被称为工厂函数. 2.常 ...

  7. Python基础编程 内置函数

    内置函数 内置函数(一定记住并且精通) print()屏幕输出 int():pass str():pass bool():pass set(): pass list() 将一个可迭代对象转换成列表 t ...

  8. python基础(16):内置函数(二)

    1. lamda匿名函数 为了解决⼀些简单的需求⽽设计的⼀句话函数 # 计算n的n次⽅ def func(n): return n**n print(func(10)) f = lambda n: n ...

  9. 学习PYTHON之路, DAY 4 - PYTHON 基础 4 (内置函数)

    注:查看详细请看https://docs.python.org/3/library/functions.html#next 一 all(), any() False: 0, Noe, '', [], ...

随机推荐

  1. js之radio应用实例

    radio和checkbox还有select,可谓是前后端常用三剑客啊!特别是checkbox和select,关于这两个今天不讲,因为在下面这几篇文章,我已经比较详细的讲解了. SpringMVC之a ...

  2. jdbc 和oracle数据库 建立连接

    package jdbc; import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException; ...

  3. P1796 汤姆斯的天堂梦

    题目描述 汤姆斯生活在一个等级为0的星球上.那里的环境极其恶劣,每天12小时的工作和成堆的垃圾让人忍无可忍.他向往着等级为N的星球上天堂般的生活. 有一些航班将人从低等级的星球送上高一级的星球,有时需 ...

  4. iOS在framework中使用CoreData出现崩溃问题及解决方法

    公司项目中有一个功能,保存授权令牌数据.最开始只有一条数据,所以就直接保存在了userdefaults中.后来需要两条数据,还是保存在userdefaults中,其中一条为固定的,另一条不固定可以进行 ...

  5. redis缓存穿透和缓存失效的预防和解决

    缓存穿透: 认识 缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时需要从数据库查询,查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到数据库去查询,造成缓存穿透. 解决办法: 对所 ...

  6. HBase数据存取流程

    一.HBase的特点是什么 1.HBase一个分布式的基于列式存储或者行式存储的数据库,基于hadoop的hdfs存储,zookeeper进行管理. 2.HBase适合存储半结构化或非结构化数据,对于 ...

  7. C 没得写的水文

    引言 - 没得写 最近工作上需要处理事情很多(接手, 维稳, 危机), 还有深入读书计划也提上了日程. 为了每月水经验, 这里带大家写个 C 的多值返回吧 : ) #include <stdio ...

  8. scala字段权限问题

    1.对象私有字段 1)private class Counter(num: Int) { private var value = 0 def increment() = { value += 1 } ...

  9. Python学习之迭代器和生成器

    那么首先什么是迭代器和生成器呢? 迭代器即迭代的工具,那么什么又是迭代呢?所谓迭代:迭代是一个重复的过程,每次重读即一次迭代,并且每次迭代的结果都是下一次迭代的初始值.例: l=[1,2,3] cou ...

  10. Nginx-------Nginx的安装和多域名配置

    Nginx安装 centos6.x yum默认没有nginx的软件包 安装方式: 到nginx下载页面http://nginx.org/en/linux_packages.html#stable,复制 ...