5 façons d'échanger deux variables en Python

Découvrez cinq techniques différentes pour échanger les valeurs de deux variables en Python, essentielles pour diverses tâches de programmation et algorithmes.

Étant donné deux variables x et y, écrivez un programme Python pour échanger leurs valeurs. Voyons différentes méthodes en Python pour effectuer cette tâche. 

Méthode 1 : Utiliser l’approche naïve

L'approche la plus naïve consiste à stocker la valeur d'une variable (disons x) dans une variable temporaire, puis à attribuer à la variable x la valeur de la variable y. Enfin, attribuez à la variable y la valeur de la variable temporaire.

# Python program to demonstrate
# swapping of two variables

x = 10
y = 50

# Swapping of two variables
# Using third variable
temp = x
x = y
y = temp

print("Value of x:", x)
print("Value of y:", y)
 

Sortir

Value of x: 50
Value of y: 10

Méthode 2 : Utilisation de l'opérateur virgule
En utilisant l'opérateur virgule, la valeur des variables peut être échangée sans utiliser de troisième variable.

# Python program to demonstrate
# swapping of two variables


x = 10
y = 50

# Swapping of two variables 
# without using third variable
x, y = y, x

print("Value of x:", x)
print("Value of y:", y)

Sortir

Value of x: 50
Value of y: 10

Méthode 3 : Utilisation de XOR
L'opérateur XOR au niveau du bit peut être utilisé pour échanger deux variables. Le XOR de deux nombres x et y renvoie un nombre dont tous les bits sont à 1 partout où les bits de x et y diffèrent. Par exemple, XOR de 10 (en binaire 1010) et 5 (en binaire 0101) est 1111 et XOR de 7 (0111) et 5 (0101) est (0010).

# Python program to demonstrate
# Swapping of two variables

x = 10
y = 50

# Swapping using xor
x = x ^ y
y = x ^ y
x = x ^ y

print("Value of x:", x)
print("Value of y:", y)

Sortir

Value of x: 50
Value of y: 10

Méthode 4 : À l’aide d’opérateurs arithmétiques, nous pouvons effectuer un échange de deux manières.

  • Utilisation de l'opérateur d'addition et de soustraction :

L'idée est d'obtenir la somme dans l'un des deux nombres donnés. Les nombres peuvent ensuite être échangés en utilisant la somme et la soustraction de la somme.

# Python program to demonstrate
# swapping of two variables

x = 10
y = 50

# Swapping of two variables
# using arithmetic operations
x = x + y 
y = x - y 
x = x - y 

print("Value of x:", x)
print("Value of y:", y)

Sortir

Value of x: 50
Value of y: 10
  • Utilisation de l'opérateur de multiplication et de division :

L'idée est d'obtenir la multiplication des deux nombres donnés. Les nombres peuvent ensuite être calculés en utilisant la division.

# Python program to demonstrate
# swapping of two variables

x = 10
y = 50

# Swapping of two numbers
# Using multiplication operator

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

print("Value of x : ", x)
print("Value of y : ", y)

Sortir

Value of x :  50.0
Value of y :  10.0

Méthode 5 : utilisation de l'addition et de la soustraction au niveau du bit pour l'échange.

#Python program to demonstrate
#swapping of two numbers
a = 5
b = 1
a = (a & b) + (a | b)
b = a + (~b) + 1
a = a + (~b) + 1
print("a after swapping: ", a)
print("b after swapping: ", b)

Sortir

a after swapping:  1
b after swapping:  5
1.10 GEEK