首页 > Python基础教程 >
-
Python中的内置函数
abs()
Return the absolute value of a number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned.
注意复数的绝对值:
>>> a = 1 + 1j
>>> abs(a)
1.4142135623730951
********************************************************************************************
dict()
class dict(object)
创建字典方式一:
| dict() -> new empty dictionary
创建字典方式二:
| dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs
>>> dict(([1, 2],[3, 4]))
{1: 2, 3: 4}
创建字典方式三:
| dict(iterable) -> new dictionary initialized as if via:
| d = {}
| for k, v in iterable:
| d[k] = v
创建字典方式四:
| dict(**kwargs) -> new dictionary initialized with the name=value pairs
| in the keyword argument list. For example: dict(one=1, two=2)
|
>>> dict(one=1,two=2)
{'one': 1, 'two': 2}
| Methods defined here:
| 方法一:
| __contains__(self, key, /)
| True if D has a key k, else False.
| 方法二:
| __delitem__(self, key, /)
| Delete self[key].
| 方法三:
| __eq__(self, value, /)
| Return self==value.
| 方法四:
| __ge__(self, value, /)
| Return self>=v
这里是比较运算符
| 方法五:
| __getattribute__(self, name, /)
| Return getattr(self, name).
********************************************************************************************
help()
Invoke the built-in help system. (This function is intended for interactive use.) If no argument is given, the interactive help system starts on the interpreter console. 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. If the argument is any other kind of object, a help page on the object is generated.
This function is added to the built-in namespace by the site module.
Changed in version 3.4: Changes to pydoc and inspect mean that the reported signatures for callables are now more comprehensive and consistent.
********************************************************************************************
min()
min(iterable, *[, default=obj, key=func]) -> value
min(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its smallest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the smallest argument.
********************************************************************************************
setattr()
setattr(obj, name, value, /)
Sets the named attribute on the given object to the specified value.
setattr(x, 'y', v) is equivalent to ``x.y = v''
********************************************************************************************
all()
all(iterable, /)
Return True if bool(x) is True for all values x in the iterable.
If the iterable is empty, return True.
********************************************************************************************
dir()
dir(...)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
如果没有参数数将返回当前空间的变量名&方法名
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module's attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.
********************************************************************************************
hex()
hex(number, /)
Return the hexadecimal representation of an integer.
>>> hex(12648430)
'0xc0ffee'
********************************************************************************************
next()
next(...)
next(iterator[, default])
Return the next item from the iterator. If default is given and the iterator
is exhausted, it is returned instead of raising StopIteration.
********************************************************************************************
slice()
>>> a = slice(0, 4, 2)
>>> b = [3, 4,2, 3, 4, 2, 3, 2, 4, 4,5,6 ,6]
>>> b[a]
[3, 2]
********************************************************************************************
any()
any(iterable, /)
Return True if bool(x) is True for any x in the iterable.
If the iterable is empty, return False.
********************************************************************************************
divmod()
>>> divmod(9, 5)
(1, 4)
********************************************************************************************
id()
>>> a = 2
>>> b = 2
>>> id(a)
4453115248
>>> id(b)
4453115248
>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> id(a)
4461028744
>>> id(b)
4461057736
********************************************************************************************
object()
class object
| The most base type
********************************************************************************************
sorted()
sorted(iterable, /, *, key=None, reverse=False)
Return a new list containing all items from the iterable in ascending order.
这个函数会返回一个新的list,这一点跟sort不同
A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.
********************************************************************************************
ascii()
ascii(obj, /)
Return an ASCII-only representation of an object.
这是嘛意思呢?
>>> ascii(3)
'3'
>>> ascii('a')
"'a'"
As repr(), return a string containing a printable representation of an
object, but escape the non-ASCII characters in the string returned by
repr() using \x, \u or \U escapes. This generates a string similar
to that returned by repr() in Python 2.
********************************************************************************************
enumerate()
class enumerate(object)
| enumerate(iterable[, start]) -> iterator for index, value of iterable
|
| Return an enumerate object. iterable must be another object that supports
| iteration. The enumerate object yields pairs containing a count (from
| start, which defaults to zero) and a value yielded by the iterable argument.
| enumerate is useful for obtaining an indexed list:
| (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
|
| Methods defined here:
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __iter__(self, /)
| Implement iter(self).
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| __next__(self, /)
********************************************************************************************
input()
input()输入的是str类型
********************************************************************************************
oct()
八进制
********************************************************************************************
staticmethod()
静态方法
********************************************************************************************
bin()
********************************************************************************************
eval()
>>> eval("print('hello world')")
hello world
********************************************************************************************
int()
>>> s = '4.333'
>>> int(eval(s))
4
********************************************************************************************
open()
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, clo
sefd=True, opener=None)
Open file and return a stream. Raise IOError upon failure.
********************************************************************************************
str()
Last login: Tue Jun 12 11:51:19 on ttys000
hujinzhaodeMacBook-Air:~ andy$ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 3
>>> str(a)
'3'
>>> help(str)
| S.upper() -> str
|
| Return a copy of S converted to uppercase.
|
| zfill(...)
| S.zfill(width) -> str
|
| Pad a numeric string S with zeros on the left, to fill a field
| of the specified width. The string S is never truncated.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| maketrans(x, y=None, z=None, /)
| Return a translation table usable for str.translate().
|
| If there is only one argument, it must be a dictionary mapping Unicode
| ordinals (integers) or characters to Unicode ordinals, strings or None.
| Character keys will be then converted to ordinals.
| If there are two arguments, they must be strings of equal length, and
| in the resulting dictionary, each character in x will be mapped to the
| character at the same position in y. If there is a third argument, it
| must be a string, whose characters will be mapped to None in the result.
********************************************************************************************
bool()
>>> bool()
False
********************************************************************************************
exec()
exec(source, globals=None, locals=None, /)
Execute the given source in the context of globals and locals.
The source may be a string representing one or more Python statements
or a code object as returned by compile().
The globals must be a dictionary and locals can be any mapping,
defaulting to the current globals and locals.
If only globals is given, locals defaults to it.
********************************************************************************************
isinstance()
>>> class A():
... pass
...
>>> a = A()
>>> isinstance(a, A)
True
********************************************************************************************
ord()
>>> ord('a')
97
********************************************************************************************
sum()
********************************************************************************************
bytearray()
>>> help(bytearray)
| zfill(...)
| B.zfill(width) -> copy of B
|
| Pad a numeric string B with zeros on the left, to fill a field
| of the specified width. B is never truncated.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| maketrans(frm, to, /)
| Return a translation table useable for the bytes or bytearray translate method.
|
| The returned table will be one where each byte in frm is mapped to the byte at
| the same position in to.
|