Write a Python programme that switch the values of the two variables, x and y, provided. Let's examine many Python approaches for doing this task.
method1:
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)
method 2:using comma operator
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)
Method 3: Using arithmetic operators we can perform swapping in two ways.
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)
Using multiplication and division operator :
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)
#PythonTutorial #PythonForBeginners #LearnPython #SwappingVariables #Coding #Programming #PythonTips #TechTutorial #PythonProgramming #CodeNewbie #python #beginner #tutorial #variables #swap #coding
Connect With Us:
—————————————
➡️ Website: https://www.cybrosys.com/
➡️ Email: info@cybrosys.com
➡️ Twitter: / cybrosys
➡️ LinkedIn: / cybrosys
➡️ Facebook: / cybrosystechnologies
➡️ Instagram: / cybrosystech
➡️ Pinterest: / cybrosys
Nesta página do site você pode assistir ao vídeo on-line How to Swap Variables in Python | EP-6 Swapping Variables | Python for Beginners | Free Tutorials duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Cybrosys Technologies 01 Julho 2024, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 364 vezes e gostou 12 espectadores. Boa visualização!