2. Python programming using simple statements and expressions (exchange the values of two variables, circulate the values of n variables, distance between two points).
2. Python programming using simple statements and expressions (exchange the values of two variables, circulate the values of n variables, distance between two points).
# 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)
Program to circulate the values of n variables
from collections import deque
lst=[1,2,3,4,5]
d=deque(lst)
print d
d.rotate(2)
print d
Output:[3,4,5,1,2]
(OR)
list=[10,20,30,40,50]
n=2 #Shift 2 location
list[n:]+list[:n]
Output: [30,40,50,10,20]
3. Distance between two points
import math
p1 = [4, 0]
p2 = [6, 6]
distance = math.sqrt( ((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2) )
print(distance)
Comments
Post a Comment