新聞中心
這里有您想知道的互聯網營銷解決方案
Python程序:打印列表中元素
創(chuàng)新互聯Python教程:

創(chuàng)新互聯建站-專業(yè)網站定制、快速模板網站建設、高性價比洛陽網站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式洛陽網站制作公司更省心,省錢,快速模板網站建設找我們,業(yè)務覆蓋洛陽地區(qū)。費用合理售后完善,十年實體公司更值得信賴。
寫一個 Python 程序,用一個實例打印列表中的元素。
打印列表元素的 Python 程序示例 1
Python 打印功能自動打印列表中的元素。
# Python Program to print Elements in a List
a = [10, 50, 60, 80, 20, 15]
print("Element in this List are : ")
print(a)
Element in this List are :
[10, 50, 60, 80, 20, 15]
返回列表元素的 Python 程序示例 2
這個 python 程序同上。但是這次,我們使用 For Loop 來迭代列表中的每個元素,并打印該元素。這種方法對于使用這個 Python 索引位置改變單個項目非常有用。
# Python Program to print Elements in a List
a = [10, 50, 60, 80, 20, 15]
print("Element in this List are : ")
for i in range(len(a)):
print("Element at Position %d = %d" %(i, a[i]))
Element in this List are :
Element at Position 0 = 10
Element at Position 1 = 50
Element at Position 2 = 60
Element at Position 3 = 80
Element at Position 4 = 20
Element at Position 5 = 15
顯示列表元素的 Python 程序示例 3
Python 打印函數還打印列表中的字符串元素。
# Python Program to print Elements in a List
Fruits = ['Apple', 'Orange', 'Grape', 'Banana']
print("Element in this List are : ")
print(Fruits)打印列表項輸出
Element in this List are :
['Apple', 'Orange', 'Grape', 'Banana']
返回列表元素的 Python 程序示例 4
這個 Python 列表項目程序和上面的一樣。但是,我們使用 For 循環(huán)來迭代列表中的每個字符串元素。
# Python Program to print Elements in a List
Fruits = ['Apple', 'Orange', 'Grape', 'Banana']
print("Element in this List are : ")
for fruit in Fruits:
print(fruit)
Element in this List are :
Apple
Orange
Grape
Banana
顯示列表元素的 Python 程序示例 5
這個 Python 程序允許用戶輸入任意整數值,是一個 List 的長度。接下來,我們使用 For 循環(huán)向列表中添加數字。
# Python Program to print Elements in a List
NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
for i in range(Number):
print("The Element at index position %d = %d " %(i, NumList[i])) 網站欄目:Python程序:打印列表中元素
網頁網址:http://m.5511xx.com/article/cdjdjoe.html


咨詢
建站咨詢
