这节课讲了一部分列表的操作,先给源代码
#-*-coding:utf-8-*-
ten_things = "Apples Oranges Crows Telephone Light Suger" # Crow 乌鸦
print "Wait there's not 10 things in that list, let's fix that."
stuff = ten_things.split(' ')
more_stuff = ["Day","Night","Song","Frisbee","Corn","Banana","Girl","Boy"] # Frisbee 飞盘
while len(stuff) != 10:
next_one = more_stuff.pop()
print "Adding: ", next_one
stuff.append(next_one)
print "There's %d items now." % len(stuff)
print "There we go: ", stuff
print "Let's do some things with stuff."
print stuff[1]
print stuff[-1] # whoa! fancy
print stuff.pop()
print ' '.join(stuff) # what? cool
print '#'.join(stuff[3:5]) # super stellar!
这个代码的运行结果是这样的:
===================================================================================================
附加练习
1-3.
#-*-coding:utf-8-*-
ten_things = "Apples Oranges Crows Telephone Light Suger" # Crow 乌鸦
print "Wait there's not 10 things in that list, let's fix that."
# 这时候列表 ten_things 里面其实没有十个元素
stuff = ten_things.split(' ')
# 创建一个叫 stuff 的列表,从 ten_things 里面用 split() 切割,以空格作为切割标志
# split(ten_things, ' ')
# 为 ten_things 和 ' ' 调用 split 函数
# Python split()通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 个子字符串
more_stuff = ["Day","Night","Song","Frisbee","Corn","Banana","Girl","Boy"] # Frisbee 飞盘
# 给一个叫 more_stuff 的列表,里面有很多另外的东西
while len(stuff) != 10:
# 如果 stuff 的长度不为10
next_one = more_stuff.pop()
# 创建变量 next_one ,从 more_stuff 里面进一个
# 为 more_stuff 调用 pop 函数
# Python list.pop() 方法: 从列表中移除并返回最后一个对象或者obj
# 意思是在这里是从最后的 Boy 开始返回的
print "Adding: ", next_one
stuff.append(next_one)
# append(stuff, nextone)
# 为 stuff 和 next_one 调用 append 函数
print "There's %d items now." % len(stuff)
print "There we go: ", stuff
print "Let's do some things with stuff."
print stuff[1]
# 输出 Oranges,这是 stuff 列表的第二个元素
print stuff[-1] # whoa! fancy
# 输出 Corn,这是 stuff 列表的最后一个元素
print stuff.pop()
# 输出 Corn,并且移除 Corn
print ' '.join(stuff) # what? cool
# join(' ', stuff)
# 为 stuff 和 ' ' 调用 join 函数
# Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串
# 这里就是用空格把新 stuff 里面的元素隔开然后生成一个字符串
print '#'.join(stuff[3:5]) # super stellar!
# join(' ', stuff[3:5])
# 把 # 字符用来隔开第4个和第5个元素
4.
Python 的类 ( Class ) :
类有这样一些的优点
1) 、类对象是多态的:也就是多种形态,这意味着我们可以对不同的类对象使用同样的操作方法,而不需要额外写代码。
2)、类的封装:封装之后,可以直接调用类的对象,来操作内部的一些类方法,不需要让使用者看到代码工作的细节。
3)、类的继承:类可以从其它类或者元类中继承它们的方法,直接使用。
然而还是不懂,以后也许就懂了。
真的这种东西要实际上手才行,光看是看不明白的