学习Python编程第011天

继续昨天没有打完的代码,虽然我学的慢,学的不多,但只要我每天在学,哪就要超过大多数人了。某些人看到我学的内容可能会感觉到我很低级,但是我不怕别人的嘲笑,我所学的是为了我自己。

>>> [1,2,3,]+['a','b','c']  #可以用+操作符连接两个列表

[1, 2, 3, 'a', 'b', 'c']

>>> [1,'a',3.14]*3  #可以用*操作符复制列表

[1, 'a', 3.14, 1, 'a', 3.14, 1, 'a', 3.14]

>>> spam=[1,2,3,]

>>> spam=spam+['a','b','c']  #可以赋值给同一个变量

>>> spam

[1, 2, 3, 'a', 'b', 'c']

>>> spam=['cat', 'bat', 'rat', 'elephant']

>>> del spam[2]  #可以直接删除下标处的值

>>> spam

['cat', 'bat', 'elephant']

>>> del spam[2]

>>> spam

['cat', 'bat']

>>> del spam  #还可以删出一整个变量

>>> spam  #删出后这个变量就不存在了,会出现NameError错误

Traceback (most recent call last):

  File "<pyshell#11>", line 1, in <module>

    spam

NameError: name 'spam' is not defined

print('Enter the name of cat 1:')

catName1 = input()

print('Enter the name of cat 2:' )

catName2 = input()

print('Enter the name of cat 3:' )

catName3 = input()

print('Enter the name of cat 4:')

catName4 = input()

print('Enter the name of cat 5:')

catName5 = input()

print('Enter the name of cat 6:' )

catName6 = input()

print('The cat names are: ' )

print(catName1+' '+catName2+' '+catName3+' '+catName4+' '+catName5 + ' ' + catName6)

通过上面这些代码,可以输入6只猫的名字。

catNames=[]  #定义一个空列表

while True:  #一直循环

      print('Enter the name of cat ' + str(len(catNames) + 1) +

      '(Or enter nothing to stop.):' )

      name=input()

      if name == '':  #判断跳出循环

          break

      catNames=catNames+[name]  # list concatenation,连接列表

print('The cat names are:')

for name in catNames:  #用列表长度进行循环,把值赋值给变量name

      print(' '+name,end=' ')  #打印猫的名字,并以空格为结束,如果不加end=' ',就是默认换行结束。

今天有点头晕,所以学的不多,就到这里吧!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容