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
On this page of the site you can watch the video online How to Swap Variables in Python | EP-6 Swapping Variables | Python for Beginners | Free Tutorials with a duration of hours minute second in good quality, which was uploaded by the user Cybrosys Technologies 01 July 2024, share the link with friends and acquaintances, this video has already been watched 364 times on youtube and it was liked by 12 viewers. Enjoy your viewing!