What is built-in functions?
------------------------
Kind Regards,
Yonggang Meng
It means that the functions are built into python interpreter itself, so you don't need to import anything.
Here is the list:
abs(x) : return the absolute value of the x, x can be a plain or long integer or float point number or complex number.
>>> abs(-1),abs(1),abs(0.1),abs(-0.1),abs(3+4j)
(1, 1, 0.10000000000000001, 0.10000000000000001, 5.0) #0.1000....1 is the format computer use, abs(0.1)==0.1, true
all(iterable) : check whether each item in iterable is true.
>>> all([1,0])
False
>>> all([1,.0])
False
>>> all([1,'.0'])
True
any(iterable) : if any item in iterable is true, the result is true.
>>> any([1,.0])
True
>>> any([0,.0])
False
>>> any([0,'.0'])
True
basestring() : the type is abstract type, can not be called or instantiated. It's the superclass of str and unicode, It can be check whether an object is an instance of str or unicode,
>>> a=''
>>> isinstance(a,basestring) # isinstance is another built-in function
True
>>> b=0
>>> isinstance(b,basestring)
False
bin(x) : convert an integer number to a binary string, if x is not an integer, x object must have __index__ () method that can return an integer.
>>> bin(2)
'0b10'
>>> bin(10)
'0b1010'
>>> bin(0)
'0b0'
>>> a=''
>>> bin(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an index
>>> bin(1.0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an index
>>>
>>> class y:
... def __index__(self):
... return 3
...
>>> d=y()
>>> bin(d)
'0b11'
>>>
bool([x]) : convert a value to a boolean. If x is false or omitted, it returns false, otherwise, it returns true. the bool is a subclass of int and can not be subclassed further.
It has only two instances false and true.
>>> bool(1)
True
>>> bool(-1)
True
>>> bool(0)
False
>>> bool('')
False
>>> bool()
False
>>> bool(.)
File "<stdin>", line 1
bool(.)
^
SyntaxError: invalid syntax
To be continued
Kind Regards,
Yonggang Meng
0 comments:
Post a Comment