注:python中的变量不需要声明定义函数类型,只需要直接写出变量名并赋值。例:age = 23 (整型变量)、message = ‘Hellow world!’(字符串)
注: a. python的列表第一个元素的索引为0,而不是1。 b. python中可以从后方访问元素,如最后一个元素则索引指定为-1,倒数第二个元素索引表示为-2。
输出结果为: [‘apple’,‘mango’,‘pear’,‘cherry’] banana
列表名.remove(指定元素):可以在不清楚元素位置但知道元素内容的情况下,直接删除该元素。列表名.sort( ):对列表中的元素按ascii从小到大永久性排序(对于中文元素,则是按照unicode排序) 列表名.sort(recerse = True):与上述排序方式相反。 sorted(函数名):暂时性传递列表元素,原列表顺序并没有受影响。列表名.reverse( ):倒序列表中元素。len(列表名):查看列表长度。range(): 例: #函数格式:range(最小值(可以取),最大值(取不到),递增值) numbers = list(range(2,20,3)) print(numbers)运行结果: [2,5,8,11,14,17]
例:
#建立列表 names = ['alice','eric','david'] #for循环遍历列表 for name in names: print("Hello everyone! My name is" + name.title() + ".\n") print("END!")运行结果: Hello everyone! My name is Alice.
Hello everyone! My name is Eric
Hello everyone! My name is David.
END!
注:
for循环后连续的有缩进的行都会进行循环,直到其后第一个没有缩进的行开始不进入循环。python中不必要的缩进则会报错,应该缩进的没有缩进也会报错。创立切片则要指定需要的元素的第一个元素的索引和最后一个元素的索引加一。如果没有第一个索引则自动从第一个元素开始,没有最后一个索引则终止与列表末尾。 例1:
#定义列表 fruits = ['apple','banana','mango','pear','cherry'] #打印第2个元素到第4个元素 print(fruits[1:4]) #打印第1个元素到第2个元素 print(fruits[:2]) #打印第3个元素到最后一个元素 print(fruits[2:]) #打印最后三个元素 print(fruits[-3:])注:应注意:列表名1 = 列表名2 与 列表名1 = 列表名2[ : ]的区别: 例2(1):
my_sports = ['volleyball','basketball','badminton'] frind_sports = my_sports[:] frind_sports.append('tennis') my_sports.append('swim') print(my_sports) print(frind_sports)输出结果: [‘volleyball’,‘basketball’,‘badminton’,‘swim’] [‘volleyball’,‘basketball’,‘badminton’,‘tennis’]
my_sports = ['volleyball','basketball','badminton'] frind_sports = my_sports frind_sports.append('tennis') my_sports.append('swim') print(my_sports) print(frind_sports)输出结果: [‘volleyball’,‘basketball’,‘badminton’,‘tennis’,‘swim’] [‘volleyball’,‘basketball’,‘badminton’,‘tennis’,‘swim’]
可看出:当利用切片来给列表相互赋值时买两个列表是相互独立的两个列表。而利用列表名直接进行赋值,实际上得到的还是原来的那个列表,只不过两个列表名都可以表示这个列表,相当于又建立了一个列表名关联到原列表。
元组看起来与列表相似,但使用圆括号而不是方括号。 元组元素可以通过索引来进行访问,但不能利用索引来进行修改元素内容,如果想对元组进行修改,则需要对列表进行重新赋值。 例:
fruits = ('apple','banana','mango','pear','cherry') #错误修改 fruits[0] = 'durian' #正确修改 fruits = ('durian','banana','mango','pear','cherry')元组元素也可以像列表一样直接运用for循环进行遍历
例1、if常规用法
a = input("请猜测我的年龄:") a = int(a) #if语句作为接下来语句是否执行的条件 if a == 19 : print("猜对了!") elif a > 19 print("您猜测的过大!") else print("您猜的过小!")例2、if语句在列表中的使用
fruits = ['apple','banana','mango','pear','cherry'] fruit = input() if fruit in fruits: print("该水果本水果店有售!") if fruit not in fruits: print("该水果本水果店不售或售罄!")字典由一系列键-值对组成,每一个键都与一个值相关联。(与键相关的值可以是数字、字符串、列表以及另一个字典) 键-值对是两个相关联的值。当指定键时,会返回与之相对应的值。键与值之间以冒号隔开。 例:
fruits = {'apple':'red','banana':10,'cherry':15} print(fruit['apple'])输出结果 red
输出结果 {‘apple’:‘red’,‘banana’:‘yellow’,‘cherry’:15}
修改字典中的值 fruits = {'apple':'red','banana':10,'cherry':15} fruits['apple'] = 'green' fruits['cherry'] = fruits['cherry'] + 5 删除键-值对 fruits = {'apple':'red','banana':10,'cherry':15} del fruits['apple'] print(fruits)输出结果: {‘banana’:10,‘cherry’:15}
遍历字典中所有键 fruits = {'apple':'red','banana':10,'cherry':15} #在不需要使用字典中的值时,可以使用方法keys() for fruit in fruits.key(): print(fruit.title())输出结果: Apple Banana Cherry
遍历字典中的值 fruits = {'apple':'red','banana':10,'cherry':15} #在不需要使用字典中的键时,可以使用方法keys() for fruit in fruits.values(): print(fruit.title()) 遍历字典中的键-值对 fruits = {'apple':'red','banana':10,'cherry':15} for key,value in fruits.items( ) : print("\n Key:" +key) print("Value:" + value)输出结果: Key:apple Value:red
Key:banana Value:10
Key:cherry Value:15
列表中嵌套字典 byd = {'prize' : '15w','clore' : 'red'} bmw = {'prize' : '40w','clore' : 'white'} audi = {'prize' : '35w','clore' : 'black'} cars = [byd,bmw,audi] for car in cars: print(car)输出结果: {‘prize’ : ‘15w’,‘clore’ : ‘red’} {‘prize’ : ‘40w’,‘clore’ : ‘white’} {‘prize’ : ‘35w’,‘clore’ : ‘black’}
字典中嵌套列表 favourite_food = { 'Eric' : ['pizza' , 'hamburger'], 'Alice' : ['noodle' , 'potato'], 'Ton' : ['steak'], } for name,foods in favourite_food.items(): print("\n" + name + "'s favourite food are:") for food in foods : print("\t" + food.title())输出结果: Eric’s favourite food are: Pizza Hamburger
Alice’s favourite food are: Noodle Potato
Ton’s favourite food are: Steak
在字典中嵌套字典 cars ={ 'bmw' : { 'prize' : '40w', 'clore' : 'red', }, 'audi' : { 'prize' : '25w', 'clore' : 'black', }, } for car,attributes in cars.items(): print('Car:' + car) print("\t价格:" + attributes['prize'] + '\t颜色:' + attributes['clore'])输出结果: Car:bmw 价格:40w 颜色:red Car:audi 价格:25w 颜色:black