python列表練習題


昨天我們學習了python中的列表,也做了一個小的練習題“用python做一個簡單的購物車”,這個功能已經實現,但是怕遺忘了,現在又從網上找了些練習題,再加強一下。OK,GO!!

題目的要求如下:

 

 

下面,我們就針對以上題目做這次練習。

1.創建一個空的列表score

score = []

2.利用while循環和append函數在scroe列表中添加10個數值:

i = 1
while i <= 10 :
    j = input('請輸入10個數值(每次一個):')
    print('還剩'+str(10-i)+'個')
    score.append(j)
    i += 1

 3.輸出score列表中第3個元素的數值:

print(score[2])

4.輸出score列表中第1-6個元素的值

print(score[0:6])

5.利用insert函數,在score列表中的第3個元素之前添加數值59

score.insert(2,'59')

6.利用變量num保存數值76,調用count函數,查詢變量num變量值在score列表中出現的次數

num = str('76')
a = score.count(num)
print(a)

 7.使用in查詢score列表中是否有num變量的考試成績

num = str('76')
if num in score:
    print('有')
else:
    print('沒有')

 8.調用index函數,查詢score列表中成績是滿分的學生學號

a = score.index('100')
print(a)

 9.在score列表中,將59分加1分

a = score.index('59')
b = str(59+1)
score.pop(a)
score.insert(a, b)
#或者
score[a] = b

 10.調用del函數,刪除列表中第一個元素

del score[0]

11.調用len函數獲得score列表中元素的個數

len(score)

12.調用sort函數,對列表中的元素進行排序,輸出考試的最高分和最低分

這個題說想求最高分和最低分,但是現有的知識還真不知道怎么解決,我覺得應該是開始的值和最后的值吧

score1 = ['68', '87', '59', '92', '100', '76', '88', '54', '89', '76', '61']
score1.sort()
a = score1[0]
b = score1[-1]
print(a)
print(score1)
print(b)

 

13.調用reverse函數,顛倒score列表中的順序

score.reverse()

14.調用pop函數,刪除score列表中尾部的元素,返回刪除了的元素

a = score.pop(-1)
print(a)

15.score列表中,追加數值88,並輸出。調用remove函數,刪除score列表中的第一個88

score.append('88')
print(score)
score.remove('88')

16.創建2個列表score1和score2,score1中包含2個元素值:80,61 ,score2中包含3個元素值,71,95,82,合並這2個列表,並輸出全部元素

score1 = ['80', '61']
score2 = ['71', '95', '82']
score1.extend(score2)
print(score1)

17.創建score1列表,其中包含2個數值:80,61,將score1 中的元素復制5次后保存在score2列表中,輸出score2列表中的全部元素。

score1 = ['80', '61']
score2 = []
score2 = score1*5
print(score2)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM