布尔真,假值
#encoding:utf-8 if True: print u'这是布尔真值' if False: print u'这是布尔假值'
与或非
and or not
#死循环
while True:
print 'hello world'
#encoding:utf-8 for i in range(3): while True: print u'打印三次后退出' break
python支持while,else格式
#encoding:utf-8 while True: print u'真' break else: print u'假'
list,数组
#声明一个list, 依次放入数字,字母,对象,list
#encoding:utf-8
testList=['a']
testList.append(1)
class Lei():
pass
lei = Lei()
testList.append(lei)
blist = ['blist']
testList.append(blist)
print testList
['a', 1, <__main__.Lei instance at 0x00000000020E5A88>, ['blist']]
#instance 意思为类的实例
pop为删除最后一个值,并返回这个值
len():数组的长度
testList = [0,1,2,3,4,5,6,7,8,9] for i in range(0,10,2): print testList[i]
xrange:不会生成真正的list,是一个迭代器,比range节省内存,运行起来更快
test.append['a'] #增,在最后一位增加内容
testList.insert(0, 'a') #增,增加加特定位置的特定内容
del testList[0] #删,删除第0个位置的内容
testList.remove(0) #删,删除值0
testList[0]='a' #改
testList[0:10:2] #查,切片
list可作为迭代器,直接用for循环
for i in testList: print i
#数组的排序
>>> a = [1,8,3,0,'a','z','m']
>>> a.sort()
>>> a
[0, 1, 3, 8, 'a', 'm', 'z'] #sort会改变原列表
>>>
>>> a.reverse() #reverse()倒序排列
>>> a
['z', 'm', 'a', 8, 3, 1, 0]
>>> sorted(a) #sorted不会影响原来列表,会创建一个新的列表
[0, 1, 3, 8, 'a', 'm', 'z']
>>> a
['z', 'm', 'a', 8, 3, 1, 0]
倒序排列
a.sort()
a.reverse() # 升序+翻转