Programs using Tuples
Program 1: Write a program to swap two values using
tuple assignment
a = int(input("Enter value
of A: "))
b = int(input("Enter value
of B: "))
print("Value of A = ",
a, "\n Value of B = ", b)
(a, b) = (b, a)
print("Value of A = ",
a, "\n Value of B = ", b)
Output:
Enter value of A: 54
Enter value of B: 38
Value of A = 54
Value of B = 38
Value of A = 38
Value of B = 54
Program 2: Write a program using a function that
returns the area and circumference of a circle whose radius is passed as an
argument.two values using tuple assignment
pi = 3.14
def Circle(r):
return (pi*r*r, 2*pi*r)
radius = float(input("Enter
the Radius: "))
(area, circum) = Circle(radius)
print ("Area of the circle =
", area)
print ("Circumference of the
circle = ", circum)
Output:
Enter the Radius: 5
Area of the circle = 78.5
Circumference of the circle =
31.400000000000002
Program 3: Write a program that has a list of
positive and negative numbers. Create a new tuple that has only positive
numbers from the list
Numbers = (5, -8, 6, 8, -4, 3, 1)
Positive = ( )
for i in Numbers:
if i > 0:
Positive += (i, )
print("Positive Numbers:
", Positive)
Output:
Positive Numbers: (5, 6, 8, 3, 1)
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.