0% found this document useful (0 votes)
14 views4 pages

Ex No 2

Uploaded by

reorffggjhg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Ex No 2

Uploaded by

reorffggjhg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PYTHON PROGRAMMING USING SIMPLE STATEMENTS AND EXPRESSIONS

Ex. No: 2a
Exchange the Values of Two Variables

Aim:

To write a python program to exchange the values of two variables.

Exchange the values of two variables with the simple statement:

Algorithm

Step 1: Start.
Step 2: Read two variables a and b.
Step 3: Substitute the value of a by adding a and b.
Step 4: Substitute the value of b by subtracting a and b.
Step 5: Substitute the value of a by subtracting a and b.
Step 6: Print the exchanged value of a and b.
Step 7: Stop.

Program

a=int(input("Enter the first value: "))


b=int(input("Enter the second value: "))
a=a+b
b=a-b
a=a-b
print("The first value after exchange is: ",a)
print("The second value after exchange is: ",b)

Exchange the values of two variables with the simple expression:

Algorithm

Step 1: Start
Step 2: Read two variables a and b.
Step 3: Exchange the values of a and b by the expression a,b = b,a.
Step 4: Print the exchanged value of a and b.
Step 5: Stop.

Program

a=int(input("Enter the first value: "))


b=int(input("Enter the second value: "))
a,b = b,a
print("The first value after exchange is: ",a)
print("The second value after exchange is: ",b)

Conclusion

Thus, the exchange the values of two variables program is written and executed.
Ex. No: 2b
Circulate the Values of N Variables

Aim

To write a Python program for circulating the values of n variables.

Algorithm

Step 1: Start.
Step 2: Get the number of variables to be circulated as no_of_terms.
Step 3: Store the elements in a list.
Step 4: Each time, pop an element from the list, append it to the list, and print the list for every
pair of pop and append operations.
Step 5: Stop.

Program

no_of_terms = int(input("Enter number of values : "))


list1 = []
for val in range(0, no_of_terms, 1):
ele = int(input("Enter integer : "))
list1.append(ele)
print("Circulating the elements of list ", list1)

for val in range(0, no_of_terms, 1):


ele = list1.pop(0)
list1.append(ele)
print(list1)

Conclusion

Thus, the program for circulating the values of n variables is written and executed.
Ex. No: 2c
Distance Between Two Points

Aim

To write a Python program for finding the distance between two points.

Algorithm

Step 1: Start.
Step 2: Get the two points from the user (x1, y1) and (x2, y2).
Step 3: Calculate the distance between two points using the formula
Step 4: Display the calculated distance between two points.
Step 5: Stop

Program

x1=int(input("enter x1 : "))
x2=int(input("enter x2 : "))
y1=int(input("enter y1 : "))
y2=int(input("enter y2 : "))
result= ((((x2 - x1 )**2) + ((y2-y1)**2) )**0.5)
print("distance between",(x1,x2),"and",(y1,y2),"is : ",result)

Conclusion

Thus, the program for finding the distance between two points is written and executed.

You might also like