1.python不用声明变量的类型,运行时python自己进行判断
2.尽量不要用“+”去拼接字符串,运行时会每遇到一个“+”就开辟一块内存空间,使用如下方式进行字符串的拼接。
msg='''
information of user %s
-----------------------
name: %s
age : %d
job : %s
-----------end---------
'''%(name,name,age,job) print(msg)
3.列表的语法和练习
python中叫列表,在其它语言中叫数组。
#命名列表
name=["mark","jack","Tang long"]
age=9
#列表中可以放任何类型 ,也可以放变量
name=["mark","jack","Tang long",22,age,56,22,47,88,9,5,6,7]
#列表取值
print("name-----",name)
print("name[1]-----",name[1])
print("name[-1]-----",name[-1])
print("name[0:6]-----",name[0:6])
print("name[:6]-----",name[:6])
print("name[:6][2:4]---",name[:6][2:4])
print("name[:6][2:4][0]---",name[:6][2:4][0])
print("name[:6][2:4][0][1]---",name[:6][2:4][0][1])
print("name[-5:-1]-----",name[-5:-1])
print("name[-5:]-----",name[-5:]) # 修改
name[1]="Wangminghu"
print("name====",name)
#插入,一次只能插一个
name.insert(2,"MingGou")
print("name====",name)
#追加
name.append("Alex")
print("name====",name)
#删除
name.remove("MingGou")
print("name====",name) #运行结果
name----- ['mark', 'jack', 'Tang long', 22, 9, 56, 22, 47, 88, 9, 5, 6, 7]
name[1]----- jack
name[-1]----- 7
name[0:6]----- ['mark', 'jack', 'Tang long', 22, 9, 56]
name[:6]----- ['mark', 'jack', 'Tang long', 22, 9, 56]
name[:6][2:4]--- ['Tang long', 22]
name[:6][2:4][0]--- Tang long
name[:6][2:4][0][1]--- a
name[-5:-1]----- [88, 9, 5, 6]
name[-5:]----- [88, 9, 5, 6, 7]
name==== ['mark', 'Wangminghu', 'Tang long', 22, 9, 56, 22, 47, 88, 9, 5, 6, 7]
name==== ['mark', 'Wangminghu', 'MingGou', 'Tang long', 22, 9, 56, 22, 47, 88, 9, 5, 6, 7]
name==== ['mark', 'Wangminghu', 'MingGou', 'Tang long', 22, 9, 56, 22, 47, 88, 9, 5, 6, 7, 'Alex']
name==== ['mark', 'Wangminghu', 'Tang long', 22, 9, 56, 22, 47, 88, 9, 5, 6, 7, 'Alex']
列表使用
列表练习,打印结果前的数字为代码的行号:
name=["Alex","jack","Rain","Eric","Monica","Fiona"]
print('2 name====',name)
#插入
name.insert(-1,'MingHU')
print('5 name===',name)
name.insert(4,'MingLong')
print('7 name===',name)
#取值
name2=name[2:7]
print('10 name2===',name2)
#删除
name.remove('Fiona')
print('13 name===',name)
#连续删除用python的全局函数del删除内存的数据
del name[4:6]
print('16 name===',name)
#del name 删除这个变量
print('18 name===',name)
name[4]="wangminglong(leader)"
print('20 name===',name)
#每隔一个取一个,最后一个数字为步长
print('22 name===',name[0::2])#或者print('name===',name[::2])
name=["Alex","jack","Rain",9,47,5,6,8,4,"Eric","Monica",9,6,9,4,5,"Fiona"]
name3=['zhangsan','lisi','zhaoliu']
#判断name中是否有9
print(9 in name)
if(9 in name):
num_of_ele=name.count(9)#对9进行计数
position_of_ele = name.index(9)#查找9的位置
name[position_of_ele]=999
print('31 [%s] 9 are/is in name,position [%s]'%(num_of_ele,position_of_ele))
position_of_ele=name.index(9)
print('33 name===',name)
#改掉全部9
for i in range(name.count(9)):
ele_index=name.index(9)
name[ele_index]=999
print('38 name===',name)
#将两个列表合起来用extend
name.extend(name3)
print('41 name===',name)
#反转
name.reverse();
print('44 name===',name)
#排序,python3之后数字和字符串不能一起排序,python2就可以
print('46 name3===',name3)
name3.sort()
print('48 name3===',name3)
#通过下标删除用pop方法
name.pop()#默认删除最后一个
print('51 name===',name)
name.pop(3)
print('53 name===',name)
#复制一个列表
#列表里可以嵌列表,如果列表中嵌了列表,在普通copy时,嵌入的列表只复制内存地址,两个列表中任何一个嵌入的列表修改,另外一个都跟着修改
#如果想完全克隆需import copy 模块用copy.deepcopy(name)
name4=name.copy()
print('58 name===',name)
print('59 name4===',name4) 运行结果:
2 name==== ['Alex', 'jack', 'Rain', 'Eric', 'Monica', 'Fiona']
5 name=== ['Alex', 'jack', 'Rain', 'Eric', 'Monica', 'MingHU', 'Fiona']
7 name=== ['Alex', 'jack', 'Rain', 'Eric', 'MingLong', 'Monica', 'MingHU', 'Fiona']
10 name2=== ['Rain', 'Eric', 'MingLong', 'Monica', 'MingHU']
13 name=== ['Alex', 'jack', 'Rain', 'Eric', 'MingLong', 'Monica', 'MingHU']
16 name=== ['Alex', 'jack', 'Rain', 'Eric', 'MingHU']
18 name=== ['Alex', 'jack', 'Rain', 'Eric', 'MingHU']
20 name=== ['Alex', 'jack', 'Rain', 'Eric', 'wangminglong(leader)']
22 name=== ['Alex', 'Rain', 'wangminglong(leader)']
True
31 [3] 9 are/is in name,position [3]
33 name=== ['Alex', 'jack', 'Rain', 999, 47, 5, 6, 8, 4, 'Eric', 'Monica', 9, 6, 9, 4, 5, 'Fiona']
38 name=== ['Alex', 'jack', 'Rain', 999, 47, 5, 6, 8, 4, 'Eric', 'Monica', 999, 6, 999, 4, 5, 'Fiona']
41 name=== ['Alex', 'jack', 'Rain', 999, 47, 5, 6, 8, 4, 'Eric', 'Monica', 999, 6, 999, 4, 5, 'Fiona', 'zhangsan', 'lisi', 'zhaoliu']
44 name=== ['zhaoliu', 'lisi', 'zhangsan', 'Fiona', 5, 4, 999, 6, 999, 'Monica', 'Eric', 4, 8, 6, 5, 47, 999, 'Rain', 'jack', 'Alex']
46 name3=== ['zhangsan', 'lisi', 'zhaoliu']
48 name3=== ['lisi', 'zhangsan', 'zhaoliu']
51 name=== ['zhaoliu', 'lisi', 'zhangsan', 'Fiona', 5, 4, 999, 6, 999, 'Monica', 'Eric', 4, 8, 6, 5, 47, 999, 'Rain', 'jack']
53 name=== ['zhaoliu', 'lisi', 'zhangsan', 5, 4, 999, 6, 999, 'Monica', 'Eric', 4, 8, 6, 5, 47, 999, 'Rain', 'jack']
58 name=== ['zhaoliu', 'lisi', 'zhangsan', 5, 4, 999, 6, 999, 'Monica', 'Eric', 4, 8, 6, 5, 47, 999, 'Rain', 'jack']
59 name4=== ['zhaoliu', 'lisi', 'zhangsan', 5, 4, 999, 6, 999, 'Monica', 'Eric', 4, 8, 6, 5, 47, 999, 'Rain', 'jack']
列表练习
4.查看长度用len()函数,适用于字符串,列表等等