列表简介
1num=['a','b','c','d'] 2print(num) 3--------------------------------------------------------------- 4['a','b','c','d'] #打印结果 5---------------------------------------------------------------
如果是遍历的话是:
1for i in num: 2 print(i) 3--------------------------------------------------------------- 4a 5b 6c 7d #打印结果 8---------------------------------------------------------------
列表访问元素
注意:列表的索引是从0开始的,不是1
1print(num[0].title()) 2--------------------------------------------------------------- 3A #打印结果 4--------------------------------------------------------------- 5print(num[-1].title()) #注意Python逆向是从-1开始的 6--------------------------------------------------------------- 7D #打印结果 8---------------------------------------------------------------
使用列表中的各个值
1bicycles = ['trek', 'cannondale', 'redline', 'specialized'] 2message = "My first bicycle was a " + bicycles[0].title() + "." 3print(message) 4--------------------------------------------------------------- 5My first bicycle was a Trek. #打印结果 6---------------------------------------------------------------
修改、添加和删除列表元素
修改列表元素:
1motorcycles = ['honda', 'yamaha', 'suzuki'] 2print(motorcycles) 3motorcycles[0] = 'ducati' #修改motorcycles索引为0的元素为'ducati' 4print(motorcycles) 5--------------------------------------------------------------- 6['honda', 'yamaha', 'suzuki'] 7['ducati', 'yamaha', 'suzuki'] #打印结果 8---------------------------------------------------------------
在列表中添加元素
1motorcycles = ['honda', 'yamaha', 'suzuki'] 2print(motorcycles) 3motorcycles.append('ducati') #append()是内置函数,意思是在列表的最后添加一个元素 4print(motorcycles) 5--------------------------------------------------------------- 6['honda', 'yamaha', 'suzuki'] 7['honda', 'yamaha', 'suzuki', 'ducati'] #打印结果 8---------------------------------------------------------------
也可以用append()添加元素
1motorcycles = [] 2motorcycles.append('honda') 3motorcycles.append('yamaha') 4motorcycles.append('suzuki') 5print(motorcycles) 6--------------------------------------------------------------- 7['honda', 'yamaha', 'suzuki'] #打印结果 8---------------------------------------------------------------
在列表中插入元素
1motorcycles = ['honda', 'yamaha', 'suzuki'] 2motorcycles.insert(0, 'ducati') #insert()函数的意思是添加元素,其索引为0 3print(motorcycles)
从列表中删除元素
1motorcycles = ['honda', 'yamaha', 'suzuki'] 2print(motorcycles) 3del motorcycles[0] #删除索引为0的元素 4print(motorcycles) 5--------------------------------------------------------------- 6['honda', 'yamaha', 'suzuki'] 7['honda', 'suzuki'] #打印结果 8---------------------------------------------------------------
使用方法pop()删除元素
1motorcycles = ['honda', 'yamaha', 'suzuki'] 2print(motorcycles) 3popped_motorcycle = motorcycles.pop() #将列表最后一个元素弹出,然后赋值给新的变量,这也叫列表元素 4print(motorcycles) 5print(popped_motorcycle) 6--------------------------------------------------------------- 7['honda', 'yamaha', 'suzuki'] 8['honda', 'yamaha'] 9suzuki #打印结果 10---------------------------------------------------------------
弹出列表中任何位置处的元素
1motorcycles = ['honda', 'yamaha', 'suzuki'] 2first_owned = motorcycles.pop(0) 3print('The first motorcycle I owned was a ' + first_owned.title() + '.') 4--------------------------------------------------------------- 5The first motorcycle I owned was a Honda. #打印结果 6---------------------------------------------------------------
根据值删除元素
1motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] 2print(motorcycles) 3motorcycles.remove('ducati') 4print(motorcycles) 5--------------------------------------------------------------- 6['honda', 'yamaha', 'suzuki', 'ducati'] 7['honda', 'yamaha', 'suzuki'] #打印结果 8---------------------------------------------------------------
使用方法 sort()对列表进行永久性排序
1cars = ['bmw', 'audi', 'toyota', 'subaru'] 2cars.sort() #reverse=True 逆序打印列表 3print(cars) 4--------------------------------------------------------------- 5['audi', 'bmw', 'subaru', 'toyota'] #打印结果 6---------------------------------------------------------------
使用函数 sorted()对列表进行临时排序
1cars = ['bmw', 'audi', 'toyota', 'subaru'] 2print("Here is the original list:") 3print(cars) 4print("\nHere is the sorted list:") 5print(sorted(cars)) # reverse=True 6print("\nHere is the original list again:") 7print(cars) 8--------------------------------------------------------------- 9Here is the original list: 10['bmw', 'audi', 'toyota', 'subaru'] 11Here is the sorted list: 12['audi', 'bmw', 'subaru', 'toyota'] 13Here is the original list again: 14['bmw', 'audi', 'toyota', 'subaru'] #打印结果 15---------------------------------------------------------------
倒着打印列表
1cars = ['bmw', 'audi', 'toyota', 'subaru'] 2print(cars) 3cars.reverse() 4print(cars) 5--------------------------------------------------------------- 6['bmw', 'audi', 'toyota', 'subaru'] 7['subaru', 'toyota', 'audi', 'bmw'] #打印结果 8---------------------------------------------------------------
确定列表的长度
1cars = ['bmw', 'audi', 'toyota', 'subaru'] 2>>> print(len(cars)) 34
操作列表 注意:Python对缩进要求很严,缩进不同程序输出的内容也不一样
1magicians = ['alice', 'david', 'carolina'] 2for magician in magicians: 3 print(magician) 4--------------------------------------------------------------- 5alice 6david 7carolina #打印结果 8---------------------------------------------------------------
在for循环中执行更多的操作
1magicians = ['alice', 'david', 'carolina'] 2for magician in magicians: 3 print(magician.title() + ", that was a great trick!") 4#Python对缩进有强烈的要求 5--------------------------------------------------------------- 6Alice, that was a great trick! 7David, that was a great trick! 8Carolina, that was a great trick! #打印结果 9---------------------------------------------------------------
#避免缩进错误 (重点)
Python根据缩进来判断代码行与前一个代码行的关系。在前面的示例中,向各位魔术师显示消息的代码行是for循环的一部分,因为它们缩进了。Python通过使用缩进让代码更易读;简单地说,它要求你使用缩进让代码整洁而结构清晰。在较长的Python程序中,你将看到缩进程度各不相同的代码块,这让你对程序的组织结构有大致的认识。 当你开始编写必须正确缩进的代码时,需要注意一些常见的缩进错误。例如,有时候,程序员会将不需要缩进的代码块缩进,而对于必须缩进的代码块却忘了缩进。通过查看这样的错误示例,有助于你以后避开它们,以及在它们出现在程序中时进行修复。
不必要的缩进
message = "Hello Python world!" print(message)
#循环后不必要的缩进 ...... #遗漏了冒号 ......
创建数值列表
使用函数 range()
1#1到5,但是不包括5, 2for value in range(1,5): 3 print(value) 4--------------------------------------------------------------- 51 62 73 84 #打印结果 9--------------------------------------------------------------- 10 11#使用 range()创建数字列表 12#list() 是列表函数 13numbers = list(range(1,6)) 14print(numbers) 15--------------------------------------------------------------- 16[1, 2, 3, 4, 5] #打印结果 17---------------------------------------------------------------
range函数参数介绍 range(start,end,num)
start:起始位置 end:结束位置,但是不包括该数字 num:步长
1even_numbers = list(range(2,11,2)) 2print(even_numbers) 3--------------------------------------------------------------- 4[2, 4, 6, 8, 10] #打印结果 5--------------------------------------------------------------- 6squares = [] 7for value in range(1,11): 8square = value**2 9squares.append(square) 10print(squares) 11--------------------------------------------------------------- 12[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] #打印结果 13--------------------------------------------------------------- 14 15 16#对数字列表执行简单的统计计算 17digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] 18>>> min(digits) 19>>> min(digits) 200 21>>> max(digits) 229 23>>> sum(digits) 2445
列表解析
1squares = [value**2 for value in range(1,11)] 2print(squares) 3--------------------------------------------------------------- 4[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] #打印结果 5---------------------------------------------------------------
列表切片
1players = ['charles', 'martina', 'michael', 'florence', 'eli'] 2# [0:3]:0到3,但是不包含3 3print(players[0:3]) 4--------------------------------------------------------------- 5['charles', 'martina', 'michael'] #打印结果 6--------------------------------------------------------------- 7players = ['charles', 'martina', 'michael', 'florence', 'eli'] 8# [1:4]:1到4,但是不包含4 9print(players[1:4]) 10--------------------------------------------------------------- 11['martina', 'michael', 'florence'] #打印结果 12--------------------------------------------------------------- 13players = ['charles', 'martina', 'michael', 'florence', 'eli'] 14#如果第一个没有写,那么默认是从0开始 15# [:4] 0到4 16print(players[:4]) 17--------------------------------------------------------------- 18['charles', 'martina', 'michael', 'florence'] #打印结果 19--------------------------------------------------------------- 20players = ['charles', 'martina', 'michael', 'florence', 'eli'] 21# [2:]:从2开始一直到后面所有元素 22print(players[2:]) 23--------------------------------------------------------------- 24['michael', 'florence', 'eli'] #打印结果 25--------------------------------------------------------------- 26players = ['charles', 'martina', 'michael', 'florence', 'eli'] 27#如果从后面开始数的话,是-1,那么找到-3然后一直到最后的元素 28print(players[-3:]) 29--------------------------------------------------------------- 30['michael', 'florence', 'eli'] #打印结果 31---------------------------------------------------------------
遍历切片
1players = ['charles', 'martina', 'michael', 'florence', 'eli'] 2print("Here are the first three players on my team:") 3#从0到3,不包含3 4for player in players[:3]: 5 print(player.title()) 6 7#复制列表 8my_foods = ['pizza', 'falafel', 'carrot cake'] 9friend_foods = my_foods[:] 10 11friend_foods = my_foods #这个是错误的
元组
定义元组:元组看起来犹如列表,但使用圆括号而不是方括号来标识。定义元组后,就可以使#用索引来 访问其元素,就像访问列表元素一样。
1dimensions = (200, 50) 2print(dimensions[0]) 3print(dimensions[1]) 4--------------------------------------------------------------- 5200 650 #打印结果 7--------------------------------------------------------------- 8#这里注意元组的数据是不能被改变的 9 10#遍历元组中的所有值 11dimensions = (200, 50) 12for dimension in dimensions: 13 print(dimension) 14--------------------------------------------------------------- 15200 1650 #打印结果 17---------------------------------------------------------------
修改元组变量
虽然不能修改元组的元素,但可以给存储元组的变量赋值。因此,如果要修改前述矩形的尺
1dimensions = (200, 50) 2print("Original dimensions:") 3for dimension in dimensions: 4 print(dimension) 5 6dimensions = (400, 100) 7print("\nModified dimensions:") 8for dimension in dimensions: 9 print(dimension) 10--------------------------------------------------------------- 11Original dimensions: 12200 1350 14 15Modified dimensions: 16400 17100 #打印结果 18---------------------------------------------------------------
#设置代码格式 ......
#格式设置指南 ......
#缩进 ......
行长 以下助于了解,不用掌握
很多Python程序员都建议每行不超过80字符。最初制定这样的指南时,在大多数计算机中,终端窗口每行只能容纳79字符;当前,计算机屏幕每行可容纳的字符数多得多,为何还要使用79字符的标准行长呢?这里有别的原因。专业程序员通常会在同一个屏幕上打开多个文件,使用标准行长可以让他们在屏幕上并排打开两三个文件时能同时看到各个文件的完整行。 PEP 8还建议注释的行长都不超过72字符,因为有些工具为大型项目自动生成文档时,会在每行注释开头添加格式化字符。 PEP 8中有关行长的指南并非不可逾越的红线,有些小组将最大行长设置为99字符。在学习 期间,你不用过多地考虑代码的行长,但别忘了,协作编写程序时,大家几乎都遵守PEP 8指南。在大多数编辑器中,都可设置一个视觉标志——通常是一条竖线,让你知道不能越过的界线在什么地方。
空行
要将程序的不同部分分开,可使用空行。你应该使用空行来组织程序文件,但也不能滥用;只要按本书的示例展示的那样做,就能掌握其中的平衡。例如,如果你有5行创建列表的代码,还有3行处理该列表的代码,那么用一个空行将这两部分隔开是合适的。然而,你不应使用三四个空行将它们隔开。空行不会影响代码的运行,但会影响代码的可读性。Python解释器根据水平缩进情况来解读代码,但不关心垂直间距。
《Python学习笔记-Day2》 是转载文章,点击查看原文。
