1、Python网址:www.python.org
2、print‘ hello Python’=print‘hello’,‘Python’
3、布尔值 and,or,not
4、字符串:‘ ’,“ ”都行。print ‘100+200=’,100+200
5、Python注释是#
6、如果一个字符串包含很多需要转义的字符,对每个字符都要转义很麻烦,为此,在字符串前+r
7、如果遇到UnicodeDecodeError,是因为py文件保存格式有问题,可在第一行添加注释:
#_*_coding:utf-8_*_
8、11/4=2;11.0/4=2.75
9、布尔类型
与运算: True and True→T,True and False→F,F and F→F,F and T→F。
或运算:T or T=T, T or F=T, F or F=F, F or T=T.
非运算:not True=false ,not false=true;
10、创建list(有序的集合)
L=['Miracle',100,True] 负数访问:-3,-2,-1。 正数访问:0,1,2
按索引访问list:print L[0] →=‘Miracle’
倒序访问:print L[-1]=true;
添加新元素:L.append('Paul'),# 添加到尾部。
L.insert(0,'Paul') #添加到第一个
删除元素:L.pop()#删除最后一个元素
L.pop(2)# 删除索引为2的
替换元素:L[2]='Paul'
10、创建tuple ,元素是有序表,一旦创建完毕,就不能修改。t=('Adam','Lisa','Bart')
获取t(0),python规定,单元素要多加一个逗号,t=(1,),print t,t=(1,)否则,t=(1), print t,t=1(和原式不等)
11、“可变”的tuple
t=('a','b',['A','B'])
L=t[2]
L[0]='X'
L[1]='Y'
print t →('a','b',['X','Y']).
12、if语句后接表达式,用: 表示代码块的开始。
if score>=60:
print 'pass'
else:
print 'fail'
13、if
....
elif
....
else
14、for name in List/tuple
15、while:
16、多重循环
for x in L
for Y in T
17、dict
{} 表示这是一个dict按照key :value
d={
‘admin’:95,
'Lisa':85,
'Paul':75
}
访问dict:
dict[key]→ dict['Adam']
d.get('Bart')
特点是:key不能重复,可以是整型,字符串,浮点数,且没有顺序
更新dict:d['Paul']=72
18、set 持有一系列元素,这和List很像,但是set元素没有重复,而且是无序的
S=set(['A','B','C'])
19、访问set,
因为set是无序的,所以不能用索引
weekdays=set(['mon','tue','wed','thu','fri','sat'])
x='???'
if x in weekdays:
print 'input ok '
else:
print 'input error'
20、遍历set
s=set([('Adam',96),('Lisa',82),('Bart',59)])
for x in s
print x[0]+':',x[1]
21、更新set
添加 s.add(3)
删除 s.remove(4)
22、函数
查看系统函数 help(abs)
def 函数名(x):
return
可以return 多值
def move(x,y,step,angle)
nx=x+step*math.cos(angle)
ny=y-step*math.sin(angle)
return nx,ny
23、定义可变参数
def fn(* args)
在名字面前加*号。表示我们可以加入0个1个或多个参数,然后组装为一个tuple
24、对list进行切片
L=['adma','Lisa','Bart','Paul']
去前3个元素:L[0:3],从头到尾 L[:]
L[ : : 2]→['Adma','Bart'],每两个元素取一个出来。
倒序切割:
L[-2:]→['Bart','Paul']
L[:-2]→['Adma','Lisa']
25、迭代
有序集合:List,tuple,str.unicode
无序集合:set
无序并key-value :dict
26、迭代dict的value
d={'adma','Lisa','Bart','Paul'}
for v in d.value():
print v
27、迭代dict的key和value
for key,value in d.item()
print key,':',value
28、生成列表
要生成list[1,2,3,4,5,6,7,8,9,10]
L=[]
for x in range(1,11):
L.append(x * x )
29、多层表达式
[m+n for m in 'ABC' for n in '123']
30、今天的总结:tuple用(),List用[ ],dict 用{ },set 用([ ])
31、__aa__两个下划线 表示私有。