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

Python Exercise Example 47

Python Exercise Example 47

Python 100 Examples Python 100 Examples

**Problem:** Swap the values of two variables.

**Program analysis:** None

Program source code:

Example

#!/usr/bin/python

-- coding: UTF-8 --

 

def

exchange

(

a

,

b

)

:

    a

,

b

=

b

,

a

   

return

(

a

,

b

)

 

if

name

==

'main'

:

    x

=

10

    y

=

20

   

print

(

'x = %d,y = %d'

%

(

x

,

y

)

)

    x

,

y

=

exchange

(

x

,

y

)

   

print

(

'x = %d,y = %d'

%

(

x

,

y

)

)

The output of the above example is:


x = 10,y = 20
x = 20,y = 10

Python 100 Examples Python 100 Examples