Python Program to Swap Two Variables

Publicado el: 01 septiembre 2019
en el canal de: IT WORLD
19
0

Using temporary variable :
In this program, we use the temp variable to temporarily hold the value of x. We then put the value of y in x and later temp in y. In this way, the values get exchanged.
Without Using Temporary Variable :
In python programming, there is a simple construct to swap variables. The following code does the same as above but without the use of any temporary variable.

x,y = y,x
If the variables are both numbers, we can use arithmetic operations to do the same. It might not look intuitive at the first sight. But if you think about it, its pretty easy to figure it out.Here are a few example
Addition and Subtraction

x = x + y
y = x - y
x = x - y
Multiplication and Division

x = x * y
y = x / y
x = x / y


En esta página del sitio puede ver el video en línea Python Program to Swap Two Variables de Duración hora minuto segunda en buena calidad , que subió el usuario IT WORLD 01 septiembre 2019, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 19 veces y le gustó 0 a los espectadores. Disfruta viendo!