Table of Contents
show
Exchange the values of two variables,
Methods
Using temporary variable
Swapping two numbers using temporary variable
def swapping(a,b):
print("Before Swapping")
print("a=",a)
print("b=",b)
temp = a
a=b
b=temp
print("After Swapping")
print("a=",a)
print("b=",b)
print("Swapping two numbers")
x = input('Enter first number ')
y = input('Enter second number ')
swapping(x,y)
Output
Swapping two numbers
Enter first number 5
Enter second number 10
Before Swapping
('a=', 5)
('b=', 10)
After Swapping
('a=', 10)
('b=', 5)
Without using temporary variable
Tuple assignment
Swapping two numbers using Tuple Assignment
a = input("Enter first number ")
b = input("Enter second number ")
print("Before Swapping")
print("a=",a)
print("b=",b)
(a,b) = (b,a)
print("After Swapping")
print("a=",a)
print("b=",b)
Output
Enter first number 5
Enter second number 9
Before Swapping
('a=', 5)
('b=', 9)
After Swapping
('a=', 9)
('b=', 5)
Using addition and subtraction
def swapping(a,b):
print("Before Swapping")
print("a=",a)
print("b=",b)
a=a+b
b = a-b
a= a-b
print("After Swapping")
print("a=",a)
print("b=",b)
print("Swapping two numbers")
x = input('Enter first number ')
y = input('Enter second number ')
swapping(x,y)
Output
Swapping two numbers
Enter first number 5
Enter second number 10
Before Swapping
('a=', 5)
('b=', 10)
After Swapping
('a=', 10)
('b=', 5)
Using multiplication and division
def swapping(a,b):
print("Before Swapping")
print("a=",a)
print("b=",b)
a=a*b
b = a/b
a= a/b
print("After Swapping")
print("a=",a)
print("b=",b)
print("Swapping two numbers")
x = input('Enter first number ')
y = input('Enter second number ')
swapping(x,y)
Output
Swapping two numbers
Enter first number 5
Enter second number 10
Before Swapping
('a=', 5)
('b=', 10)
After Swapping
('a=', 10)
('b=', 5)
Using xor operator
def swapping(a,b):
print("Before Swapping")
print("a=",a)
print("b=",b)
a=a^b
b = a^b
a= a^b
print("After Swapping")
print("a=",a)
print("b=",b)
print("Swapping two numbers")
x = input('Enter first number ')
y = input('Enter second number ')
swapping(x,y)
Output
Swapping two numbers
Enter first number 5
Enter second number 10
Before Swapping
('a=', 5)
('b=', 10)
After Swapping
('a=', 10)
('b=', 5)
Circulate the values of n variables
list1 = list(input('enter the list'))
print("Original List :" , list1)
print("circulating the values in the list")
for i in range(1, len(list1)):
print(list1[i:] +list1[:i])
Output
enter the list 10,20,30,40,50
('Original List :', [10, 20, 30, 40, 50])
circulating the values in the list
[20, 30, 40, 50, 10]
[30, 40, 50, 10, 20]
[40, 50, 10, 20, 30]
[50, 10, 20, 30, 40]
Distance between two points
import math
x1 = input("Enter x co-ordinate of point 1 : ")
y1 = input("Enter y co-ordinate of point 1 : ")
x2 = input("Enter x co-ordinate of point 2 : ")
y2 = input("Enter y co-ordinate of point 2 : ")
distance = math.sqrt((x2-x1)**2 + (y2-y1)**2)
print("Distance between two points", distance)
Output:
Enter x co-ordinate of point 1 : 4
Enter y co-ordinate of point 1 : 5
Enter x co-ordinate of point 2 : 2
Enter y co-ordinate of point 2 : 6
('Distance between two points', 2.23606797749979)
Views: 0