《Python编程从入门到实践》——动手试一试
列表的题目
书本50页
competitors=["jacklove",'theshy','Feaker',"Rookie"]
for x in competitors:
print(x.title())
print(x.title+' could be the MVP.')
print('I really like them!!')
#输出:
Jacklove
Jacklove could be the MVP.
Theshy
Theshy could be the MVP.
Feaker
Feaker could be the MVP.
Rookie
Rookie could be the MVP.
I really like them!!
书本54页
for x in range(1,21):
print(x)#回车太多了,占行数,我就不写出来了
num=[x for x in range(1,1000001)]
print(sum(num))
print(max(num))
print(min(num))
num=[]
for x in range(1,21,2):
num.append(x)
print(num)
num=[]
for x in range(3,31,3):
num.append(x)
print(num)
num=[]
for x in range(1,11):
num.append(x**3)
print(num)
num=[x**3 for x in range(1,11)]
print(num)
500000500000
1000000
1
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
书本58页
切片:
competitors=["jacklove",'theshy','Feaker',"Rookie",'Ning']
print('First three competitors: '+str(competitors[:3]))
print('Last three competitors: '+str(competitors[2:]))
print('Middle three competitors: '+str(competitors[1:4]))
First three competitors: ['jacklove', 'theshy', 'Feaker']
Last three competitors: ['Feaker', 'Rookie', 'Ning']
Middle three competitors: ['theshy', 'Feaker', 'Rookie']
列表的传递(copy),循环输出列表
competitors=["jacklove",'theshy','Feaker',"Rookie",'Ning']
copy=[]
copy=competitors[:]
competitors.append('Zoom')
copy.append("samme")
print(str(competitors).title())
print(str(copy).title())
for x in competitors:
print(x.title())
print('\n')
for x in copy:
print(x.title())
['Jacklove', 'Theshy', 'Feaker', 'Rookie', 'Ning', 'Zoom']
['Jacklove', 'Theshy', 'Feaker', 'Rookie', 'Ning', 'Samme']
Jacklove
Theshy
Feaker
Rookie
Ning
Zoom
Jacklove
Theshy
Feaker
Rookie
Ning
Samme
书本60页
competitors=("jacklove",'theshy','Feaker',"Rookie",'Ning')
for x in competitors:
print(str(x).title())
#
competitors=("jacklove",'theshy','Feaker',"Rookie",'Ning')
competitors[0]='UZI'
for x in competitors:
print(str(x).title())
Jacklove
Theshy
Feaker
Rookie
Ning
#
TypeError: 'tuple' object does not support item assignment
元组若想改变其中的元素,只能再次赋值。
competitors=("jacklove",'theshy','Feaker',"Rookie",'Ning')
print(competitors)
competitors=("jacklove",'theshy','369',"Rookie",'leyan')
print(competitors)
competitors=("jacklove",'theshy','Feaker',"Rookie",'Ning')
print(competitors)
competitors=("jacklove",'theshy','369',"Rookie",'leyan')
print(competitors)
('jacklove', 'theshy', 'Feaker', 'Rookie', 'Ning')
('jacklove', 'theshy', '369', 'Rookie', 'leyan')
格式设置指南
(https://www.python.org/dev/peps/pep-0008/)
人家专业写的就是好看。自行查阅吧(●’◡’●)
my_list = [
1, 2, 3,
4, 5, 6,
]
result = some_function_that_takes_arguments(
'a', 'b', 'c',
'd', 'e', 'f',
)