python按规则打印一组数字![[无辜呆]](//lf-web-assets.juejin.cn/obj/juejin-web/xitu_juejin_web/img/jj_emoji_96.4fb9d71.png)
![[无辜呆]](//lf-web-assets.juejin.cn/obj/juejin-web/xitu_juejin_web/img/jj_emoji_96.4fb9d71.png)
enumerate()
enumerate()函数意为循环计数,获得索引和值。
for index, ball in enumerate(balls)
在for index, ball in enumerate(balls):中 index 是索引,ball是该索引对应的值。可以在for循环中使用。
print(f'{ball:0>2d}', end=' ')
在print(f'{ball:0>2d}', end=' ')中 {:0>2d}代表用两个宽度的十进制显示,已零补齐。
print(f'......')与format函数类似。f后面一定要跟紧字符串不能隔有空格。
end=''用来设定以什么结尾。
if index == len(balls) - 2:
if index == len(balls) - 2:意为判断当前索引是倒数第二个遍历的。
![[无辜呆]](http://lf-web-assets.juejin.cn/obj/juejin-web/xitu_juejin_web/img/jj_emoji_96.4fb9d71.png)
![[无辜呆]](http://lf-web-assets.juejin.cn/obj/juejin-web/xitu_juejin_web/img/jj_emoji_96.4fb9d71.png)
enumerate()
enumerate()函数意为循环计数,获得索引和值。
for index, ball in enumerate(balls)
在for index, ball in enumerate(balls):中 index 是索引,ball是该索引对应的值。可以在for循环中使用。
print(f'{ball:0>2d}', end=' ')
在print(f'{ball:0>2d}', end=' ')中 {:0>2d}代表用两个宽度的十进制显示,已零补齐。
print(f'......')与format函数类似。f后面一定要跟紧字符串不能隔有空格。
end=''用来设定以什么结尾。
if index == len(balls) - 2:
if index == len(balls) - 2:意为判断当前索引是倒数第二个遍历的。
展开
评论
点赞