python学习day3--python基础

1.python不用声明变量的类型,运行时python自己进行判断

2.尽量不要用“+”去拼接字符串,运行时会每遇到一个“+”就开辟一块内存空间,使用如下方式进行字符串的拼接。

msg='''
information of user %s
-----------------------
name:   %s
age :   %d
job :   %s
-----------end---------
'''%(name,name,age,job)

print(msg)

3.列表的语法和练习

python中叫列表,在其它语言中叫数组。

 1 #命名列表
 2 name=["mark","jack","Tang long"]
 3 age=9
 4 #列表中可以放任何类型 ,也可以放变量
 5 name=["mark","jack","Tang long",22,age,56,22,47,88,9,5,6,7]
 6 #列表取值
 7 print("name-----",name)
 8 print("name[1]-----",name[1])
 9 print("name[-1]-----",name[-1])
10 print("name[0:6]-----",name[0:6])
11 print("name[:6]-----",name[:6])
12 print("name[:6][2:4]---",name[:6][2:4])
13 print("name[:6][2:4][0]---",name[:6][2:4][0])
14 print("name[:6][2:4][0][1]---",name[:6][2:4][0][1])
15 print("name[-5:-1]-----",name[-5:-1])
16 print("name[-5:]-----",name[-5:])
17 
18 # 修改
19 name[1]="Wangminghu"
20 print("name====",name)
21 #插入,一次只能插一个
22 name.insert(2,"MingGou")
23 print("name====",name)
24 #追加
25 name.append("Alex")
26 print("name====",name)
27 #删除
28 name.remove("MingGou")
29 print("name====",name)
30 
31 #运行结果
32 name----- ['mark', 'jack', 'Tang long', 22, 9, 56, 22, 47, 88, 9, 5, 6, 7]
33 name[1]----- jack
34 name[-1]----- 7
35 name[0:6]----- ['mark', 'jack', 'Tang long', 22, 9, 56]
36 name[:6]----- ['mark', 'jack', 'Tang long', 22, 9, 56]
37 name[:6][2:4]--- ['Tang long', 22]
38 name[:6][2:4][0]--- Tang long
39 name[:6][2:4][0][1]--- a
40 name[-5:-1]----- [88, 9, 5, 6]
41 name[-5:]----- [88, 9, 5, 6, 7]
42 name==== ['mark', 'Wangminghu', 'Tang long', 22, 9, 56, 22, 47, 88, 9, 5, 6, 7]
43 name==== ['mark', 'Wangminghu', 'MingGou', 'Tang long', 22, 9, 56, 22, 47, 88, 9, 5, 6, 7]
44 name==== ['mark', 'Wangminghu', 'MingGou', 'Tang long', 22, 9, 56, 22, 47, 88, 9, 5, 6, 7, 'Alex']
45 name==== ['mark', 'Wangminghu', 'Tang long', 22, 9, 56, 22, 47, 88, 9, 5, 6, 7, 'Alex']
列表使用

 列表练习,打印结果前的数字为代码的行号:

 1 name=["Alex","jack","Rain","Eric","Monica","Fiona"]
 2 print('2 name====',name)
 3 #插入
 4 name.insert(-1,'MingHU')
 5 print('5 name===',name)
 6 name.insert(4,'MingLong')
 7 print('7 name===',name)
 8 #取值
 9 name2=name[2:7]
10 print('10 name2===',name2)
11 #删除
12 name.remove('Fiona')
13 print('13 name===',name)
14 #连续删除用python的全局函数del删除内存的数据
15 del name[4:6]
16 print('16 name===',name)
17 #del name 删除这个变量
18 print('18 name===',name)
19 name[4]="wangminglong(leader)"
20 print('20 name===',name)
21 #每隔一个取一个,最后一个数字为步长
22 print('22 name===',name[0::2])#或者print('name===',name[::2])
23 name=["Alex","jack","Rain",9,47,5,6,8,4,"Eric","Monica",9,6,9,4,5,"Fiona"]
24 name3=['zhangsan','lisi','zhaoliu']
25 #判断name中是否有9
26 print(9 in name)
27 if(9 in name):
28     num_of_ele=name.count(9)#对9进行计数
29     position_of_ele = name.index(9)#查找9的位置
30     name[position_of_ele]=999
31     print('31 [%s] 9 are/is in name,position [%s]'%(num_of_ele,position_of_ele))
32     position_of_ele=name.index(9)
33 print('33 name===',name)
34 #改掉全部9
35 for i in range(name.count(9)):
36     ele_index=name.index(9)
37     name[ele_index]=999
38 print('38 name===',name)
39 #将两个列表合起来用extend
40 name.extend(name3)
41 print('41 name===',name)
42 #反转
43 name.reverse();
44 print('44 name===',name)
45 #排序,python3之后数字和字符串不能一起排序,python2就可以
46 print('46 name3===',name3)
47 name3.sort()
48 print('48 name3===',name3)
49 #通过下标删除用pop方法
50 name.pop()#默认删除最后一个
51 print('51 name===',name)
52 name.pop(3)
53 print('53 name===',name)
54 #复制一个列表
55 #列表里可以嵌列表,如果列表中嵌了列表,在普通copy时,嵌入的列表只复制内存地址,两个列表中任何一个嵌入的列表修改,另外一个都跟着修改
56 #如果想完全克隆需import copy 模块用copy.deepcopy(name)
57 name4=name.copy()
58 print('58 name===',name)
59 print('59 name4===',name4)
60 
61 运行结果:
62 2 name==== ['Alex', 'jack', 'Rain', 'Eric', 'Monica', 'Fiona']
63 5 name=== ['Alex', 'jack', 'Rain', 'Eric', 'Monica', 'MingHU', 'Fiona']
64 7 name=== ['Alex', 'jack', 'Rain', 'Eric', 'MingLong', 'Monica', 'MingHU', 'Fiona']
65 10 name2=== ['Rain', 'Eric', 'MingLong', 'Monica', 'MingHU']
66 13 name=== ['Alex', 'jack', 'Rain', 'Eric', 'MingLong', 'Monica', 'MingHU']
67 16 name=== ['Alex', 'jack', 'Rain', 'Eric', 'MingHU']
68 18 name=== ['Alex', 'jack', 'Rain', 'Eric', 'MingHU']
69 20 name=== ['Alex', 'jack', 'Rain', 'Eric', 'wangminglong(leader)']
70 22 name=== ['Alex', 'Rain', 'wangminglong(leader)']
71 True
72 31 [3] 9 are/is in name,position [3]
73 33 name=== ['Alex', 'jack', 'Rain', 999, 47, 5, 6, 8, 4, 'Eric', 'Monica', 9, 6, 9, 4, 5, 'Fiona']
74 38 name=== ['Alex', 'jack', 'Rain', 999, 47, 5, 6, 8, 4, 'Eric', 'Monica', 999, 6, 999, 4, 5, 'Fiona']
75 41 name=== ['Alex', 'jack', 'Rain', 999, 47, 5, 6, 8, 4, 'Eric', 'Monica', 999, 6, 999, 4, 5, 'Fiona', 'zhangsan', 'lisi', 'zhaoliu']
76 44 name=== ['zhaoliu', 'lisi', 'zhangsan', 'Fiona', 5, 4, 999, 6, 999, 'Monica', 'Eric', 4, 8, 6, 5, 47, 999, 'Rain', 'jack', 'Alex']
77 46 name3=== ['zhangsan', 'lisi', 'zhaoliu']
78 48 name3=== ['lisi', 'zhangsan', 'zhaoliu']
79 51 name=== ['zhaoliu', 'lisi', 'zhangsan', 'Fiona', 5, 4, 999, 6, 999, 'Monica', 'Eric', 4, 8, 6, 5, 47, 999, 'Rain', 'jack']
80 53 name=== ['zhaoliu', 'lisi', 'zhangsan', 5, 4, 999, 6, 999, 'Monica', 'Eric', 4, 8, 6, 5, 47, 999, 'Rain', 'jack']
81 58 name=== ['zhaoliu', 'lisi', 'zhangsan', 5, 4, 999, 6, 999, 'Monica', 'Eric', 4, 8, 6, 5, 47, 999, 'Rain', 'jack']
82 59 name4=== ['zhaoliu', 'lisi', 'zhangsan', 5, 4, 999, 6, 999, 'Monica', 'Eric', 4, 8, 6, 5, 47, 999, 'Rain', 'jack']
列表练习

 4.查看长度用len()函数,适用于字符串,列表等等

posted @ 2016-08-30 15:01  爆烈鼓手  阅读(605)  评论(0编辑  收藏  举报