- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to print the elements of an array present on odd position
When it is required to print the elements of a list that is present in odd index/position, a loop can be used to iterate over the elements, and only check the odd positions in the list by specifying the step size as 2 in the range function.
Below is a demonstration of the same −
Example
my_list = [31, 42, 13, 34, 85, 0, 99, 1, 3] print("The list is :") print(my_list) print("The elements in odd positions are : ") for i in range(1, len(my_list), 2): print(my_list[i])
Output
The list is : [31, 42, 13, 34, 85, 0, 99, 1, 3] The elements in odd positions are : 42 34 0 1
Explanation
A list is defined, and is displayed on the console.
The list is iterated over beginning from the second index element, and the step size is mentioned as 2 in the range method.
Those elements that are in odd positions are displayed on the console.
Advertisements