《Python编程从入门到实践》习题02

《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',
    )
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值