In this python tuple program, we will create a tuple having squares of the elements from the list.
Steps to solve the program
- Create a tuple and convert it to a list using list().
- Use for loop to iterate over elements in the list.
- Find the index of the element using index().
- Get the square of the element.
- Now replace the square with the old element in the list.
- Now convert that list to a tuple using tuple().
- Print the output.
tup = (1,3,5,7,6)
print("Origianl tuple: ",tup)
a = list()
for i in list(tup):
b = i**2
a.append(b)
tup = tuple(a)
print("After sqauring elements: ",tup)
Output :
Origianl tuple: (1, 3, 5, 7, 6)
After sqauring elements: (1, 9, 25, 49, 36)
multiply adjacent elements of a tuple.
1 thought on “Create a tuple having squares of the elements from the list”