Python 程序:列印偶數位置列表項

寫一個 Python 程序,在偶數位置甚至索引位置列印列表項。在這個 Python 偶數位置示例中,列表切片從 1 開始,在列表末尾結束,並遞增 2。

evList = [3, 6, 9, 11, 13, 15, 17, 19]

print('Printing the List Items at Even Position')
print(evList[1:len(evList):2])

Python 程序使用 for 循環在偶數索引位置列印列表項。

evList = [17, 24, 36, 48, 55, 79, 82, 93]

print('Printing the List Items at Even Position')
for i in range(1, len(evList), 2):
    print(evList[i], end = '  ')
Printing the List Items at Even Position
24  48  79  93 

Python 程序,使用 while 循環在偶數位置列印列表項

evList = [90, 120, 240, 180, 80, 60]

print('Printing the List Items at Even Position')
i = 1
while i < len(evList):
    print(evList[i], end = '  ')
    i = i + 2
Printing the List Items at Even Position
120  180  60 

在這個 Python 示例中,for 循環從 0 迭代到列表長度。if 條件檢查索引位置除以二等於 1。如果為真,則列印偶數位置列表項。

evlist = []
evListTot = int(input("Total List Items to enter = "))

for i in range(1, evListTot + 1):
    evListvalue = int(input("Please enter the %d List Item = "  %i))
    evlist.append(evListvalue)

print('\nPrinting the List Items at Even Position')
for i in range(1, len(evlist), 2):
    print(evlist[i], end = '  ')

print('\nPrinting the List Items at Even Position')
for i in range(len(evlist)):
    if i % 2 != 0:
        print(evlist[i], end = '  ')
Total List Items to enter = 8
Please enter the 1 List Item = 12
Please enter the 2 List Item = 23
Please enter the 3 List Item = 34
Please enter the 4 List Item = 45
Please enter the 5 List Item = 56
Please enter the 6 List Item = 67
Please enter the 7 List Item = 78
Please enter the 8 List Item = 89

Printing the List Items at Even Position
23  45  67  89  
Printing the List Items at Even Position
23  45  67  89 

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/285663.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-22 15:45
下一篇 2024-12-22 15:45

相關推薦

  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智慧、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • ArcGIS更改標註位置為中心的方法

    本篇文章將從多個方面詳細闡述如何在ArcGIS中更改標註位置為中心。讓我們一步步來看。 一、禁止標註智能調整 在ArcMap中設置標註智能調整可以自動將標註位置調整到最佳顯示位置。…

    編程 2025-04-29
  • Python字元轉列表指南

    Python是一個極為流行的腳本語言,在數據處理、數據分析、人工智慧等領域廣泛應用。在很多場景下需要將字元串轉換為列表,以便於操作和處理,本篇文章將從多個方面對Python字元轉列…

    編程 2025-04-29
  • Python程序文件的拓展

    Python是一門功能豐富、易於學習、可讀性高的編程語言。Python程序文件通常以.py為文件拓展名,被廣泛應用於各種領域,包括Web開發、機器學習、科學計算等。為了更好地發揮P…

    編程 2025-04-29
  • Python中不同類型的列表

    Python是一種功能強大的編程語言,其內置數據結構之一為列表。列表可以容納任意數量的元素,並且可以存儲不同類型的數據。 一、列表的基本操作 Python的列表類型支持許多操作,如…

    編程 2025-04-29
  • Python購物車程序

    Python購物車程序是一款基於Python編程語言開發的程序,可以實現購物車的相關功能,包括商品的添加、購買、刪除、統計等。 一、添加商品 添加商品是購物車程序的基礎功能之一,用…

    編程 2025-04-29
  • Python定義函數判斷奇偶數

    本文將從多個方面詳細闡述Python定義函數判斷奇偶數的方法,並提供完整的代碼示例。 一、初步了解Python函數 在介紹Python如何定義函數判斷奇偶數之前,我們先來了解一下P…

    編程 2025-04-29
  • 爬蟲是一種程序

    爬蟲是一種程序,用於自動獲取互聯網上的信息。本文將從如下多個方面對爬蟲的意義、運行方式、應用場景和技術要點等進行詳細的闡述。 一、爬蟲的意義 1、獲取信息:爬蟲可以自動獲取互聯網上…

    編程 2025-04-29
  • Vb運行程序的三種方法

    VB是一種非常實用的編程工具,它可以被用於開發各種不同的應用程序,從簡單的計算器到更複雜的商業軟體。在VB中,有許多不同的方法可以運行程序,包括編譯器、發布程序以及命令行。在本文中…

    編程 2025-04-29

發表回復

登錄後才能評論