Python 练习实例32

2022-04-08  本文已影响0人  loinue

来自菜鸟教程
https://www.runoob.com/python/python-exercise-example32.html
题目:按相反的顺序输出列表的值。

程序分析:无。

程序源代码:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
a = ['one', 'two', 'three']
for i in a[::-1]:
    print (i)

以上实例输出结果为:

three
two
one

这里主要是使用了a[::-1]这种写法。
注意有两个冒号,说明有三个参数。前两个参数忽略。
最后一个参数用-1表示是倒数第一个。

上一篇 下一篇

猜你喜欢

热点阅读