APP下载
报价宝  ›  科技  › 

Python爬虫小白基础必看100例项 Python练习资料(71到75)

报价宝 来源:baojiabao.com 发布时间:2019-10-28 22:57:00 09月16日更新
报价宝综合消息Python爬虫小白基础必看100例项 Python练习资料(71到75)

Python 练习例项资料、72、73、74、7(715)

71、题目:编写input()和output()函式输入,输出5个学生的资料记录。

程式分析:无。

程式源代码:

zil#!/usr/bin/python

# -*- coding: UTF-8 -*-

N = 3

#stu

# num : string

# name : string

# score[4]: list

student = []

for i in range(5):

student.append(['','',[]])

def input_stu(stu):

for i in range(N):

stu[i][0] = raw_input('input student num: ')

stu[i][1] = raw_input('input student name: ')

for j in range(3):

stu[i][2].append(int(raw_input('score: ')))

def output_stu(stu):

for i in range(N):

print '%-6s%-10s' % ( stu[i][0],stu[i][1] )

for j in range(3):

print '%-8d' % stu[i][2][j]

if __name__ == '__main__':

input_stu(student)

print student

output_stu(student)

结果:

input student num:

2

input student name:

aaa

score:

89

score:

98

score:

67

input student num:

bbb

input student name:

ccc

score:

87

score:

45

score:

68

input student num:

ddd

input student name:

eee

score:

56

score:

78

score:

56

[['2', 'aaa', [89, 98, 67]], ['bbb', 'ccc', [87, 45, 68]], ['ddd', 'eee', [56, 78, 56]], ['', '', []], ['', '', []]]

2 aaa

89

98

67

bbb ccc

87

45

68

ddd eee

56

78

56

72、

题目:建立一个连结串列。

程式分析:无。

#!/usr/bin/python

# -*- coding: UTF-8 -*-

if __name__ == '__main__':

ptr = []

for i in range(5):

num = int(raw_input('please input a number: '))

ptr.append(num)

print ptr

结果:

please input a number:

3

please input a number:

5

please input a number:

7

please input a number:

8

please input a number:

2

[3, 5, 7, 8, 2]

73、

题目:反向输出一个连结串列。

程式分析:无。

#!/usr/bin/python

# -*- coding: UTF-8 -*-

if __name__ == '__main__':

ptr = []

for i in range(5):

num = int(raw_input('please input a number: '))

ptr.append(num)

print ptr

ptr.reverse()

print ptr

结果:

please input a number:

6

please input a number:

5

please input a number:

3

please input a number:

4

please input a number:

8

[6, 5, 3, 4, 8]

[8, 4, 3, 5, 6]

74、

题目:列表排序及连线。

程式分析:排序可使用 sort() 方法,连线可以使用 + 号或 extend() 方法。

程式源代码:

#!/usr/bin/python

# -*- coding: UTF-8 -*-

if __name__ == '__main__':

a = [1,3,2]

b = [3,4,5]

a.sort() # 对列表 a 进行排序

print a

# 连线列表 a 与 b

print a+b

# 连线列表 a 与 b

a.extend(b)

print a

结果:

[1, 2, 3]

[1, 2, 3, 3, 4, 5]

[1, 2, 3, 3, 4, 5]

75、

题目:放松一下,算一道简单的题目。

程式分析:无。

#!/usr/bin/python

# -*- coding: UTF-8 -*-

if __name__ == '__main__':

for i in range(5):

n = 0

if i != 1: n += 1

if i == 3: n += 1

if i == 4: n += 1

if i != 4: n += 1

if n == 3: print 64 + i

结果:

67

私我带走干货

文章标签: 报价宝 降噪耳机价格 耳机价格 红米手机价格 华为手机价格 小米手机价格 电视机价格 笔记本电脑价格 笔记本价格 汽车价格 汽车价格 笔记本电脑价格 电视机价格 小米手机价格 耳机价格