Exercise of the python list

1. Exercise 1

Assuming this list below:
names = [ 'fentiao', 'fendai', 'Fensi', 'Apple']
output is: 'I have fentiao, fendai, fensi and apple.'

names = ['fentiao', 'fendai', 'fensi', 'apple']
print('I have ' + ','.join(names[:-1]) + ' and ' + names[-1])

Output:
Here Insert Picture Description

2. English II

Enter a year on a certain date (yyyy-MM-dd), judgment day is the day of the year?

cal = input('输入日期 yyyy-mm-dd:')
date = cal.split('-') #拆分日期
print(date)
year = int(date[0])
month = int(date[1])
day = int(date[2])
arr = [0,31,28,31,30,31,30,31,31,30,31,30,31]
num = 0

if ((year % 4 ==0) and (year % 100 !=0) or(year % 400 ==0)):
    arr[2] = 29

for i in range(1,len(arr)):
    if month > i:
        num += arr[i]
    else:
        num += day
        break

print('天数:',num)

Output:
Here Insert Picture Description

3. Practice three

There is a list that includes 10 elements,
for example, the list is [1,2,3,4,5,6,7,8,9,0],
requires that each element of a list move forward one position ,
the first element to the last in the list, and then output the list.
The final style is [2,3,4,5,6,7,8,9,0,1]

l1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
l2 = l1.pop(0)
l1.append(l2)
print(l1)

Output:
Here Insert Picture Description

4. Exercise four

Request operation implemented in the following list of:
generating a list, which has 40 elements and each element is a random integer of 50 to 100
if the data in this list represent a class score 40, calculate results the number of students score below average
on the above list of elements descending order and outputs li.sort (reverse = True)

import random
score = []

# 循环40次
for count  in range(40):
    num = random.randint(50,100)
    score.append(num)

print('40人的分数为:',score)
sum_score = sum(score)
print(sum_score)
ave_num = sum_score/40
# 将小于平均成绩的成绩找出来 组成新的列表 并求列表的长度
less_ave = []
for i in score:
    if i < ave_num:
        less_ave.append(i)
long = len(less_ave)
print(long)
print('平均分数为:%.1f' %(ave_num))
print('有%d个学生低于平均分数:'%(long))

score.sort(reverse=True)
print('排序结果:',score)

Output:
Here Insert Picture Description

Published 60 original articles · won praise 6 · views 1377

Guess you like

Origin blog.csdn.net/weixin_45775963/article/details/103630734