Python Practice Example 5

Python 100例Python 100 Examples

Title:Input three integers x, y, z, please output these three numbers from small to large.

Program analysis:We try to put the smallest number on 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例Python 100 Examples

Other extensions