python内建函数(不完全)

简介:

各位还是参考官方文档吧,我这些是自己感觉重要和常用的

abs ()
all (iterable) 如果迭代序列中所有的元素都为真,或者迭代序列为空的时候返回 True 。等价于:
def  all (iterable):
     for  element in  iterable:
         if  not  element:
             return  False
     return  True
all (iterable) 如果迭代序列中所有的元素都为真,返回 True 。等价于
def  any (iterable):
     for  element in  iterable:
         if  element:
             return  True
     return  False
complex ()创建复数:
>>> complex ( 1 , 2 )
( 1 + 2j )
>>> complex ( 1 )
( 1 + 0j )
delattr ( object , name)
For example, delattr (x, 'foobar' ) is  equivalent to del  x.foobar.
dict ()创建字典
dir ()
>>> import  struct
>>> dir ()   # show the names in the module namespace
[ '__builtins__' , '__doc__' , '__name__' , 'struct' ]
>>> dir (struct)   # show the names in the struct module
[ 'Struct' , '__builtins__' , '__doc__' , '__file__' , '__name__' ,
  '__package__' , '_clearcache' , 'calcsize' , 'error' , 'pack' , 'pack_into' ,
  'unpack' , 'unpack_from' ]
>>> class  Shape( object ):
         def  __dir__( self ):
             return  [ 'area' , 'perimeter' , 'location' ]
>>> s =  Shape()
>>> dir (s)
[ 'area' , 'perimeter' , 'location' ]
divmod (I,j)返回以商和余数组成的元祖:
>>> divmod ( 10 , 3 )
( 3 , 1 )
enumerate (sequence[, start = 0 ])
>>> seasons =  [ 'Spring' , 'Summer' , 'Fall' , 'Winter' ]
>>> list ( enumerate (seasons))
[( 0 , 'Spring' ), ( 1 , 'Summer' ), ( 2 , 'Fall' ), ( 3 , 'Winter' )]
>>> list ( enumerate (seasons, start = 1 ))
[( 1 , 'Spring' ), ( 2 , 'Summer' ), ( 3 , 'Fall' ), ( 4 , 'Winter' )]
其实也就等价于:
def  enumerate (sequence, start = 0 ):
     n =  start
     for  elem in  sequence:
         yield  n, elem
         n + =  1
eval (expression[, globals [, locals ]])
>>> x =  1
>>> print  eval ( 'x+1' )
2
filter (function, iterable) is  equivalent to [item for  item in  iterable if  function(item)] if  function is  not  None  and  [item foritem in  iterable if  item] if  function is  None .
getattr ( object , name[, default])
  For example, getattr (x, 'foobar' ) is  equivalent to x.foobar.
hasattr ( object , name)
help ([ object ])¶
hex (x) 将整形x转化为 16 进制字符串,如果想要转化浮点型,可以使用 float . hex (x)
id ( object ) 对象的内存地址
input ([prompt]) Equivalent to eval ( raw_input (prompt)).
isinstance ( object , classinfo)
issubclass ( class , classinfo)
iter (o[, sentinel]) 迭代o,直到指和sentinel相等。例如:reads a file  until the readline() method returns an empty string:
with open ( 'mydata.txt' ) as fp:
     for  line in  iter (fp.readline, ''):
         process_line(line)
map (function, iterable, ...) Apply  function to every item of iterable and  return  a list  of the results.
max (iterable[, args...][, key])
min (iterable[, args...][, key])
next (iterator[, default])
oct (x)
Convert an integer number (of any  size) to an octal string. The result is  a valid Python expression.
open (name[, mode[, buffering]])
property ([fget[, fset[, fdel[, doc]]]])
class  C( object ):
     def  __init__( self ):
         self ._x =  None
 
     def  getx( self ):
         return  self ._x
     def  setx( self , value):
         self ._x =  value
     def  delx( self ):
         del  self ._x
     x =  property (getx, setx, delx, "I'm the 'x' property." )
创建只读属性:这个属性的值就不能修改了
class  Parrot( object ):
     def  __init__( self ):
         self ._voltage =  100000
 
     @property
     def  voltage( self ):
         """Get the current voltage."""
         return  self ._voltage
 
 
class  C( object ):
     def  __init__( self ):
         self ._x =  None
 
     @property
     def  x( self ):
         """I'm the 'x' property."""
         return  self ._x
 
     @x .setter
     def  x( self , value):
         self ._x =  value
 
     @x .deleter
     def  x( self ):
         del  self ._x
range ([start], stop[, step])
>>> range ( 10 )
[ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
>>> range ( 1 , 11 )
[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
>>> range ( 0 , 30 , 5 )
[ 0 , 5 , 10 , 15 , 20 , 25 ]
>>> range ( 0 , 10 , 3 )
[ 0 , 3 , 6 , 9 ]
>>> range ( 0 , - 10 , - 1 )
[ 0 , - 1 , - 2 , - 3 , - 4 , - 5 , - 6 , - 7 , - 8 , - 9 ]
>>> range ( 0 )
[]
>>> range ( 1 , 0 )
[]
reduce (function, iterable[, initializer])
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.
round (x[, n])
>>> round ( 1.12313 , 2 )
1.12
slice ([start], stop[, step])
sorted (iterable[, cmp [, key[, reverse]]])
staticmethod (function)
Return a static method for  function.
class  C:
     @staticmethod
     def  f(arg1, arg2, ...): ...
sum (iterable[, start])
tuple ([iterable])
>>> tuple ( "ada" )
( 'a' , 'd' , 'a' )
zip ([iterable, ...])
zip () in  conjunction with the *  operator can be used to unzip a list :
>>> x =  [ 1 , 2 , 3 ]
>>> y =  [ 4 , 5 , 6 ]
>>> zipped =  zip (x, y)
>>> zipped
[( 1 , 4 ), ( 2 , 5 ), ( 3 , 6 )]
>>> x2, y2 =  zip ( * zipped)
>>> x = =  list (x2) and  y = =  list (y2)
True
 
>>> float .fromhex( '0x3.a7p10' )
3740.0
 
>>> lists =  [[]] *  3
>>> lists
[[], [], []]
>>> lists[ 0 ].append( 3 )
>>> lists
[[ 3 ], [ 3 ], [ 3 ]]
 
>>> lists =  [[] for  i in  range ( 3 )]
>>> lists[ 0 ].append( 3 )
>>> lists[ 1 ].append( 5 )
>>> lists[ 2 ].append( 7 )
>>> lists
[[ 3 ], [ 5 ], [ 7 ]]
 
>>> "The sum of 1 + 2 is {0}" . format ( 1 + 2 )
'The sum of 1 + 2 is 3'
 
>>> "they're bill's friends from the UK" .title()
"They'Re Bill'S Friends From The Uk"
 
>>> print  '%(language)s has %(number)03d quote types.'  %  \
...       { "language" : "Python" , "number" : 2 }
Python has 002  quote types.
 
>>> dishes =  { 'eggs' : 2 , 'sausage' : 1 , 'bacon' : 1 , 'spam' : 500 }
>>> keys =  dishes.viewkeys()
>>> values =  dishes.viewvalues()
 
>>> # iteration
>>> n =  0
>>> for  val in  values:
...     n + =  val
>>> print (n)
504
 
>>> # keys and values are iterated over in the same order
>>> list (keys)
[ 'eggs' , 'bacon' , 'sausage' , 'spam' ]
>>> list (values)
[ 2 , 1 , 1 , 500 ]
 
>>> # view objects are dynamic and reflect dict changes
>>> del  dishes[ 'eggs' ]
>>> del  dishes[ 'sausage' ]
>>> list (keys)
[ 'spam' , 'bacon' ]
 
>>> # set operations
>>> keys & { 'eggs' , 'bacon' , 'salad' }
{ 'bacon' }
 
>>> v =  memoryview( 'abcefg' )
>>> v[ 1 ]
'b'
>>> v[ - 1 ]
'g'
>>> v[ 1 : 4 ]
<memory at 0x77ab28 >
>>> v[ 1 : 4 ].tobytes()
'bce'
 
>>> data =  bytearray( 'abcefg' )
>>> v =  memoryview(data)
>>> v.readonly
False
>>> v[ 0 ] =  'z'
>>> data
bytearray(b 'zbcefg' )
>>> v[ 1 : 4 ] =  '123'
>>> data
bytearray(b 'z123fg' )
>>> v[ 2 ] =  'spam'
Traceback (most recent call last):
   File  "<stdin>" , line 1 , in  <module>
ValueError: cannot modify size of memoryview object
 
>>> m =  memoryview( "abc" )
>>> m.tobytes()
'abc'
 
>>> memoryview( "abc" ).tolist()
[ 97 , 98 , 99 ]
目录
相关文章
|
5天前
|
人工智能 JavaScript Linux
【Claude Code 全攻略】终端AI编程助手从入门到进阶(2026最新版)
Claude Code是Anthropic推出的终端原生AI编程助手,支持40+语言、200k超长上下文,无需切换IDE即可实现代码生成、调试、项目导航与自动化任务。本文详解其安装配置、四大核心功能及进阶技巧,助你全面提升开发效率,搭配GitHub Copilot使用更佳。
|
7天前
|
存储 人工智能 自然语言处理
OpenSpec技术规范+实例应用
OpenSpec 是面向 AI 智能体的轻量级规范驱动开发框架,通过“提案-审查-实施-归档”工作流,解决 AI 编程中的需求偏移与不可预测性问题。它以机器可读的规范为“单一真相源”,将模糊提示转化为可落地的工程实践,助力开发者高效构建稳定、可审计的生产级系统,实现从“凭感觉聊天”到“按规范开发”的跃迁。
924 13
|
3天前
|
云安全 安全
免费+限量+领云小宝周边!「阿里云2026云上安全健康体检」火热进行中!
诚邀您进行年度自检,发现潜在风险,守护云上业务连续稳健运行
1169 1
|
6天前
|
人工智能 JavaScript 前端开发
【2026最新最全】一篇文章带你学会Cursor编程工具
本文介绍了Cursor的下载安装、账号注册、汉化设置、核心模式(Agent、Plan、Debug、Ask)及高阶功能,如@引用、@Doc文档库、@Browser自动化和Rules规则配置,助力开发者高效使用AI编程工具。
813 4
|
7天前
|
消息中间件 人工智能 Kubernetes
阿里云云原生应用平台岗位急招,加入我们,打造 AI 最强基础设施
云原生应用平台作为中国最大云计算公司的基石,现全面转向 AI,打造 AI 时代最强基础设施。寻找热爱技术、具备工程极致追求的架构师、极客与算法专家,共同重构计算、定义未来。杭州、北京、深圳、上海热招中,让我们一起在云端,重构 AI 的未来。
|
9天前
|
IDE 开发工具 C语言
【2026最新】VS2026下载安装使用保姆级教程(附安装包+图文步骤)
Visual Studio 2026是微软推出的最新Windows专属IDE,启动更快、内存占用更低,支持C++、Python等开发。推荐免费的Community版,安装简便,适合初学者与个人开发者使用。
1025 11
|
11天前
|
存储 JavaScript 前端开发
JavaScript基础
本节讲解JavaScript基础核心知识:涵盖值类型与引用类型区别、typeof检测类型及局限性、===与==差异及应用场景、内置函数与对象、原型链五规则、属性查找机制、instanceof原理,以及this指向和箭头函数中this的绑定时机。重点突出类型判断、原型继承与this机制,助力深入理解JS面向对象机制。(238字)
|
9天前
|
人工智能 Shell 开发工具
Claude Code 2.1.2超详细更新说明,小白也能10分钟上手
Claude Code 2.1.x重磅更新:Shift+Enter换行、Esc+Esc撤销、Ctrl+B后台运行,Skills技能系统全面升级,支持多语言、通配符权限与动态MCP检测,性能提升50%,迭代速度惊人,开发者效率暴涨!
Claude Code 2.1.2超详细更新说明,小白也能10分钟上手