新聞中心
創(chuàng)新互聯(lián)Python教程:

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比洛陽網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式洛陽網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋洛陽地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴。
寫一個(gè) Python 程序,用一個(gè)實(shí)例打印列表中的元素。
打印列表元素的 Python 程序示例 1
Python 打印功能自動(dòng)打印列表中的元素。
# 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
這個(gè) python 程序同上。但是這次,我們使用 For Loop 來迭代列表中的每個(gè)元素,并打印該元素。這種方法對(duì)于使用這個(gè) Python 索引位置改變單個(gè)項(xiàng)目非常有用。
# 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 打印函數(shù)還打印列表中的字符串元素。
# Python Program to print Elements in a List
Fruits = ['Apple', 'Orange', 'Grape', 'Banana']
print("Element in this List are : ")
print(Fruits)打印列表項(xiàng)輸出
Element in this List are :
['Apple', 'Orange', 'Grape', 'Banana']
返回列表元素的 Python 程序示例 4
這個(gè) Python 列表項(xiàng)目程序和上面的一樣。但是,我們使用 For 循環(huán)來迭代列表中的每個(gè)字符串元素。
# 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
這個(gè) Python 程序允許用戶輸入任意整數(shù)值,是一個(gè) List 的長(zhǎng)度。接下來,我們使用 For 循環(huán)向列表中添加數(shù)字。
# 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程序:打印列表中元素
網(wǎng)頁鏈接:http://m.fisionsoft.com.cn/article/cdjdjoe.html


咨詢
建站咨詢
