
How to Swap Variables in Python | EP-6 Swapping Variables | Python for Beginners | Free Tutorials
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: www.cybrosys.com/
➡️ Email: info@cybrosys.com
➡️ Twitter: twitter.com/cybrosys
➡️ LinkedIn: www.linkedin.com/company/cybrosys/
➡️ Facebook: www.facebook.com/cybrosystechnologies
➡️ Instagram: www.instagram.com/cybrosystech/
➡️ Pinterest: pinterest.com/cybrosys/
コメント