python build in function_Python Built-in function

本文介绍了Python中dir()函数的行为,针对不同类型的对象展示其属性,以及enumerate()函数用于迭代并计数。同时讲解了format()函数的参数设置和数字格式化技巧。涵盖了对象操作、字符串格式化和基础数据类型处理。

dir(object)

默认dir()机制对不同类型的对象表现不同,因为它尝试生成最相关但非完整的信息。

如果对象是模块对象,则列表包含模块属性的名称。

如果对象是类型或类对象,则列表包含其属性的名称,并递归地包含其基础的属性。

#没有对象

>>> dir()

['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']

# 给予对象

>>> import turtle

>>> dir(turtle)

['Canvas', 'Pen', 'RawPen', 'RawTurtle', 'Screen', 'ScrolledCanvas', 'Shape', 'TK', 'TNavigator', 'TPen', 'Tbuffer', 'Terminator', 'Turtle', 'TurtleGraphicsError', 'TurtleScreen', 'TurtleScreenBase', 'Vec2D', '_CFG', '_LANGUAGE', '_Root', '_Screen', '_TurtleImage', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__forwardmethods', '__func_body', '__loader__', '__methodDict', '__methods', '__name__', '__package__', '__spec__', '__stringBody', '_alias_list', '_make_global_funcs', '_screen_docrevise', '_tg_classes', '_tg_screen_functions', '_tg_turtle_functions', '_tg_utilities', '_turtle_docrevise', '_ver', 'addshape', 'back', 'backward', 'begin_fill', 'begin_poly', 'bgcolor', 'bgpic', 'bk', 'bye', 'circle', 'clear', 'clearscreen', 'clearstamp', 'clearstamps', 'clone', 'color', 'colormode', 'config_dict', 'deepcopy', 'degrees', 'delay', 'distance', 'done', 'dot', 'down', 'end_fill', 'end_poly', 'exitonclick', 'fd', 'fillcolor', 'filling', 'forward', 'get_poly', 'get_shapepoly', 'getcanvas', 'getmethparlist', 'getpen', 'getscreen', 'getshapes', 'getturtle', 'goto', 'heading', 'hideturtle', 'home', 'ht', 'inspect', 'isdown', 'isfile', 'isvisible', 'join', 'left', 'listen', 'lt', 'mainloop', 'math', 'mode', 'numinput', 'onclick', 'ondrag', 'onkey', 'onkeypress', 'onkeyrelease', 'onrelease', 'onscreenclick', 'ontimer', 'pd', 'pen', 'pencolor', 'pendown', 'pensize', 'penup', 'pos', 'position', 'pu', 'radians', 'read_docstrings', 'readconfig', 'register_shape', 'reset', 'resetscreen', 'resizemode', 'right', 'rt', 'screensize', 'seth', 'setheading', 'setpos', 'setposition', 'settiltangle', 'setundobuffer', 'setup', 'setworldcoordinates', 'setx', 'sety', 'shape', 'shapesize', 'shapetransform', 'shearfactor', 'showturtle', 'simpledialog', 'speed', 'split', 'st', 'stamp', 'sys', 'textinput', 'tilt', 'tiltangle', 'time', 'title', 'towards', 'tracer', 'turtles', 'turtlesize', 'types', 'undo', 'undobufferentries', 'up', 'update', 'width', 'window_height', 'window_width', 'write', 'write_docstringdict', 'xcor', 'ycor']

enumerate(iterable,start = 0 )

返回一个枚举对象。iterable必须是序列。enumerate()返回一个包含计数的元组(从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')]

format()

format 函数可以接受不限个参数,位置可以不按顺序。

>>>"{},{}".format("hello", "world") # 不设置指定位置,按默认顺序

'hello world'

>>> "{0},{1}".format("hello", "world") # 设置指定位置

'hello world'

>>> "{1},{0},{1}".format("hello", "world") # 设置指定位置

'world hello world'

设置参数

>>> print("网站名:{name}, 地址:{url}".format(name="菜鸟教程", url="www.runoob.com"))

网站名:菜鸟教程, 地址:www.runoob.com

# 通过字典设置参数

>>> site = {"name":"菜鸟教程", "url":"www.runoob.com"}

>>> print("网站名:{name}, 地址 {url}".format(**site))

网站名:菜鸟教程, 地址:www.runoob.com

# 通过列表索引设置参数

my_list = ['菜鸟教程', 'www.runoob.com']

print("网站名:{0[0]}, 地址 {0[1]}".format(my_list)) # "0" 是必须的

网站名:菜鸟教程, 地址:www.runoob.com

数字格式化

>>> print("{:.2f}".format(3.1415926));

3.14

数字

格式

输出

描述

3.1415926

{:.2f}

3.14

保留小数点后两位

3.1415926

{:+.2f}

+3.14

带符号保留小数点后两位

-1

{:+.2f}

-1.00

带符号保留小数点后两位

2.71828

{:.0f}

3

不带小数

5

{:0>2d}

05

数字补零 (填充左边, 宽度为2)

5

{:x<4d}

5xxx

数字补x (填充右边, 宽度为4)

10

{:x<4d}

10xx

数字补x (填充右边, 宽度为4)

1000000

{:,}

1,000,000

以逗号分隔的数字格式

0.25

{:.2%}

25.00%

百分比格式

1000000000

{:.2e}

1.00e+09

指数记法

13

{:10d}

13

右对齐 (默认, 宽度为10)

13

{:<10d}

13

左对齐 (宽度为10)

13

{:^10d}

13

中间对齐 (宽度为10)

^, 分别是居中、左对齐、右对齐,后面带宽度, : 号后面带填充的字符,只能是一个字符,不指定则默认是用空格填充。

此外我们可以使用大括号 {} 来转义大括号

isinstance(object, classinfo)

object -- 实例对象。

classinfo -- 可以是直接或间接类名、基本类型或者由它们组成的元组。

>>>a = 2

>>> isinstance (a,int)

True

>>> isinstance (a,str)

False

>>> isinstance (a,(str,int,list)) # 是元组中的一个返回 True

True

对于基本类型来说 classinfo 可以是:

int,float,bool,complex,str(字符串),list,dict(字典),set,tuple

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值