[Job] Python- function package: exchange of two variables values

1, the exchange value of two variables

1.1> intermediate variables (variables third party)

# Switching a value of two variables 
DEF the swap (A, B):
     # added to a variable assigned to one another intermediate 
    C = A 
    A = B 
    B = C
     return A, B 


Print (the swap (. 1,. 3))

Output:

 

 

 

1.2> and using the two variables

# With two variables and reassigning 
DEF the swap (a, B): 
    a = a + B
     # at this time has been a re-assignment of 
    B = a - B
     # In this case a is (a + b), b above have been a value assigned to the original 
    a = a - B
     return a, B 


Print (the swap (. 1,. 3))

Output:

 

 

1.3> direct exchange

# Direct assignment equal sign changes noted 
DEF the swap (A, B): 
    A, B = B, A
     return A, B 


Print (the swap (. 1,. 3))

Output:

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/cxstudypython/p/11970608.html