7. Python程序打印数组中位于偶数位置的元素

2025 年 3 月 17 日 | 阅读 1 分钟

在这个程序中,我们需要打印位于偶数位置的元素。可以通过遍历数组并将 i 的值递增 2 来找到位于偶数位置的元素。

Python program to print the elements of an array present on even position

在上面的数组中,位于偶数位置的元素是 b 和 d。

算法

  • 步骤 1: 声明并初始化一个数组。
  • 步骤 2:计算声明的数组的长度。
  • 步骤 3:通过将变量 "i" 的值初始化为 1(因为第一个位于偶数位置的元素位于 i = 1)然后将其值递增 2,即 i=i+2,来循环遍历数组。
  • 步骤 4:打印位于偶数位置的元素。

程序

输出

Elements of given array present on even position:
2
4
下一个主题Python 程序