python内置函数学习笔记

# coding:utf8

# 1.abs(x) 求一个数的绝对值
"""Return the absolute value of a number. The argument may be a plain or long integer or a floating point number. If the argument is a complex number, its magnitude is returned."""

from ast import operator
from curses import raw
from filecmp import cmp
from imp import reload
import imp
from re import X
from telnetlib import DO
from turtle import setx


a = -10**10
print(abs(a))

# 2.all(iterable)  只有全部为True才返回True,否则返回Flase
"""
Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to:
底层实现
def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True
"""
print(all([1, 0, 23, 233, False]))  # Flase

# 3.any(iterable)    只要有一个为True就返回Ture,否则返回False
"""
Return True if any element of the iterable is true. If the iterable is empty, return False. 
底层实现
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
"""
print(any([12, 0, 23, '', 'awefaw']))  # True

# 4.basestring() str和unicode的超类,是一个抽象类型,不能调用和实例化对象 可以用于检测
# 一个对象是不是str或者unicode的实例
"""This abstract type is the superclass for str and unicode. It cannot be called or instantiated, but it can be used to test whether an object is an instance of str or unicode. isinstance(obj, basestring) is equivalent to isinstance(obj, (str, unicode))."""
print(isinstance("hello world", basestring))
print(isinstance(1111, basestring))

# 5.bin(x) 返回一个数的二进制 如果x不是一个int对象,那么它必须定义__index__()方法来返回一个整数
"""Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer."""
print(bin(10))


class MyInt:
    def __index__(self):
        return id(self)


print(bin(MyInt()))

# 6.bool() 返回一个值对应的布尔值 bool也是一个类,它是int的一个子类,但是它不能用来实例化对象,它的对象只有Ture和False
"""Return a Boolean value, i.e. one of True or False. x is converted using the standard truth testing procedure. If x is false or omitted, this returns False; otherwise it returns True. bool is also a class, which is a subclass of int. Class bool cannot be subclassed further. Its only instances are False and True."""
print(bool("123"))

# 7.bytearray()  返回一个字节数组
arr_li = bytearray("hello world你好")
print(arr_li)
print(arr_li[0])
str_u = u'你好,world'  # unicode
str_u_byte = bytearray(str_u, encoding='utf8')
print(str_u.encode('utf8'))
print(str_u_byte.decode('utf8'))
print(str_u)

# 8.callable(obj) 判断一个对象是否是可调用的
print(callable(1))
print(callable("123"))
print(callable(list))

# 9.chr(i) 将码值(0~255的ASCII码值)转成码符
"""Return a string of one character whose ASCII code is the integer i. For example, chr(97) returns the string 'a'. This is the inverse of ord(). The argument must be in the range [0..255], inclusive; ValueError will be raised if i is outside that range. See also unichr()."""
print(chr(97))

# 10.classmethod(fuc)  将一个函数转成一个类方法
"""Return a class method for function.
class C(object):
    @classmethod
    def f(cls, arg1, arg2, ...):
        ...
"""


# 10.cmp(x,y) 丢弃

# 11.compile 将一个字符串编译为字节代码
'''
compile(source, filename, mode[, flags[, dont_inherit]])
    source -- 字符串或者AST(Abstract Syntax Trees)对象。。
    filename -- 代码文件名称,如果不是从文件读取代码则传递一些可辨认的值。
    mode -- 指定编译代码的种类。可以指定为 exec, eval, single。
    flags -- 变量作用域,局部命名空间,如果被提供,可以是任何映射对象。。
    flags和dont_inherit是用来控制编译源码时的标志

'''
c = compile('for i in range(10):print(i)', '', 'exec')
print(c)
exec(c)
eval(c)

# 12.complex()  返回一个复数对象
'''class complex([real[, imag]])
Return a complex number with the value real + imag*1j or convert a string or number to a complex number. If the first parameter is a string, it will be interpreted as a complex number and the function must be called without a second parameter. The second parameter can never be a string. Each argument may be any numeric type (including complex). If imag is omitted, it defaults to zero and the function serves as a numeric conversion function like int(), long() and float(). If both arguments are omitted, returns 0j.'''
c = complex("10+1j")
c = complex(10, 5)  # real,imag
print(c, c.real, c.imag)

# 13.delattr(obj,name) 删除对象obj的属性name 等价于 del obj.name


class A:
    def __init__(self):
        self.name = "hello"


a = A()
print(getattr(a, 'name'))
print(a.__dict__)
delattr(a, 'name')
print(a.__dict__)

# 14.dict(**kwarg) 创建一个字典
d = dict(a="a", b="b", c="c")
print(d)


# 15.dir([obj]) 不带参数返回本地作用域名称列表,如果有一个参数,尝试返回该对象的有效属性
'''Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.'''
print(dir(a))


# 16.divmod(a,b) 返回aheb的商和余数
'''Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using long division. With mixed operand types, the rules for binary arithmetic operators apply. For plain and long integers, the result is the same as (a // b, a % b). For floating point numbers the result is (q, a % b), where q is usually math.floor(a / b) but may be 1 less than that. In any case q * b + a % b is very close to a, if a % b is non-zero it has the same sign as b, and 0 <= abs(a % b) < abs(b).'''
res = divmod(4.3, 2.32)
print(res)


# 17.enumerate(sequence,start=0) 返回一个枚举对象
'''Return an enumerate object. sequence must be a sequence, an iterator, or some other object which supports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over sequence:'''
seq = ['h', 'e', 'l', 'l', 'o']
e = enumerate(seq, start=1)
print(list(e))

# 18.eval()传入一个表达式,返回表达式的值
print(eval('1*6+3-2'))

# 19.execfile(filename,[globals,[locals]])   python3已移除
'''This function is similar to the exec statement, but parses a file instead of a string.'''
# execfile('lll01_内置函数.py')
# execfile('demo.py')


# 20.file 构造函数 已丢弃


# 21.filter(func,iterable) 过滤器,迭代iterable中的每一个值,将它们传给func,如果返回True就保留
'''Construct a list from those elements of iterable for which function returns true.'''
print(filter(lambda a: a % 2, range(10)))  # 获取10以内的奇数


# 22.float(x) 将一个数或则字符串x转成一个浮点数
'''Return a floating point number constructed from a number or string x.

If the argument is a string, it must contain a possibly signed decimal or floating point number, possibly embedded in whitespace. The argument may also be [+|-]nan or [+|-]inf. Otherwise, the argument may be a plain or long integer or a floating point number, and a floating point number with the same value (within Python’s floating point precision) is returned. If no argument is given, returns 0.0.'''
print(float(1))
print(float('1212'))


# 23.format(value,[format_spec]) 字符串格式化
s = 'hello,{}'.format('Tom')
print(s)

# 24.frozenset([iterable]) 返回一个frozenset(不可变集合)
'''Return a new frozenset object, optionally with elements taken from iterable.'''
fs = frozenset(range(10))
print(fs)
print(1 in fs)


# 25.getattr(obj,name) 获取对象obj的属性name的值
'''Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.'''
a = A()
print(getattr(a, 'name'))


# 26.globals()  返回当前全局符号表
'''Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).'''
print(globals())


# 27.hasattr(obj,name) 判断对象obj是否有属性name
'''The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not.'''
print(hasattr(a, 'age'))


# 28.hash(obj) 返回对象obj的hash值 数值(值相等,但类型不同)的hash值是相等的
'''Return the hash value of the object (if it has one). Hash values are integers. They are used to quickly compare dictionary keys during a dictionary lookup. Numeric values that compare equal have the same hash value (even if they are of different types, as is the case for 1 and 1.0).'''
print(hash(a))
print(hash(111) == hash(111.000000))

# 29.help(obj)  交互式的返回obj的相关帮组文档
'''If the argument is a string, then the string is looked up as the name of a module, function, class, method, keyword, or documentation topic, and a help page is printed on the console. '''
# help(a)


# 30.hex(x) 将一个整数转成一个16进制的数
print(hex(12))


# 31.id(obj) 返回对象obj的”唯一标识“
'''Return the “identity” of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.'''
# CPython implementation detail: This is the address of the object in memory. --- 在cpython解释器中,这里对应的是对象的内存地址
print(id("hello"))


# 32.input(prompt) 输入
# input('请输入你的选择:')


# 33.int(x=0)  将x转成一个int对象
print(int(1111))
print(int('1111'))
print(int(123.45))


# 34.int(x,base=10) x必须为一个字符串或unicode对象 将base进制的数转成对应的10进制数
'''The default base is 10. The allowed values are 0 and 2–36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O/0, or 0x/0X, as with integer literals in code. Base 0 means to interpret the string exactly as an integer literal, so that the actual base is 2, 8, 10, or 16.'''
print(int('1234', base=8))

# 35.isinstance(obj,classinfo) 判断对象obj是不是classinfo的实例,或者子类的实例
'''Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. Also return true if classinfo is a type object (new-style class) and object is an object of that type or of a (direct, indirect or virtual) subclass thereof. If object is not a class instance or an object of the given type, the function always returns false. If classinfo is a tuple of class or type objects (or recursively, other such tuples), return true if object is an instance of any of the classes or types. If classinfo is not a class, type, or tuple of classes, types, and such tuples, a TypeError exception is raised.'''


class B(A):
    pass


b = B()
print(isinstance(b, A))
print(isinstance(b, B))

# 36.iter(o,[sentinel]) 如果没有第二个参数,o必须为可迭代对象; 如果有第二个参数,o必须是一个可调用的对象 通过o不断产出值,如果值等于sentinel就停止
'''
Return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, o must be a collection object which supports the iteration protocol (the __iter__() method), or it must support the sequence protocol (the __getitem__() method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised. If the second argument, sentinel, is given, then o must be a callable object. The iterator created in this case will call o with no arguments for each call to its next() method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.

'''

# 一行一行的读取文件内容
with open('demo.py') as fp:
    for line in iter(fp.readline, ''):  # 遇到空字符串就说明读取完了
        print(line)


# 37.len(obj) 返回对象obj的长度
print(len("hello world!"))


# 38.list(iterable) 将可迭代对象转成列表
print(list('hello world!'))

# 39.locals() 返回当当前局部标识符表 只能在函数中被调用 对它的修改是不起作用的
'''Update and return a dictionary representing the current local symbol table. Free variables are returned by locals() when it is called in function blocks, but not in class blocks.  The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter.'''


def fun():
    a = 1
    b = '2'
    print(locals())
    locals()['a'] = 111  # 修改无效
    print(locals())


fun()


# 40.long() python3丢弃

# 41.map(func,iterable,...) 将func作用到iterable的每一个元素中,并将返回结果以列表的形式返回
'''Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended with None items. If function is None, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.'''
res = map(lambda *a: sum(a), [1, 2, 3], [4, 5, 6])  # 求序列对应位置的和
print(res)
res = map(lambda *a: sum(a), [1, 2, 3], [4, 5, 6], [7, 8, 9])
print(res)


# 42.max([*]iterable,[key])  返回最大值 key用于指定比较方法
'''Return the largest item in an iterable or the largest of two or more arguments.

If one positional argument is provided, iterable must be a non-empty iterable (such as a non-empty string, tuple or list). The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned.

The optional key argument specifies a one-argument ordering function like that used for list.sort(). The key argument, if supplied, must be in keyword form (for example, max(a,b,c,key=func)).'''
print(max({1, 2, 3, 4, -23, -1, 23, 343, -999}, key=abs))  # 按照绝对值比较,返回最大值


# 43.memoryview(obj) 将对象obj转成一个memory view对象
s = 'hello world'
mv = memoryview(s)
print(mv)


# 44.min([*]iterable,[key])  返回最小值 key用于指定比较方法
print(min({1, 2, 3, 4, -23, -1, 23, 343, -999}, key=abs))  # 按照绝对值比较,返回最小值


# 45.next(iterator,[default]) 返回迭代器的下一个值,如果没有就返回default
'''Retrieve the next item from the iterator by calling its next() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.'''
it = (i for i in range(10))  # 生成器也是迭代器
while True:
    s = next(it, 666)
    print(s)
    if s == 666:
        break


# 46.oct(x) 将一个整数转成一个八进制数
'''Convert an integer number (of any size) to an octal string. The result is a valid Python expression.'''
print(oct(100))

# 47.open(name[,mode,[buffering]]) 打开文件
with open('lll01_内置函数.py', 'r') as f:
    print(f.read())


# 48.ord(c) 返回一个字符对应的unicode码值
print(ord('a'))

# 49.pow(x,y[,z]) 返回x**y的值,如果z被传入,返回 (x**y)%z的值
print(pow(2, 3, 6))

# 50.print(*object,sep='',end='\n',file=sys.stdout) 打印输出
print('#hello wolrd!')


# 51.property([fget[, fset[, fdel[, doc]]]]) 特性 常用于修饰限定某一个属性
'''
如果没有fset,那么property所修饰的属性就是一个只读变量

'''


class A:

    def __init__(self):
        self.x = None

    def setx(self, x):
        self.x = x

    def getx(self):
        return self.x

    def delx(self):
        del self.x

    x = property(getx, setx, delx, "this is a attribte of instance of A.")


a = A()
a.x = 6666
print(a.x)


# 52.range(start,stop[,step]) 返回一个等差数列 start指定开始值,默认为0 stop指定结束值,step指定步长 左闭右开原则
aaa = range(1, 10, 2)
print(aaa)

# 53.raw_input([prompt]) 读入一行内容
# s=raw_input('--->')
# print(s)


# 54.reduce(function,iterable[,intializer]) 累积操作,将多个值最终变成一个值
'''Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned. Roughly equivalent to:

def reduce(function, iterable, initializer=None):
    it = iter(iterable)
    if initializer is None:
        try:
            initializer = next(it)
        except StopIteration:
            raise TypeError('reduce() of empty sequence with no initial value')
    accum_value = initializer
    for x in it:
        accum_value = function(accum_value, x)
    return accum_value'''
# 求1到100的和
print(reduce(lambda a, b: a+b, range(1, 101)))


# 55.reload(module) 重新加载一个模块 一般用于版本兼容
'''Reload a previously imported module. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter. The return value is the module object (the same as the module argument).'''
try:
    import csv
except:
    reload(csv)


# 56.repr(object) 返回一个对象的“描绘”  可用于打印显示对象的相关信息
'''Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary function. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__() method.'''
print(repr(A()))

# 57.reversed(seq) 翻转序列seq,并以迭代器的形式返回
'''Return a reverse iterator. seq must be an object which has a __reversed__() method or supports the sequence protocol (the __len__() method and the __getitem__() method with integer arguments starting at 0).'''
a = [1, 2, 3, 4]
b = reversed(a)
print(id(a), a)
print(id(b), b)


# 58.round(number[, ndigits]) 四舍五入 ndigits表示保留的小数位
'''Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0 (so, for example, round(0.5) is 1.0 and round(-0.5) is -1.0).'''
print(round(10.2323434, 2))
print(round(-7.232323723223232, 5))


# 59.class set([iterable]) 从iterable中获取元素,将其转成一个集合
'''Return a new set object, optionally with elements taken from iterable. set is a built-in class. See set and Set Types — set, frozenset for documentation about this class.'''
print(set(range(10)))


# 60.setattr(object, name, value) 给对象object设置属性name的值为value(如果object本身没有这个属性,就是新增属性并赋值)
'''This is the counterpart of getattr(). The arguments are an object, a string and an arbitrary value. The string may name an existing attribute or a new attribute. The function assigns the value to the attribute, provided the object allows it. For example, setattr(x, 'foobar', 123) is equivalent to x.foobar = 123.'''
a = A()
setattr(a, 'x', 6666)
print(a.x)


# 61.class slice(start, stop[, step]) 返回一个切片对象
'''Return a slice object representing the set of indices specified by range(start, stop, step). The start and step arguments default to None. Slice objects have read-only data attributes start, stop and step which merely return the argument values (or their default). They have no other explicit functionality; however they are used by Numerical Python and other third party extensions. Slice objects are also generated when extended indexing syntax is used. For example: a[start:stop:step] or a[start:stop, i]. See itertools.islice() for an alternate version that returns an iterator.'''
a = list(range(10))
a_slice = slice(0, 9, 3)  # 索引范围:[0,9) a步长为3进行取值
print(a[0:9:3])
print(a[a_slice])


# 62.sorted(iterable[, cmp[, key[, reverse]]]) 对iterable中的元素进行排序,并以列表的形式返回 cmp指定排序规则,key指定排序的参考值,

# a=[1,-2,32,4,-999,888]
# print(sorted(a,cmp=lambda x,y:cmp(abs(x),abs(y))))


# 63.staticmethod(function) 对于不会用到类的属性但需要定义在类里面的方法可以使用它  能够被所属类直接调用,也可以被所属类的实例直接调用
'''A static method can be called either on the class (such as C.f()) or on an instance (such as C().f()).

Static methods in Python are similar to those found in Java or C++. Also see classmethod() for a variant that is useful for creating alternate class constructors.'''


class B:
    @staticmethod
    def say(): print('hello')

    def __str__(self):
        return 'helo str!'

    def __repr__(self):
        return 'hello repr!'


B.say()
B().say()


# 64.class str(object='') 返回调用对应的__str__方法,如果没有定义,就调用__repr__方法,如果都没有调用,就返回obj的描述
'''Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.'''
print(str(B()))


# 65.sum(iterable[, start]) 求iterable中所有元素之和 start指定初始值,默认为0 即 result=sum(iterable)+start
'''Sums start and the items of an iterable from left to right and returns the total. start defaults to 0. The iterable’s items are normally numbers, and the start value is not allowed to be a string.'''
print(sum([1, 2, 3, 4, 5], 1))


# 66.class super([type[, object-or-type]]) 用于调用超类中的方法 如果是多继承下,多个超类中有相同的方法名,具体调用哪一个要参考__mro__
# class Animal:
#     def __init__(self):
#         print('我是一个动物!')

# class Dog(Animal):
#     def __init__(self):
#         super().__init__(self)
#         print('我是一只狗🐶!')

# Dog()


# 67.tuple([iterable]) 将iterable转成一个元组并返回
'''Return a tuple whose items are the same and in the same order as iterable’s items. iterable may be a sequence, a container that supports iteration, or an iterator object. If iterable is already a tuple, it is returned unchanged. For instance, tuple('abc') returns ('a', 'b', 'c') and tuple([1, 2, 3]) returns (1, 2, 3). If no argument is given, returns a new empty tuple, ().'''
print(tuple(range(10)))


# 68.class type(object) 返回object对象的类型
'''With one argument, return the type of an object.'''
print(type('hello'))


# 69.unichr(i) f返回码值i对应的码符
'''Return the Unicode string of one character whose Unicode code is the integer i. '''
print(chr(97))


# 70.unicode(object='') python3已丢弃


# 71.vars([object])  返回对象object的所有属性
'''Return the __dict__ attribute for a module, class, instance, or any other object with a __dict__ attribute.'''
print(vars())


# 72.xrange python3已丢弃


# 73.zip([iterable, ...]) 并行访问多个iterable中相同位置的元素,并以一个元组的形式返回
'''This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence. When there are multiple arguments which are all of the same length, zip() is similar to map() with an initial argument of None. With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list.'''
a = range(10)
b = range(10, 20)
c = range(20, 30)
for i in zip(a, b, c):
    print(i)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值