search
HomeBackend DevelopmentPython TutorialPython全栈之路系列之Python3内置函数

Python全栈之路系列之Python3内置函数

Feb 09, 2017 am 10:45 AM
pythonfunction

The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.

Built-in Functions

abs() dict() help() min() setattr() all() dir()
hex() next() slice() any() pmod() id() object()
sorted() ascii() enumerate() input() oct() staticmethod() bin()
eval() int() open() str() bool() 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() delattr() hash() memoryview() set()

官方介绍:http://www.php.cn/

内置函数详解

abs(x)

返回数字的绝对值,参数可以是整数或浮点数,如果参数是复数,则返回其大小。

# 如果参数是复数,则返回其大小。
 >>> abs(-25)
25
 >>> abs(25)
25

all(iterable)

all()会循环括号内的每一个元素,如果括号内的所有元素都是真的,或者如果iterable为空,则返回True,如果有一个为假的那么就返回False

>>> all([])
True
>>> all([1,2,3])
True
>>> all([1,2,""])
False
# 如果有一个为假,则都为假
>>> all([1,2,None])
False

假的参数有:False0None""[](){}等,查看一个元素是否为假可以使用bool进行查看。

any(iterable)

循环元素,如果有一个元素为真,那么就返回True,否则就返回False

 >>> any([0,1])
True
 >>> any([0])
False

ascii(object)

在对象的类中寻找__repr__方法,获取返回值

 >>> class Foo:
 ...  def __repr_(self):
 ...     return "hello"
 ...
 >>> obj = Foo()
 >>> r = ascii(obj)
 >>> print(r)
# 返回的是一个可迭代的对象
<__main__.Foo object at 0x000001FDEE13D320>

bin(x)

将整数x转换为二进制字符串,如果x不为Python中int类型,x必须包含方法__index__()并且返回值为integer

# 返回一个整数的二进制
 >>> bin(999)
'0b1111100111'
# 非整型的情况,必须包含__index__()方法切返回值为integer的类型
 >>> class myType:
 ...   def __index__(self):
 ...       return 35
 ...
 >>> myvar = myType()
 >>> bin(myvar)
'0b100011'

bool([x])

查看一个元素的布尔值,非真即假

 >>> bool(0)
False
 >>> bool(1)
True
 >>> bool([1])
True
 >>> bool([10])
True

bytearray([source [, encoding [, errors]]])

返回一个byte数组,Bytearray类型是一个可变的序列,并且序列中的元素的取值范围为 [0 ,255]。

source参数:

  1. 如果source为整数,则返回一个长度为source的初始化数组;

  2. 如果source为字符串,则按照指定的encoding将字符串转换为字节序列;

  3. 如果source为可迭代类型,则元素必须为[0 ,255]中的整数;

  4. 如果source为与buffer接口一致的对象,则此对象也可以被用于初始化bytearray.。

 >>> bytearray(3)
bytearray(b'\x00\x00\x00')

bytes([source[, encoding[, errors]]])

 >>> bytes("asdasd",encoding="utf-8")
b'asdasd'

callable(object)

返回一个对象是否可以被执行

 >>> def func():
 ...  return 123
 ...
 >>> callable(func)
True
 >>> func = 123
 >>> callable(func)
False

chr(i)

返回一个数字在ASCII编码中对应的字符,取值范围256个

 >>> chr(66)
'B'
 >>> chr(5)
'\x05'
 >>> chr(55)
'7'
 >>> chr(255)
'\xff'
 >>> chr(25)
'\x19'
 >>> chr(65)
'A'

classmethod(function)

返回函数的类方法

compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)

把字符串编译成python可执行的代码

 >>> str = "for i in range(0,10): print(i)"
 >>> c = compile(str,'','exec')
 >>> exec(c)
0
1
2
3
4
5
6
7
8
9

complex([real[, imag]])

创建一个值为real + imag * j的复数或者转化一个字符串或数为复数。如果第一个参数为字符串,则不需要指定第二个参数

 >>> complex(1, 2)
(1+2j)
# 数字
 >>> complex(1)
(1+0j)
# 当做字符串处理
 >>> complex("1")
(1+0j)
# 注意:这个地方在“+”号两边不能有空格,也就是不能写成"1 + 2j",应该是"1+2j",否则会报错
 >>> complex("1+2j")
(1+2j)

delattr(object, name)

删除对象的属性值

>>> class cls:
...   @classmethod
...   def echo(self):
...     print('CLS')
... 
>>> cls.echo()
CLS
>>> delattr(cls, 'echo')
>>> cls.echo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'cls' has no attribute 'echo'

dict(**kwarg)

创建一个数据类型为字典

 >>> dic = dict({"k1":"123","k2":"456"})
 >>> dic
{'k1': '123', 'k2': '456'}

dir([object])

返回一个对象中中的所有方法

 >>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce\_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

pmod(a, b)

返回的是a//b(除法取整)以及a对b的余数,返回结果类型为tuple

 >>> pmod(10, 3)
(3, 1)

enumerate(iterable, start=0)

为元素生成下标

 >>> li = ["a","b","c"]
 >>> for n,k in enumerate(li):
 ...  print(n,k)
 ...
0 a
1 b
2 c

eval(expression, globals=None, locals=None)

把一个字符串当作一个表达式去执行

 >>> string = "1 + 3"
 >>> string
'1 + 3'
 >>> eval(string)
4

exec(object[, globals[, locals]])

把字符串当作python代码执行

 >>> exec("for n in range(5): print(n)")
0
1
2
3
4

filter(function, iterable)

筛选过滤,循环可迭代的对象,把迭代的对象当作函数的参数,如果符合条件就返回True,否则就返回False

 >>> def func(x):
 ...  if x == 11 or x == 22:
 ...    return True
 ...
 >>> ret = filter(func,[11,22,33,44])
 >>> for n in ret:
 ...  print(n)
 ...
11
22
>>> list(filter((lambda x: x > 0),range(-5,5)))
[1, 2, 3, 4]

float([x])

将整数和字符串转换成浮点数

 >>> float("124")
124.0
 >>> float("123.45")
123.45
 >>> float("-123.34")
-123.34

format(value[, format_spec])

字符串格式化

详键:http://www.php.cn/

frozenset([iterable])

frozenset是冻结的集合,它是不可变的,存在哈希值,好处是它可以作为字典的key,也可以作为其它集合的元素。缺点是一旦创建便不能更改,没有add,remove方法。

getattr(object, name[, default])

返回对象的命名属性的值,name必须是字符串,如果字符串是对象属性之一的名称,则结果是该属性的值。

globals()

获取或修改当前文件内的全局变量

>>> a = "12"
>>> bsd = "54asd"
>>> globals()
{'__doc__': None, 'a': '12', '__loader__': <class &#39;_frozen_importlib.BuiltinImporter&#39;>, 'bsd': '54asd', '__builtins__': <module &#39;builtins&#39; (built-in)>, 'n': '__doc__', '__name__': '__main__', '__spec__': None, '__package__': None}

hasattr(object, name)

参数是一个对象和一个字符串,如果字符串是对象的某个属性的名称,则结果为True,否则为False。

hash(object)

返回一个对象的hash值

 >>> a = "asdadasdwqeq234sdfdf"
 >>> hash(a)
5390438057823015497

help([object])

查看一个类的所有详细方法,或者查看某个方法的使用详细信息

 >>> help(list)
Help on class list in module __builtin__:

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items
 |  
 |  Methods defined here:
 |  
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 |  
 |  __contains__(...)
 |      x.__contains__(y) <==> y in x
 |  
 |  __delitem__(...)
 |      x.__delitem__(y) <==> del x[y]
 |  
 |  __delslice__(...)
 |      x.__delslice__(i, j) <==> del x[i:j]
 |      
 |      Use of negative indices is not supported.
..........

hex(x)

获取一个数的十六进制

 >>> hex(13)
'0xd'

id(object)

返回一个对象的内存地址

 >>> a = 123
 >>> id(a)
1835400816

input([prompt])

交互式输入

 >>> name = input("Pless your name: ")
Pless your name: ansheng
 >>> print(name)
ansheng

int(x, base=10)

获取一个数的十进制

 >>> int("31")
31

也可以作为进制转换

 >>> int(10)
10
 >>> int('0b11',base=2)
3
 >>> int('11',base=8)
9
 >>> int('0xe',base=16)
14

isinstance(object, classinfo)

判断对象是否是这个类创建的

>>> li = [11,22,33]
>>> isinstance(li,list)
True

issubclass(class, classinfo)

查看一个对象是否为子类

iter(object[, sentinel])

创建一个可迭代的对象

 >>> obj = iter([11,22,33,44])
 >>> obj
<list_iterator object at 0x000002477DB25198>
 >>> for n in obj:
 ...  print(n)
 ...
11
22
33
44

len(s)

查看一个对象的长度

 >>> url="ansheng.me"
 >>> len(url)
10

list([iterable])

创建一个数据类型为列表

 >>> li = list([11,22,33,44])
 >>> li
[11, 22, 33, 44]

locals()

返回当前作用域的局部变量,以字典形式输出

 >>> func()
 >>> def func():
 ...  name="ansheng"
 ...  print(locals())
 ...
 >>> func()
{'name': 'ansheng'}

map(function, iterable,  ...)

对一个序列中的每一个元素都传到函数中执行并返回

>>> list(map((lambda x : x +10),[1,2,3,4]))
[11, 12, 13, 14]

max(iterable, *[, key, default])

max(arg1, arg2, *args[, key])

取一个对象中的最大值

 >>> li = list([11,22,33,44])
 >>> li = [11,22,33,44]
 >>> max(li)
44

memoryview(obj)

返回对象obj的内存查看对象

 >>> import struct
 >>> buf = struct.pack("i"*12, *list(range(12)))
 >>> x = memoryview(buf)
 >>> y = x.cast('i', shape=[2,2,3])
 >>> print(y.tolist())
[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]

min(iterable, *[, key, default])

min(arg1, arg2, *args[, key])

取一个对象中的最小值

 >>> li = list([11,22,33,44])
 >>> li = [11,22,33,44]
 >>> min(li)
11

next(iterator[, default])

每次只拿取可迭代对象的一个元素

 >>> obj = iter([11,22,33,44])
 >>> next(obj)
11
 >>> next(obj)
22
 >>> next(obj)
33
 >>> next(obj)
44
 >>> next(obj)
 # 如果没有可迭代的元素了就会报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

object

返回一个新的无特征对象

oct(x)

获取一个字符串的八进制

 >>> oct(13)
'0o15'

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

文件操作的函数,用来做文件操作的

 # 打开一个文件
- >>> f = open("a.txt","r")

ord(c)

把一个字母转换为ASCII对对应表中的数字

 >>> ord("a")
97
 >>> ord("t")
116

pow(x, y[, z])

返回一个数的N次方

 >>> pow(2, 10)
1024
 >>> pow(2, 20)
1048576

print(*objects, sep=' ', end='n', file=sys.stdout, flush=False)

打印输出

 >>> print("hello word")
hello word

property(fget=None, fset=None, fdel=None, doc=None)

range(start, stop[, step])

生成一个序列

 >>> range(10)
range(0, 10)
 >>> for n in range(5):
 ...  print(n)
 ...
0
1
2
3
4

repr(object)

返回一个包含对象的可打印表示的字符串

>>> repr(111)
'111'
>>> repr(111.11)
'111.11'

reversed(seq)

对一个对象的元素进行反转

 >>> li = [1, 2, 3, 4]
 >>> reversed(li)
<list_reverseiterator object at 0x000002CF0EF6A940>
 >>> for n in reversed(li):
 ...  print(n)
 ...
4
3
2
1

round(number[, ndigits])

四舍五入

 >>> round(3.3)
3
 >>> round(3.7)
4

set([iterable])

创建一个数据类型为集合

 >>> varss = set([11,222,333])
 >>> type(varss)
<class &#39;set&#39;>

setattr(object, name, value)

为某个对象设置一个属性

slice(start, stop[, step])

元素的切片操作都是调用的这个方法

sorted(iterable, key)

为一个对象的元素进行排序

代码:

#!/usr/bin/env python
# _*_ coding:utf-8 _*_

char=['赵',"123", "1", "25", "65","679999999999", "a","B","alex","c" ,"A", "_", "ᒲ",'a钱','孙','李',"余", '佘',"佗", "㽙", "铱", "钲钲㽙㽙㽙"]

new_chat = sorted(char)
print(new_chat)
for i in new_chat:
    print(bytes(i, encoding='utf-8'))

输出结果:

C:\Python35\python.exe F:/Python_code/Note/soretd.py
['1', '123', '25', '65', '679999999999', 'A', 'B', '_', 'a', 'alex', 'a钱', 'c', 'ᒲ', '㽙', '佗', '佘', '余', '孙', '李', '赵', '钲钲㽙㽙㽙', '铱']
b'1'
b'123'
b'25'
b'65'
b'679999999999'
b'A'
b'B'
b'_'
b'a'
b'alex'
b'a\xe9\x92\xb1'
b'c'
b'\xe1\x92\xb2'
b'\xe3\xbd\x99'
b'\xe4\xbd\x97'
b'\xe4\xbd\x98'
b'\xe4\xbd\x99'
b'\xe5\xad\x99'
b'\xe6\x9d\x8e'
b'\xe8\xb5\xb5'
b'\xe9\x92\xb2\xe9\x92\xb2\xe3\xbd\x99\xe3\xbd\x99\xe3\xbd\x99'
b'\xe9\x93\xb1'

Process finished with exit code 0

staticmethod(function)

返回函数的静态方法

str(object=b'', encoding='utf-8', errors='strict')

字符串

 >>> a = str(111)
 >>> type(a)
<class &#39;str&#39;>

sum(iterable[, start])

求和

 >>> sum([11,22,33])
66

super([type[, object-or-type]])

执行父类的构造方法

tuple([iterable])

创建一个对象,数据类型为元组

>>> tup = tuple([11,22,33,44])
>>> type(tup)
<class &#39;tuple&#39;>

type(object)

查看一个对象的数据类型

 >>> a = 1
 >>> type(a)
<class &#39;int&#39;>
 >>> a = "str"
 >>> type(a)
<class &#39;str&#39;>

vars([object])

查看一个对象里面有多少个变量

zip(*iterables)

将两个元素相同的序列转换为字典

>>> li1 = ["k1","k2","k3"]
>>> li2 = ["a","b","c"]
>>> d = dict(zip(li1,li2))
>>> d
{'k1': 'a', 'k2': 'b', 'k3': 'c'}

__import__(name, globals=None, locals=None, fromlist=(), level=0)

导入模块,把导入的模块作为一个别名

生成随机验证码例子

生成一个六位的随机验证码,且包含数字,数字的位置随机

# 导入random模块
import random
temp = ""
for i in range(6):
    num = random.randrange(0,4)
    if num == 3 or num == 1:
        rad2 = random.randrange(0,10)
        temp = temp + str(rad2)
    else:
        rad1 = random.randrange(65,91)
        c1 = chr(rad1)
        temp = temp + c1
print(temp)

输出结果

C:\Python35\python.exe F:/Python_code/sublime/Day06/built_in.py
72TD11

原文链接


The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.

Built-in Functions

abs() dict() help() min() setattr() all() dir()
hex() next() slice() any() pmod() id() object()
sorted() ascii() enumerate() input() oct() staticmethod() bin()
eval() int() open() str() bool() 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() delattr() hash() memoryview() set()

官方介绍:http://www.php.cn/

内置函数详解

abs(x)

返回数字的绝对值,参数可以是整数或浮点数,如果参数是复数,则返回其大小。

# 如果参数是复数,则返回其大小。
 >>> abs(-25)
25
 >>> abs(25)
25

all(iterable)

all()会循环括号内的每一个元素,如果括号内的所有元素都是真的,或者如果iterable为空,则返回True,如果有一个为假的那么就返回False

>>> all([])
True
>>> all([1,2,3])
True
>>> all([1,2,""])
False
# 如果有一个为假,则都为假
>>> all([1,2,None])
False

假的参数有:False0None""[](){}等,查看一个元素是否为假可以使用bool进行查看。

any(iterable)

循环元素,如果有一个元素为真,那么就返回True,否则就返回False

 >>> any([0,1])
True
 >>> any([0])
False

ascii(object)

在对象的类中寻找__repr__方法,获取返回值

 >>> class Foo:
 ...  def __repr_(self):
 ...     return "hello"
 ...
 >>> obj = Foo()
 >>> r = ascii(obj)
 >>> print(r)
# 返回的是一个可迭代的对象
<__main__.Foo object at 0x000001FDEE13D320>

bin(x)

将整数x转换为二进制字符串,如果x不为Python中int类型,x必须包含方法__index__()并且返回值为integer

# 返回一个整数的二进制
 >>> bin(999)
'0b1111100111'
# 非整型的情况,必须包含__index__()方法切返回值为integer的类型
 >>> class myType:
 ...   def __index__(self):
 ...       return 35
 ...
 >>> myvar = myType()
 >>> bin(myvar)
'0b100011'

bool([x])

查看一个元素的布尔值,非真即假

 >>> bool(0)
False
 >>> bool(1)
True
 >>> bool([1])
True
 >>> bool([10])
True

bytearray([source [, encoding [, errors]]])

返回一个byte数组,Bytearray类型是一个可变的序列,并且序列中的元素的取值范围为 [0 ,255]。

source参数:

  1. 如果source为整数,则返回一个长度为source的初始化数组;

  2. 如果source为字符串,则按照指定的encoding将字符串转换为字节序列;

  3. 如果source为可迭代类型,则元素必须为[0 ,255]中的整数;

  4. 如果source为与buffer接口一致的对象,则此对象也可以被用于初始化bytearray.。

 >>> bytearray(3)
bytearray(b'\x00\x00\x00')

bytes([source[, encoding[, errors]]])

 >>> bytes("asdasd",encoding="utf-8")
b'asdasd'

callable(object)

返回一个对象是否可以被执行

 >>> def func():
 ...  return 123
 ...
 >>> callable(func)
True
 >>> func = 123
 >>> callable(func)
False

chr(i)

返回一个数字在ASCII编码中对应的字符,取值范围256个

 >>> chr(66)
'B'
 >>> chr(5)
'\x05'
 >>> chr(55)
'7'
 >>> chr(255)
'\xff'
 >>> chr(25)
'\x19'
 >>> chr(65)
'A'

classmethod(function)

返回函数的类方法

compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)

把字符串编译成python可执行的代码

 >>> str = "for i in range(0,10): print(i)"
 >>> c = compile(str,'','exec')
 >>> exec(c)
0
1
2
3
4
5
6
7
8
9

complex([real[, imag]])

创建一个值为real + imag * j的复数或者转化一个字符串或数为复数。如果第一个参数为字符串,则不需要指定第二个参数

 >>> complex(1, 2)
(1+2j)
# 数字
 >>> complex(1)
(1+0j)
# 当做字符串处理
 >>> complex("1")
(1+0j)
# 注意:这个地方在“+”号两边不能有空格,也就是不能写成"1 + 2j",应该是"1+2j",否则会报错
 >>> complex("1+2j")
(1+2j)

delattr(object, name)

删除对象的属性值

>>> class cls:
...   @classmethod
...   def echo(self):
...     print('CLS')
... 
>>> cls.echo()
CLS
>>> delattr(cls, 'echo')
>>> cls.echo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'cls' has no attribute 'echo'

dict(**kwarg)

创建一个数据类型为字典

 >>> dic = dict({"k1":"123","k2":"456"})
 >>> dic
{'k1': '123', 'k2': '456'}

dir([object])

返回一个对象中中的所有方法

 >>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce\_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

pmod(a, b)

返回的是a//b(除法取整)以及a对b的余数,返回结果类型为tuple

 >>> pmod(10, 3)
(3, 1)

enumerate(iterable, start=0)

为元素生成下标

 >>> li = ["a","b","c"]
 >>> for n,k in enumerate(li):
 ...  print(n,k)
 ...
0 a
1 b
2 c

eval(expression, globals=None, locals=None)

把一个字符串当作一个表达式去执行

 >>> string = "1 + 3"
 >>> string
'1 + 3'
 >>> eval(string)
4

exec(object[, globals[, locals]])

把字符串当作python代码执行

 >>> exec("for n in range(5): print(n)")
0
1
2
3
4

filter(function, iterable)

筛选过滤,循环可迭代的对象,把迭代的对象当作函数的参数,如果符合条件就返回True,否则就返回False

 >>> def func(x):
 ...  if x == 11 or x == 22:
 ...    return True
 ...
 >>> ret = filter(func,[11,22,33,44])
 >>> for n in ret:
 ...  print(n)
 ...
11
22
>>> list(filter((lambda x: x > 0),range(-5,5)))
[1, 2, 3, 4]

float([x])

将整数和字符串转换成浮点数

 >>> float("124")
124.0
 >>> float("123.45")
123.45
 >>> float("-123.34")
-123.34

format(value[, format_spec])

字符串格式化

详键:http://www.php.cn/

frozenset([iterable])

frozenset是冻结的集合,它是不可变的,存在哈希值,好处是它可以作为字典的key,也可以作为其它集合的元素。缺点是一旦创建便不能更改,没有add,remove方法。

getattr(object, name[, default])

返回对象的命名属性的值,name必须是字符串,如果字符串是对象属性之一的名称,则结果是该属性的值。

globals()

获取或修改当前文件内的全局变量

>>> a = "12"
>>> bsd = "54asd"
>>> globals()
{'__doc__': None, 'a': '12', '__loader__': <class &#39;_frozen_importlib.BuiltinImporter&#39;>, 'bsd': '54asd', '__builtins__': <module &#39;builtins&#39; (built-in)>, 'n': '__doc__', '__name__': '__main__', '__spec__': None, '__package__': None}

hasattr(object, name)

参数是一个对象和一个字符串,如果字符串是对象的某个属性的名称,则结果为True,否则为False。

hash(object)

返回一个对象的hash值

 >>> a = "asdadasdwqeq234sdfdf"
 >>> hash(a)
5390438057823015497

help([object])

查看一个类的所有详细方法,或者查看某个方法的使用详细信息

 >>> help(list)
Help on class list in module __builtin__:

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items
 |  
 |  Methods defined here:
 |  
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 |  
 |  __contains__(...)
 |      x.__contains__(y) <==> y in x
 |  
 |  __delitem__(...)
 |      x.__delitem__(y) <==> del x[y]
 |  
 |  __delslice__(...)
 |      x.__delslice__(i, j) <==> del x[i:j]
 |      
 |      Use of negative indices is not supported.
..........

hex(x)

获取一个数的十六进制

 >>> hex(13)
'0xd'

id(object)

返回一个对象的内存地址

 >>> a = 123
 >>> id(a)
1835400816

input([prompt])

交互式输入

 >>> name = input("Pless your name: ")
Pless your name: ansheng
 >>> print(name)
ansheng

int(x, base=10)

获取一个数的十进制

 >>> int("31")
31

也可以作为进制转换

 >>> int(10)
10
 >>> int('0b11',base=2)
3
 >>> int('11',base=8)
9
 >>> int('0xe',base=16)
14

isinstance(object, classinfo)

判断对象是否是这个类创建的

>>> li = [11,22,33]
>>> isinstance(li,list)
True

issubclass(class, classinfo)

查看一个对象是否为子类

iter(object[, sentinel])

创建一个可迭代的对象

 >>> obj = iter([11,22,33,44])
 >>> obj
<list_iterator object at 0x000002477DB25198>
 >>> for n in obj:
 ...  print(n)
 ...
11
22
33
44

len(s)

查看一个对象的长度

 >>> url="ansheng.me"
 >>> len(url)
10

list([iterable])

创建一个数据类型为列表

 >>> li = list([11,22,33,44])
 >>> li
[11, 22, 33, 44]

locals()

返回当前作用域的局部变量,以字典形式输出

 >>> func()
 >>> def func():
 ...  name="ansheng"
 ...  print(locals())
 ...
 >>> func()
{'name': 'ansheng'}

map(function, iterable,  ...)

对一个序列中的每一个元素都传到函数中执行并返回

>>> list(map((lambda x : x +10),[1,2,3,4]))
[11, 12, 13, 14]

max(iterable, *[, key, default])

max(arg1, arg2, *args[, key])

取一个对象中的最大值

 >>> li = list([11,22,33,44])
 >>> li = [11,22,33,44]
 >>> max(li)
44

memoryview(obj)

返回对象obj的内存查看对象

 >>> import struct
 >>> buf = struct.pack("i"*12, *list(range(12)))
 >>> x = memoryview(buf)
 >>> y = x.cast('i', shape=[2,2,3])
 >>> print(y.tolist())
[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]

min(iterable, *[, key, default])

min(arg1, arg2, *args[, key])

取一个对象中的最小值

 >>> li = list([11,22,33,44])
 >>> li = [11,22,33,44]
 >>> min(li)
11

next(iterator[, default])

每次只拿取可迭代对象的一个元素

 >>> obj = iter([11,22,33,44])
 >>> next(obj)
11
 >>> next(obj)
22
 >>> next(obj)
33
 >>> next(obj)
44
 >>> next(obj)
 # 如果没有可迭代的元素了就会报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

object

返回一个新的无特征对象

oct(x)

获取一个字符串的八进制

 >>> oct(13)
'0o15'

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

文件操作的函数,用来做文件操作的

 # 打开一个文件
- >>> f = open("a.txt","r")

ord(c)

把一个字母转换为ASCII对对应表中的数字

 >>> ord("a")
97
 >>> ord("t")
116

pow(x, y[, z])

返回一个数的N次方

 >>> pow(2, 10)
1024
 >>> pow(2, 20)
1048576

print(*objects, sep=' ', end='n', file=sys.stdout, flush=False)

打印输出

 >>> print("hello word")
hello word

property(fget=None, fset=None, fdel=None, doc=None)

range(start, stop[, step])

生成一个序列

 >>> range(10)
range(0, 10)
 >>> for n in range(5):
 ...  print(n)
 ...
0
1
2
3
4

repr(object)

返回一个包含对象的可打印表示的字符串

>>> repr(111)
'111'
>>> repr(111.11)
'111.11'

reversed(seq)

对一个对象的元素进行反转

 >>> li = [1, 2, 3, 4]
 >>> reversed(li)
<list_reverseiterator object at 0x000002CF0EF6A940>
 >>> for n in reversed(li):
 ...  print(n)
 ...
4
3
2
1

round(number[, ndigits])

四舍五入

 >>> round(3.3)
3
 >>> round(3.7)
4

set([iterable])

创建一个数据类型为集合

 >>> varss = set([11,222,333])
 >>> type(varss)
<class &#39;set&#39;>

setattr(object, name, value)

为某个对象设置一个属性

slice(start, stop[, step])

元素的切片操作都是调用的这个方法

sorted(iterable, key)

为一个对象的元素进行排序

代码:

#!/usr/bin/env python
# _*_ coding:utf-8 _*_

char=['赵',"123", "1", "25", "65","679999999999", "a","B","alex","c" ,"A", "_", "ᒲ",'a钱','孙','李',"余", '佘',"佗", "㽙", "铱", "钲钲㽙㽙㽙"]

new_chat = sorted(char)
print(new_chat)
for i in new_chat:
    print(bytes(i, encoding='utf-8'))

输出结果:

C:\Python35\python.exe F:/Python_code/Note/soretd.py
['1', '123', '25', '65', '679999999999', 'A', 'B', '_', 'a', 'alex', 'a钱', 'c', 'ᒲ', '㽙', '佗', '佘', '余', '孙', '李', '赵', '钲钲㽙㽙㽙', '铱']
b'1'
b'123'
b'25'
b'65'
b'679999999999'
b'A'
b'B'
b'_'
b'a'
b'alex'
b'a\xe9\x92\xb1'
b'c'
b'\xe1\x92\xb2'
b'\xe3\xbd\x99'
b'\xe4\xbd\x97'
b'\xe4\xbd\x98'
b'\xe4\xbd\x99'
b'\xe5\xad\x99'
b'\xe6\x9d\x8e'
b'\xe8\xb5\xb5'
b'\xe9\x92\xb2\xe9\x92\xb2\xe3\xbd\x99\xe3\xbd\x99\xe3\xbd\x99'
b'\xe9\x93\xb1'

Process finished with exit code 0

staticmethod(function)

返回函数的静态方法

str(object=b'', encoding='utf-8', errors='strict')

字符串

 >>> a = str(111)
 >>> type(a)
<class &#39;str&#39;>

sum(iterable[, start])

求和

 >>> sum([11,22,33])
66

super([type[, object-or-type]])

执行父类的构造方法

tuple([iterable])

创建一个对象,数据类型为元组

>>> tup = tuple([11,22,33,44])
>>> type(tup)
<class &#39;tuple&#39;>

type(object)

查看一个对象的数据类型

 >>> a = 1
 >>> type(a)
<class &#39;int&#39;>
 >>> a = "str"
 >>> type(a)
<class &#39;str&#39;>

vars([object])

查看一个对象里面有多少个变量

zip(*iterables)

将两个元素相同的序列转换为字典

>>> li1 = ["k1","k2","k3"]
>>> li2 = ["a","b","c"]
>>> d = dict(zip(li1,li2))
>>> d
{'k1': 'a', 'k2': 'b', 'k3': 'c'}

__import__(name, globals=None, locals=None, fromlist=(), level=0)

导入模块,把导入的模块作为一个别名

生成随机验证码例子

生成一个六位的随机验证码,且包含数字,数字的位置随机

# 导入random模块
import random
temp = ""
for i in range(6):
    num = random.randrange(0,4)
    if num == 3 or num == 1:
        rad2 = random.randrange(0,10)
        temp = temp + str(rad2)
    else:
        rad1 = random.randrange(65,91)
        c1 = chr(rad1)
        temp = temp + c1
print(temp)

输出结果

C:\Python35\python.exe F:/Python_code/sublime/Day06/built_in.py
72TD11


更多Python全栈之路系列之Python3内置函数 相关文章请关注PHP中文网!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. C  : Memory Management and ControlPython vs. C : Memory Management and ControlApr 19, 2025 am 12:17 AM

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python for Scientific Computing: A Detailed LookPython for Scientific Computing: A Detailed LookApr 19, 2025 am 12:15 AM

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Python and C  : Finding the Right ToolPython and C : Finding the Right ToolApr 19, 2025 am 12:04 AM

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python for Data Science and Machine LearningPython for Data Science and Machine LearningApr 19, 2025 am 12:02 AM

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Learning Python: Is 2 Hours of Daily Study Sufficient?Learning Python: Is 2 Hours of Daily Study Sufficient?Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python for Web Development: Key ApplicationsPython for Web Development: Key ApplicationsApr 18, 2025 am 12:20 AM

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python vs. C  : Exploring Performance and EfficiencyPython vs. C : Exploring Performance and EfficiencyApr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.