How python exchanges two variables
This article is about how python exchanges two variables. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Swap two variables
Exchange is the process of exchanging the values of two variables with each other. This can be useful in many operations in computer science. Here, I have written two main methods for programmers to exchange values and the best solution.
Method I-use temporary variables
This method uses temporary variables to store some data. The following code is written using temporary variable names.
A print b = 50,60 print (ameme b) temp= axiom b # averse 50 baked 50 temp=110 b = a # averse 50 baked 50 temp=110 a = temp-b # averse 60 baked 50 temp=110 print ("After swapping:", aPermian b)
Output quantity
50 60 After swapping: 60 50
Method II- does not use temporary variables
The following code swaps variables without using temporary variables.
A print b = 50,60 print (aMagneb) a = aquib # averse 110 baud 60 b = a Mui b # aqu 110 bang 50 a = a Mui b # ahe 60 baked 50 print ("After swapping:", aMagneb)
Output quantity
50 60 After swapping: 60 50
Methods the excellent solution in III-Python
This is another way to exchange variables using python. In the previous section, we learned about multiple assignments. We can use the concept of exchange.
A print b = 50,60 print (a minute b) a print b = b ("After swapping", a line b)
Output quantity
50 60 After swapping 60 50 Thank you for your reading! This is the end of the article on "how to exchange two variables in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!