Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Exercise Example 40

Python Exercise Example 40

Python 100 Examples Python 100 Examples

**Problem:** Output an array in reverse order.

**Program Analysis:** Swap the first element with the last one.

Program source code:

Example

#!/usr/bin/python

-- coding: UTF-8 --

if

name

==

'

main

'

:

a

=

[

9

,

6

,

5

,

4

,

1

]

N

=

len

(

a

)

print

(

a

)

for

i

in

range

(

len

(

a

)

//

2

)

:

a

[

i

]

,

a

[

N

i

1

]

=

a

[

N

i

1

]

,

a

[

i

]

print

(

a

)

The output of the above example is:


[9, 6, 5, 4, 1]
[1, 4, 5, 6, 9]

Python 100 Examples Python 100 Examples