**笔记30 笨办法学python练习38之二,列表的转换与增添,用for替换while**

笔记30 笨办法学python练习38之二,列表的转换与增添,用for替换while

重温了for和while这两个函数的意义之后,开始做练习ex38.py。代码的录入和执行都很顺利,对于这个代码的涵义好像比以前对于代码的理解要深入一些了。顺利执行之后,对于代码的差不多每一行都做了注释,注释做好后形成的ex38.4.py再执行,依然是很顺利。我将在给出注释和执行之后,按照巩固练习的要求,再做一个ex38.5,用for取代while,并相应更改一些列表的数字来形成ex38.5.py,再予以执行,从中理解for与while的差异,以及列表的有关操作的涵义。
练习ex38.4py

在这里插入代码片#一个称十样东西的变量,赋值的只有一个项目的字符串
ten_things = "Apples Oranges Crows Telephone Light Sugar"
#所以,需要给ten字符串分离成若干项目,以和变量名相称,打印这个想法
print("Wait there are not 10 things in that list, Let's fix that.")
#现在另有变量stuff,它可以把ten变量分离,用函数split
stuff = ten_things.split(' ')
#又有一个变量more_stuff是一个8位列表
more_stuff = ["Day", "Night", "Song", "Frisbee",
               "Corn", "Banana", "Girl", "Boy"]
#我们有三个变量均赋值,现在看对这些变量的值做出什么样的改变
#对于不等于10长度的,变量stuff做while循环
while len(stuff) != 10:
    next_one = more_stuff.pop() #在morestuff中拿走一些元素
    print("Adding: ", next_one) #这些拿走的元素加到next_one
    stuff.append(next_one)
    print(f"there are {len(stuff)} items now.")
#在stuff中加到10项了,做成了一个十项的列表了
print("There we go: ", stuff)

print("Let's do some things with stuff.")
#打印列表的第二个元素[1]oranges
print(stuff[1])
#打印列表的最后一个元素[-1]corn
print(stuff[-1]) # whoa! fancy
print(stuff.pop()) #把corn这个元素拿走
# 把stuff的十个元素去掉引号' '展开
print(' '.join(stuff)) #what? cool
# 在列表的排列4后加上#,使得3与5之间有星号#
print('#'.join(stuff[3:5])) #super stellar

练习ex38.4.py的执行结果`在这里插入代码片PS
PS C:\Users\lenovo\1pythonw> python ex38.4.py
Wait there are not 10 things in that list, Let’s fix that.
Adding: Boy
there are 7 items now.
Adding: Girl
there are 8 items now.
Adding: Banana
there are 9 items now.
Adding: Corn
there are 10 items now.
There we go: [‘Apples’, ‘Oranges’, ‘Crows’, ‘Telephone’, ‘Light’, ‘Sugar’, ‘Boy’, ‘Girl’, ‘Banana’, ‘Corn’]
Let’s do some things with stuff.
Oranges
Corn
Corn
Apples Oranges Crows Telephone Light Sugar Boy Girl Banana
Telephone#Light
PS C:\Users\lenovo\1pythonw>

做成ex38.5.py后发现,while后带的字码格式与for后带的字码格式有些区别。For常后带某物in某空间,while则是一个等号和等值号的语句,或者是真假的关键字。此外,后带的print的缩进也有区别。执行结果似乎还没有看到区别。
练习38的扩展ex38.5.py

在这里插入代码片#一个称十样东西的变量,赋值的只有一个项目的字符串
six_things = "Apples Oranges Crows Telephone Light Sugar"
#所以,需要给ten字符串分离成若干项目,以和变量名相称,打印这个想法
print("Wait there are not 8 things in that list, Let's fix that.")
#现在另有变量stuff,它可以把ten变量分离,用函数split
stuff = six_things.split(' ')
#又有一个变量more_stuff是一个8位列表
name_stuff = ["zhao", "qian", "sun", "li",
               "zhou", "wu", "zheng", "wang"]
#我们有三个变量均赋值,现在看对这些变量的值做出什么样的改变
#对于不等于10长度的,变量stuff做while循环
for name in name_stuff:
    next_one = name_stuff.pop() #在morestuff中拿走一些元素
    print("Adding: ", next_one) #这些拿走的元素加到next_one
    stuff.append(next_one)
    print(f"there are {len(stuff)} items now.")
#在stuff中加到10项了,做成了一个十项的列表了
print("There we go: ", stuff)

print("Let's do some things with stuff.")
#打印列表的第二个元素[1]oranges
print(stuff[1])
#打印列表的最后一个元素[-1]corn
print(stuff[-1]) # whoa! fancy
print(stuff.pop()) #把corn这个元素拿走
# 把stuff的十个元素去掉引号' '展开
print(' '.join(stuff)) #what? cool
# 在列表的排列4后加上#,使得3与5之间有星号#
print('&'.join(stuff[4:6])) #super stellar

练习ex38.5.py的执行

在这里插入代码片PS C:\Users\lenovo\1pythonw> python ex38.5.py
Wait there are not 8 things in that list, Let's fix that.
Adding:  wang
there are 7 items now.
Adding:  zheng
there are 8 items now.
Adding:  wu
there are 9 items now.
Adding:  zhou
there are 10 items now.
There we go:  ['Apples', 'Oranges', 'Crows', 'Telephone', 'Light',
Let's do some things with stuff.
Oranges
zhou
zhou
Apples Oranges Crows Telephone Light Sugar wang zheng wu
Light&Sugar
PS C:\Users\lenovo\1pythonw> python ex38.4.py

这个练习,对于巩固练习中提到的调用函数的意思有了更进一步的理解。如教材所说,在more_stuff列表中调用函数pop,这其实就是一个python字符串的对译,它在python中可以翻译为more_stuff.pop()。这个翻译出来的东西,在python中还可以另外表示为另一个指令符:pop(more_stuff),翻译为中文大概是,参数为more_stuff的函数pop。
Python中的class有和自然语言相通之处,但差别可能很大,也许再阅读完成几个练习,对于class,对于面向对象编程object oriented programming,还有一些相关的python语言中的字符,就会有更多的认知。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值