Write a program to print swapping variables

Write a program to print swapping variables

Program

def myfun(): 
    x, y = 10, 20
    x, y = y, x
    print('x value is : ', x)
    print('y value is : ', y)
             
if __name__=="__main__":
    myfun()


Output:

x value is : 20
y value is : 10


Using the XOR (exclusive or) operator

def myfun(): 
    x, y = 10, 20
    # step one
    x ^= y
    # step two
    y ^= x
    # step three
    x ^= y
    print('x value is : ', x)
    print('y value is : ', y)
             
if __name__=="__main__":
    myfun()



Output:

x value is : 20
y value is : 10



More Questions


32 . Write a program to print swapping variables
33 . Write a python program to counting item occurrences
34 . Write a python program to print flatten a nested list
35 . Write a python program to Find the index of the largest number in the list
36 . Write a python program to Find the index of the smallest number in the list
37 . Write a python program to find absolute value of a number in the list
38 . Write a python program to adding a thousand separator
39 . Write a python program startswith and Endswith Methods
40 . Write a python program to print Nlargest numbers
41 . Write a python program to print Nsmallest numbers



For latest job updates join Telegram Channel: https://t.me/sateeshm