computer science 101 unit3--list
2018-03-09 21:52
190 查看
1.python----list
list: 数据可以是任意类型.
p = ['y', 'a', 'b', 'b', 'a', '!']
列表中的索引操作:
p[0] --->'y',p[2:4] --->['b','b'] :返回的是一个列表。
构建列表:
<list> -------->[<Expression>,<Expression>,<Expression>,....]:方括号由逗号隔开的任意数量的表达式。
2. 月份天数
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def how_many_days[month]:
return days_in_month[month-1]
print how_many_days(12)
3. Nested Lists
mixed_up = ['apple', 3, 'oranges', 27, [1, 2, ['alpha', 'beta']] ]
beatles = [['John', 1940], ['Paul', 1942], ['George', 1943], ['Ringo', 1940]]
4. 列表可变..(mutation)
p = ['H', 'e', 'l', 'l', 'o']
q = p
p[0] = 'Y'
print q
输出: ['Y', 'e', 'l', 'l', 'o']
说明q,p指向同一个object.
别名使用(alias).
5. list operations
(1). <list>.append(<element>)
stooges = ['Moe', 'Larry', 'Curly']
stooges.append('Shemp')
(2) <list> + <list>
[0,1] + [2,3] = [0, 1, 2, 3]
(3) len(<list>)
len([0,1]) --------->2
len(['a', ['b',['c']]]) ------------->2
len("Udacity") ------------------->7
6.Loops on Lists.
(1). while <test>:
<Block>
(2). for循环
for <name> in <list>:
<block>
列表求和函数:
def sum_list(p):
sum = 0
for e in p:
sum = sum + e
return sum
list: 数据可以是任意类型.
p = ['y', 'a', 'b', 'b', 'a', '!']
列表中的索引操作:
p[0] --->'y',p[2:4] --->['b','b'] :返回的是一个列表。
构建列表:
<list> -------->[<Expression>,<Expression>,<Expression>,....]:方括号由逗号隔开的任意数量的表达式。
2. 月份天数
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def how_many_days[month]:
return days_in_month[month-1]
print how_many_days(12)
3. Nested Lists
mixed_up = ['apple', 3, 'oranges', 27, [1, 2, ['alpha', 'beta']] ]
beatles = [['John', 1940], ['Paul', 1942], ['George', 1943], ['Ringo', 1940]]
4. 列表可变..(mutation)
p = ['H', 'e', 'l', 'l', 'o']
q = p
p[0] = 'Y'
print q
输出: ['Y', 'e', 'l', 'l', 'o']
说明q,p指向同一个object.
别名使用(alias).
5. list operations
(1). <list>.append(<element>)
stooges = ['Moe', 'Larry', 'Curly']
stooges.append('Shemp')
(2) <list> + <list>
[0,1] + [2,3] = [0, 1, 2, 3]
(3) len(<list>)
len([0,1]) --------->2
len(['a', ['b',['c']]]) ------------->2
len("Udacity") ------------------->7
6.Loops on Lists.
(1). while <test>:
<Block>
(2). for循环
for <name> in <list>:
<block>
列表求和函数:
def sum_list(p):
sum = 0
for e in p:
sum = sum + e
return sum
相关文章推荐
- computer science 101-unit1-python2字符串小结
- computer science 101- unit2-exercise2
- BOOK LIST for Computer Science
- computer science 101-unit2 some exercise
- MIT Computer Science And Artificial Intelligence Laboratory
- Any problem in computer science can be solved with another layer of indirection
- Computer vision scientist David Mumford wins National Medal of Science
- computer science and technology
- Computer Science III Midterm 1 复习Topic
- Computer and Computer Science
- Top 50 Computer Science Blogs
- List the Codec Files on a Computer
- 计算机方向的一些顶级会议和期刊—Top Conferences and Journals in Computer Science
- Computer Science Conference Rankings
- (ZZ) Computer Science Conference Rankings
- Computer Science Theory for the Information Age-1: 高维空间中的球体
- Computer Science Conference Ranking
- 《101 Windows Phone 7 Apps》读书笔记-TODO LIST
- Computer Science 学习第四章--CPU 指令集和指令处理
- Computer Science Theory for the Information Age-6: 学习理论——VC定理的证明