之所以学习Python,第一个是他比较简单,寒假时间充裕,而且听说功能也很不错,最重要的是,我今年的项目就要用到它。
而且刘汝佳的书上说到,一个好的Acmer要是不会一点Python那就是太可惜了。废话不说,看看第一天的学习内容!
1 class _const(object): 2 class ConstError(TypeError): pass 3 4 def __setattr__(self,name,value): 5 if self.__dict__.has_key(name): 6 raise self.ConstError, "Can‘t rebind const(%s)" % name 7 self.__dict__[name] = value 8 9 def __delatter__(self,name): 10 if name in self.__dict__: 11 raise self.ConstError, "Can‘t unbind const(%s)" % name 12 raise NameError, name 13 14 import sys 15 sys.modules[__name__] = _const()
2、变量,赋值
1 print ‘It\‘s a dog‘ 2 3 print "hello\nhello"
2、单引号,双引号,三引号
(单引号保护双引号,双引号保护单引号,三引号保留换行格式)
3、自然字符串(r):取消转义
1 print "hello boy\nhello boy" 2 3 print r‘hello boy\nhello boy‘
4、字符串简单操作(重复*,索引[],切片)
1 #字符串重复 2 string="Yinjian" 3 4 print string*20 5 6 _str = "YinJianPython" 7 8 c = _str[0] 9 print c 10 11 #切片运算符[a:b] 是左闭右开的 a,b-1 12 _str1 = _str[:2] 13 print _str1 14 15 _str1 = _str[:3] 16 print _str1 17 18 _str1 = _str[:] 19 print _str1
stu = ["Yinjian","xixi"] print stu[1] stu[1] = "xixi" print stu[1]
3、元组(不可修改)
1 stu = ("Yinjian","Tom") 2 3 print stu[1] 4 5 stu = (1,2,3) 6 print stu[1]
4、集合(建立关系,去重)
1 set1 = set("skdfjsofd") 2 set2 = set("dsflksdf") 3 4 print set1&set2 5 6 print set1|set2 7 8 print set1-set2 9 10 new = set(set1) 11 print new
1 lis = [1,2,2,3,3,"hello","hello","xixi"] 2 3 sett = set(lis) 4 5 print sett 6 7 lislen = len(lis) 8 9 print lislen 10 11 settlen = len(sett) 12 print settlen
import pickle lista = ["ming yue ","ji shi ","you"] listb = pickle.dumps(lista) print listb listc = pickle.loads(listb) print listc #读取文件 group = ("ba jiu ","wen ","qing tian") f1 = file(‘1.pk1‘,‘wb‘) pickle.dump(group,f1,True) f1.close() f2 = file(‘1.pk1‘,‘rb‘) t = pickle.load(f2) print t f2.close()
原文:http://www.cnblogs.com/TreeDream/p/6345173.html