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

Python Exercise Example 5

Python Exercise Example 5

Python 100 Examples Python 100 Examples

**Problem:** Input three integers x, y, z, and output these three numbers in ascending order.

**Program Analysis:** We find a way to put the smallest number in x. First compare x with y; if x>y, swap the values of x and y. Then compare x with z; if x>z, swap the values of x and z. This makes x the smallest.

Program source code:

Example (Python 2.0+)

#!/usr/bin/python

-- coding: UTF-8 --

l

=

[

]

for

i

in

range

(

3

)

:

x

=

int

(

raw_input

(

'

integer:

\n

'

)

)

l

.

append

(

x

)

l

.

sort

(

)

print

l

Example (Python 3.0+)

#!/usr/bin/python3

l

=

[

]

for

i

in

range

(

3

)

:

x

=

int

(

input

(

'

integer:

\n

'

)

)

l

.

append

(

x

)

l

.

sort

(

)

print

(

l

)

The output of the above example is:


integer:
8
integer:
5
integer:
6
[5, 6, 8]

Python 100 Examples Python 100 Examples