python 学习笔记一
#coding=UTF-8'''Created on 2011-5-18@author: lingyibin'''import typesimport mathprint 'hello'#appenda = [1,2,3,4,5,6]a.append([2,4])print a[6][1]print a;#extenda = [1,2,3,4,5,6]a.extend([2,4])print a#popa.pop()print aa.pop(2)print a#长度print len(a)#算出元素在list中出现的次数print a.count(4)#算出元素在list中的位置print a.index(4)'''4[1, 2, 3, 4, 5, 6, [2, 4]][1, 2, 3, 4, 5, 6, 2, 4][1, 2, 3, 4, 5, 6, 2][1, 2, 4, 5, 6, 2]612'''# list的反转a.reverse()print a# 用list来模拟栈a = []a.append(0)a.append(3)a.append(5)a.pop()print a# 用list来模拟队列a = []a.append(0)a.append(3)a.append(5)a.pop(0)print a#用list来模拟树leaf1 = [0,1]leaf2 = [2,3]leaf3 = [4,5]leaf4 = [6,7]branch1 = [leaf1,leaf2]branch2 = [leaf3,leaf4]root = [branch1,branch2]print root#把字符串按一定的格式输出a=["123","456","abc","Abc","AAA"]k = [k.center(7) for k in a] print k #[' 123 ', ' 456 ', ' abc ', ' Abc ', ' AAA ']#得到a中仅由字母组成的字条串,并把它变成大写k = [k.upper() for k in a if k.isalpha()]print k #['ABC', 'ABC', 'AAA']k = [ k.lower() for k in a if k.isupper() ]print k #['aaa']k = [int(k) for k in a if k.isdigit()]print k #[123, 456]a=[123,456,"abc","Abc","AAA"]k = [k for k in a if type(k) == types.IntType]print kb = """Hello , I am lingyibin.nice to meet you!\a"""print b#把句子格式化,即开头大写c = "THIS IS A SENTENCE!".capitalize() #This is a sentence!print c#大小写互换print c.swapcase()#字符串查找print c.index("is") #2print c.rindex("is") #反向查找,结果5c = "ok abc abc abc" print c.find("abc",7) #从7的位置开始查找,结果#7print c.find("abc",4,9) #4到9的位置查找,不包含9,结果-1print c.count("abc") #算字符串出现了几次,结果3#按格式打印print "%s is good,he he ,%d" % ("Food",2) #这里,在linux下,注意%与%的差别print "%s’s height is %dcm"%("Charles",180)#转为8进制print "%d is %o or %#o"%(16,16,16)#转为16进制print "%d is %x or %#x"%(16,16,16)#科学表示法print "%e"%(1000) #1.000000e+03print "%E"%(1000) #1.000000E+03#字符转数字,数字转字符print "%c"%(68)print ord('0')print ord('A')print ord('a')print chr(ord('d')+5)#固定字符打印。print "hello".ljust(10)print "hello".rjust(10)print "hello".center(10)print "hello".center(10).lstrip() #去掉左边的空格print "hello".center(10).rstrip() #去掉右边的空格print "hello".center(10).strip() #去掉两边的空格#分解与组合print "\t".join(["Hello","World","Python","Said"]) #Hello World Python Saidprint " ".join("Hello World Python Said".split()) #Hello World Python Said#元组a,b=(1,2)print a,b #1 2#巧妙地互换b,a=a,bprint a,b #2 1#用in来查找str="abcd"if "a" in str: print " ok "x=12l=[12,13]if x in l : print "x is in l"#取部分元素打印。print str[1:] #bcdprint l[1:] #[13]print l[1:]*2 #[13, 13]pricelist={"clock":12,"table":100,"xiao":100 }print pricelist["clock"] #12del pricelist["clock"]print pricelistprint pricelist.items() #[('table', 100), ('xiao', 100)]print pricelist.values() #[100, 100]print pricelist.keys() #['table', 'xiao']# tuple中有两个元素,一个是key,一个是valuepricelist=dict([("clock",12),("table",100),("xiao",100)])print pricelist#加入一个元素pricelist["apple"]=12print pricelist #{'table': 100, 'apple': 12, 'xiao': 100, 'clock': 12}#复制a = pricelist.copy()print a #{'table': 100, 'clock': 12, 'apple': 12, 'xiao': 100}b = pricelista["cool"] = 101print pricelist #{'table': 100, 'apple': 12, 'xiao': 100, 'clock': 12}#下面的是直接引用。b["cool"] = 101print pricelist #{'table': 100, 'cool': 101, 'apple': 12, 'xiao': 100, 'clock': 12}# print pricelist["pear"] #报错print pricelist.get("pear") # 打印Noneprint pricelist.has_key("pear") #Falsepricelist.clear()pricelist=dict([(x,10*x) for x in [1,2,3]])print pricelistprint range(1,10,2) #[1, 3, 5, 7, 9]#乘方和n次方根print 2**10 #1024print 27**(1.0/3)print math.pow(1024, 1.0/10)?