# Python program to alternate elements operation on tuple

Here, we have a tuple consisting of multiple elements. We need to create a python program that will perform alternate elements operations on tuples.
Submitted by Shivang Yadav, on September 30, 2021

Python programming language allows its users to work on data structures and manipulate them based on some given condition. In this program, we have a tuple consisting of multiple elements and perform the task of alternate element operation on tuples the operation is chain sum.

```Input:
(5, 1, 7, 3, 9)

Output:
Alternate chain sum = 1 + 3 = 4
Alternate chain sum = 5 + 7 + 9 = 21
```

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

```tuple = ("python", "includehelp", 43, 54.23)
```

## Performing alternate elements operation on tuple

We have a tuple consisting of multiple elements and we need to perform an operation on alternate elements of the tuple i.e. operations will consider either even or odd values of the tuple for one iteration.

For performing this task we will be traversing the tuple and then performing operations on alternate elements of the tuple.

Python programming language provides multiple methods to solve the problem. Here, we will be discussing some of them.

Method 1:

One method is to simply loop over the tuple and find the elements that have an alternate occurrence index. For this, we will be using the enumerate() method and then alternate the elements using the conditional operator.

```# Python program to Alternate Elements Operation
# on Tuple

# Creating and printing tuple list
myTuple = (3, 1, 6, 4, 9)
print("Initial values of tuple : " + str(myTuple))

# Performing alternate element operation
sumEven = 0
sumOdd = 0
for idx, value in enumerate(myTuple):
if idx % 2:
sumEven += value
else :
sumOdd += value

# Printing alternate elements operation
print("The alternate elements sum for even index values : " + str(sumEven))
print("The alternate elements sum for odd index values : " + str(sumOdd))
```

Output:

```Initial values of tuple : (3, 1, 6, 4, 9)
The alternate elements sum for even index values : 5
The alternate elements sum for odd index values : 18
```

Method 2:

Another method to solve the problem is by using the slice operator which will extract the elements of the tuple from the required alternate index.

```# Python program to Alternate Elements Operation
# on Tuple

# Creating and printing tuple list
myTuple = (3, 1, 6, 4, 9)
print("Initial values of tuple : " + str(myTuple))

# Performing alternate element operation
sumOdd = sum(myTuple[1::2])
sumEven = sum(myTuple[0::2])

# Printing alternate elements operation
print("The alternate elements sum for even index values : " + str(sumEven))
print("The alternate elements sum for odd index values : " + str(sumOdd))
```

Output:

```Initial values of tuple : (3, 1, 6, 4, 9)
The alternate elements sum for even index values : 18
The alternate elements sum for odd index values : 5
```

Preparation

What's New

Top Interview Coding Problems/Challenges!