首页 > 其他 > 详细

第四章 操作列表

时间:2017-03-05 14:29:58      阅读:107      评论:0      收藏:0      [点我收藏+]
#1.
#A:python根据缩进来判断代码行与上一句代码行的关系,所以在python中不能胡乱缩进
#B:胡乱使用缩进会导致编译不能通过

#2.
#A:对于for循环语句后面属于for循环内容的代码必须使用一致的缩进,for语句末尾的冒号告诉python下一行时循环开始行
Test = [1, 3, 2]
for value in Test:
    print(value, end = ‘‘)      #132
print(‘‘)
print(Test)                     #[1, 3, 2]

#3.
#A:range()从第一个指定值开始,到达第二个指定值后结束(即生成的值不包含第二个指定值)
#B:list()可以将range()的结果直接转换为列表
Test = list(range(1, 5))
print(Test)                     #[1, 2, 3, 4]
Test = list(range(1, 5, 2))
print(Test)                     #[1, 3]

#4.
#A:min(), max(),sum()
Test = [1, 5, 6];
print(min(Test))                #1
print(max(Test))                #6
print(sum(Test))                #12

#5.
#A:列表解析将for循环和创建新元素合并,并自动附加新元素
Test = [value ** 2 for value in range(1, 3)]
print(Test)                     #[1, 4]

#6.
#A:列表切片可指定使用的第一个元素和最后一个元素的索引,在到达第二个元素之前结束
Test = [1, 5, 6, 7]
TestCopy = Test[0:2]
print(TestCopy)                 #[1, 5]
TestCopy = Test[:]
print(TestCopy)                 #[1, 5, 6, 7] 
TestCopy = Test[1:]
print(TestCopy)                 #[5, 6, 7]
TestCopy = Test[-3:]
print(TestCopy)                 #[5, 6, 7]
TestCopy = Test[:-3]
print(TestCopy)                 #[1]
for value in Test[0:2]:
    print(value, end = ‘ ‘)     #1 5
print("")

#7.
#A:要复制列表时,可以创建一个包含整个列表的切片
Test = [1, 2, 3]
TestCopy1 = Test                #TestCopy1其实和Test指向同一个列表
TestCopy2 = Test[:]
Test[0] = ‘s‘
print(TestCopy1)                #[‘s‘, 2, 3]
print(TestCopy2)                #[1, 2, 3]

#8.
#A:元组使用圆括号,其元素不可更改,可以通过索引来访问元组的元素
Test = (1, 2 ,3)
print(Test)                     #(1, 2, 3)
Test = (4, 5, 6)                #元组中的元素不可被更改,但是可以给存储元组的变量重新赋值
print(Test)                     #(4, 5, 6)
#Test[0] = 1                    #运行会出错

#9.
#A:在编写python代码时候,一定要用制表符来控制缩进,但要对编译器进行设置,使其插入代码的是空格而不是制表符,这一点vs2010支持

  

第四章 操作列表

原文:http://www.cnblogs.com/szn409/p/6505176.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!