Even indices. Given a list of numbers, find and print all the elements that are located at even index positions (i.e., A[0], A[2], A[4], …).
|
numbers = input().split() for i in range(0, len(numbers), 2): print(numbers[i]) |
Explanation of the Code
- Reading Input:
numbers = input().split()
- The
input()
function reads a line of text from the user.
- The
split()
method splits this line into a list of strings based on spaces and stores it in the variable numbers
.
- Iterating Through the List and Printing Even Index Elements:
for i in range(0, len(numbers), 2):
print(numbers[i])
- Use a
for
loop to iterate through the list numbers
with a step of 2, starting from index 0.
- In each iteration, print the element at the current index
i
.