3.1 列表定义、输出及元素输出:

由系列按照特定顺序排列的元素组成。元素之间可以没有任何关系。方括号([])来表示列表,元素以逗号(,)分割。

#output the table and the element
bicycles = ['trek','cannondale','redline','specialized']  
#打印出列表
print(bicycles)
#索引由0开始,访问列表元素
print(bicycles[0])
print(bicycles[1])
#访问列表元素,并添加格式
#元素字母均大写
print(bicycles[1].upper())
#元素字母均小写
print(bicycles[1].lower())
#元素首字母大写
print(bicycles[1].title())

#output the second/third/last/penultimate
print(bicycles[1].title(),bicycles[2].title(),bicycles[-1].title(),bicycles[-2].title())   

#element
message = "my first bicycle was a "+ bicycles[0].title() +"."		
print(message)

output:

['trek', 'cannondale', 'redline', 'specialized']
trek
cannondale
CANNONDALE
cannondale
Cannondale
Cannondale Redline Specialized Redline
my first bicycle was a Trek.

Quiz:

3_1/2/3

#!/usr/bin/python
# -*- coding: latin-1 -*-
import os, sys
President=['Washington','Adams','Jefferson','Madison','Bush','Obama']
#打印列表
print(President)
#打印前四个列表元素,元素能否换行显示?"\n"行不通。四个元素在一行
print(President[0],President[1],President[2],President[3])
#打印第五个列表元素
print(President[4])
#打印第六个列表元素
print(President[5])
#问候每个人,抬头为名字
#定义问候变量greet
greet="thank you for your present"
message_1="President "+President[0]+","+greet
message_2="President "+President[1]+","+greet
print(message_1)
print(message_2)

#出行方式
method=['Train','Aircraft','Bus']
print(method[0].title()+" is my favorite way, for the reason that I can enjoy the beautiful scence on the way!")
print(method[1].title()+" can save me lots of time and I can apppreciate all kinds of attractive clouds ")

output:

['Washington', 'Adams', 'Jefferson', 'Madison', 'Bush', 'Obama']
Washington Adams Jefferson Madison
Bush
Obama
President Washington,thank you for your present
President Adams,thank you for your present
Train is my favorite way, for the reason that I can enjoy the beautiful scence on the way!
Aircraft can save me lots of time and I can apppreciate all kinds of attractive clouds

 

Surprise:  no encoding declared

SyntaxError: Non-UTF-8 code starting with '\xb4' in file 3.1_quiz_3_1.py on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Problems:

In Python 2.1, Unicode literals can only be written using the Latin-1 based encoding "unicode-escape". This makes the programming environment rather unfriendly to Python users who live and work in non-Latin-1 locales such as many of the Asian countries. Programmers can write their 8-bit strings using the favorite encoding, but are bound to the "unicode-escape" encoding for Unicode literals.

Solution:  

https://www.python.org/dev/peps/pep-0263/

 

首行添加:

#!/usr/bin/python
# -*- coding: latin-1 -*-
import os, sys

 

3.2 修改、添加、删除元素

#!/usr/bin/python
#-*- coding:latin-1 -*-
#import os, sys


#修改列表元素
#定义列表
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
#索引为0元素替换
motorcycles[0] = 'ducati'
print(motorcycles)

Output:
['honda', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki']


#列表添加元素
#列表末尾添加
motorcycles = ['honda','yamaha','suzuki']
motorcycles.append('ducai')
print(motorcycles)
#空列表逐一添加元素
new_motorcycles = []
new_motorcycles.append('honda')
new_motorcycles.append('yamaha')
new_motorcycles.append('suzuki')
new_motorcycles.append('ducai')
new_motorcycles.append('dayang')
print(new_motorcycles)
#列表中插入元素
motorcycles = ['honda','yamaha','suzuki']
motorcycles.insert(0,'ducati')
motorcycles.insert(3,'Forth Band ')
print(motorcycles)

Output:
['honda', 'yamaha', 'suzuki', 'ducai']
['honda', 'yamaha', 'suzuki', 'ducai', 'dayang']
['ducati', 'honda', 'yamaha', 'Forth Band ', 'suzuki']


#列表删除元素
#del索引为0元素
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
#del索引为1元素
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
del motorcycles[1]
print(motorcycles)
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
del motorcycles[-1]
print(motorcycles)
del motorcycles[-2]
print(motorcycles)
#IndexError: list assignment index out of range
#del motorcycles[-2]
#print (motorcycles)

Output:
['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']
['honda', 'yamaha', 'suzuki']
['honda', 'suzuki']
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha']
['yamaha']


#方法pop()可删除列表末尾的元素,并可继续使用它。术语
#pop源自类比:列表就像一个栈,而删除列表末尾的元素相当于弹出栈顶元素
#从motorcycles中弹出一款摩托车
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
popped_motorcycles = motorcycles.pop()
print(motorcycles)
print(popped_motorcycles)
#假设列表中摩托车是按照购买时间存储,则可得到最后购买的是哪款摩托车
motorcycles = ['honda','yamaha','suzuki']
Last_purchased = motorcycles.pop()
message = "The last motorcycles I owned was a " 
print(message+Last_purchased.title()+".")
#弹出列表中任何位置处的元素
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
first_owned = motorcycles.pop(0)
print(motorcycles)
print(first_owned)
print("The first motorcycle I owned was a "+first_owned.title()+".")
#根据值删除元素
motorcycles = ['honda','yamaha','suzuki','ducati']
print(motorcycles)
removed_motorcycles = motorcycles.remove('ducati')
print(removed_motorcycles)
print(motorcycles)

Output:
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha']
suzuki
The last motorcycles I owned was a Suzuki.
['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']
honda
The first motorcycle I owned was a Honda.
['honda', 'yamaha', 'suzuki', 'ducati']
None
['honda', 'yamaha', 'suzuki']



#方法remove()只删除第一个指定值,如果指定值在列表中可能出现多次,就需要使用循环来判断
motorcycles = ['honda','yamaha','suzuki','ducati']
print(motorcycles)
too_expensive = 'ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
print("A "+too_expensive.title()+" is too expensive for me.")

Output:
['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'suzuki']
A Ducati is too expensive for me.

 

Quiz  :

#!/usr/bin/python
#-*- coding: latin-1 -*-
import os , sys
guest = ['Washington','Adams','Jefferson','Madison','Bush','Obama']
message = "On behalf of our school students, to enjoy new year dinner, it's my great honor to invite President "
print(message+guest[0]+".")
print(message+guest[1]+".")
print(message+guest[2]+".")
print(message+guest[3]+".")
print(message+guest[4]+".")
print(message+guest[5]+".")
print(guest)

output:
On behalf of our school students, to enjoy new year dinner, it's my great honor to invite President Washington.
On behalf of our school students, to enjoy new year dinner, it's my great honor to invite President Adams.
On behalf of our school students, to enjoy new year dinner, it's my great honor to invite President Jefferson.
On behalf of our school students, to enjoy new year dinner, it's my great honor to invite President Madison.
On behalf of our school students, to enjoy new year dinner, it's my great honor to invite President Bush.
On behalf of our school students, to enjoy new year dinner, it's my great honor to invite President Obama.
['Washington', 'Adams', 'Jefferson', 'Madison', 'Bush', 'Obama']


#无法赴约,新嘉宾替换,嘉宾邀请
absent_guest = guest[-2]
absent_reason = " doesn't feel well and need have a good rest as the doctor advised. So he will be absent the dinner"
print("President "+absent_guest+absent_reason)
guest[-2] = "Trump"
print(guest)
print(message+guest[0]+".")
print(message+guest[1]+".")
print(message+guest[2]+".")
print(message+guest[3]+".")
print(message+guest[4]+".")
print(message+guest[5]+".")

output:
President Bush doesn't feel well and need have a good rest as the doctor advised. So he will be absent the dinner
['Washington', 'Adams', 'Jefferson', 'Madison', 'Trump', 'Obama']
On behalf of our school students, to enjoy new year dinner, it's my great honor to invite President Washington.
On behalf of our school students, to enjoy new year dinner, it's my great honor to invite President Adams.
On behalf of our school students, to enjoy new year dinner, it's my great honor to invite President Jefferson.
On behalf of our school students, to enjoy new year dinner, it's my great honor to invite President Madison.
On behalf of our school students, to enjoy new year dinner, it's my great honor to invite President Trump.
On behalf of our school students, to enjoy new year dinner, it's my great honor to invite President Obama.


#添加嘉宾
inv_mor_rea = "For we have bought a bigger table right now, we can invite three more people."
print(inv_mor_rea)
#列表索引0位置插入元素
print(guest)
guest.insert(0,'Crick') 
print (guest)
#7元素,中间位置即索引3,插入元素
guest.insert(3,'Watson') 
print (guest)
#八元素,末尾添加元素
guest.append( 'Feng Zhang') 
print (guest)

output:
For we have bought a bigger table right now, we can invite three more people.
['Washington', 'Adams', 'Jefferson', 'Madison', 'Trump', 'Obama']
['Crick', 'Washington', 'Adams', 'Jefferson', 'Madison', 'Trump', 'Obama']
['Crick', 'Washington', 'Adams', 'Watson', 'Jefferson', 'Madison', 'Trump', 'Obama']
['Crick', 'Washington', 'Adams', 'Watson', 'Jefferson', 'Madison', 'Trump', 'Obama', 'Feng Zhang']



#缩减名单
inv_less_rea = "The table we bought for the dinner can't arrive on time. Two of the three guests invited for the dinner will be appropriate. The guests who receive a invitation at the end will be invited. "
print(inv_less_rea)
#pop,根据索引删除元素并将元素值赋予变量
first = guest.pop(1)
apol_message = ", We are so sorry to infrom you the news."
print("President "+first+apol_message+" "+inv_less_rea)
#print("President "+guest.pop(1)+apol_message+" "+inv_less_rea)
second = guest.pop(1)
print("President "+second+apol_message+" "+inv_less_rea)
third = guest.pop(2)
print("President "+third+apol_message+" "+inv_less_rea)
fourth = guest.pop(2)
print("President "+fourth+apol_message+" "+inv_less_rea)
#remove,元素值赋值于变量,print时调该变量 
fifth = 'Trump'
guest.remove(fifth)
print("President "+fifth+apol_message+" "+inv_less_rea)
sixth = 'Obama'
guest.remove(sixth)
print("President "+sixth+apol_message+" "+inv_less_rea)
seventh = 'Feng Zhang'
guest.remove(seventh)
print("President "+seventh+apol_message+" "+inv_less_rea)
print(guest)
#余嘉宾两位,分别输出两条信息告知其在受邀之列
invitation_mes = "You are the very distinguished guest that we want to invite to enjoy the dinner"
print("Professor "+guest[0]+", "+invitation_mes)
print("Professor "+guest[1]+", "+invitation_mes)
#del删除嘉宾,至名单为空
print(guest)
del guest[0]
del guest[0]
print(guest)

output:
The table we bought for the dinner can't arrive on time. Two of the three guests invited for the dinner will be appropriate. The guests who receive a invitation at the end will be invited.
President Washington, We are so sorry to infrom you the news. The table we bought for the dinner can't arrive on time. Two of the three guests invited for the dinner will be appropriate. The guests who receive a invitation at the end will be invited.
President Adams, We are so sorry to infrom you the news. The table we bought for the dinner can't arrive on time. Two of the three guests invited for the dinner will be appropriate. The guests who receive a invitation at the end will be invited.
President Jefferson, We are so sorry to infrom you the news. The table we bought for the dinner can't arrive on time. Two of the three guests invited for the dinner will be appropriate. The guests who receive a invitation at the end will be invited.
President Madison, We are so sorry to infrom you the news. The table we bought for the dinner can't arrive on time. Two of the three guests invited for the dinner will be appropriate. The guests who receive a invitation at the end will be invited.
President Trump, We are so sorry to infrom you the news. The table we bought for the dinner can't arrive on time. Two of the three guests invited for the dinner will be appropriate. The guests who receive a invitation at the end will be invited.
President Obama, We are so sorry to infrom you the news. The table we bought for the dinner can't arrive on time. Two of the three guests invited for the dinner will be appropriate. The guests who receive a invitation at the end will be invited.
President Feng Zhang, We are so sorry to infrom you the news. The table we bought for the dinner can't arrive on time. Two of the three guests invited for the dinner will be appropriate. The guests who receive a invitation at the end will be invited.
['Crick', 'Watson']
Professor Crick, You are the very distinguished guest that we want to invite to enjoy the dinner
Professor Watson, You are the very distinguished guest that we want to invite to enjoy the dinner
['Crick', 'Watson']
[]