python 列表的创建以及基本操作

《python编程从入门到实践》

第三章 列表简介

  1. 用"[]"来建立列表,例如:letter = [a,b,c];
  2. 用"[]"来提取列表元素,例如letter[0],列表元素从0开始,最后一个元素定义为letter[-1],一次类推-2,-3,....;
  3. 修改元素,采用赋值的方法:letter[0] = D;
  4. 添加元素:末尾添加元素,letter.append('k');列表插入元素,letter.insert(0,m);
  5. 根据元素的位置来删除元素 del letter[0];弹出元素(删除元素可以保存),letter_first = letter.pop(0);
  6. 根据元素的值来删除元素,letter.remove(a)

exercise3_2

name = ['Steve Jobs','Bill Gates','Albert Einstein'] #想要邀请的人的名字
print(name[0]+','+name[1]+','+name[2]+ '!'+' Wellcome you to come to have dinner with me!')
name_pop = name.pop(1);
print('Bill Gates is to busy to come here!')
name.insert(1,'Elon Musk')
print(name[0]+','+name[1]+','+name[2]+ '!'+' Wellcome you to come to have dinner with me!')
print('I find a lager table!')
name.insert(0,'A')
name.insert(2,'B')
name.append('C')
print(name[0]+','+name[1]+','+name[2]+','+name[3]+','+name[4]+','+name[5]+'!'+' Wellcome you to come to have dinner with me!')
print('sorry,everybody, I just can invit two of you')
name1 = name.pop()
print('sorry ' + name1 +". I can't invit you!")
name2 = name.pop()
print('sorry ' + name2 +". I can't invit you!")
name3 = name.pop()
print('sorry ' + name3 +". I can't invit you!")
name4 = name.pop()
print('sorry ' + name4 +". I can't invit you!")
print(name[0] +' and ' + name[1] + "! You are still invited")
del name[1]
del name[0]
print(name)
posted @ 2020-07-15 10:20  无涯987  阅读(932)  评论(0)    收藏  举报