之所以学习Python,第一个是他比较简单,寒假时间充裕,而且听说功能也很不错,最重要的是,我今年的项目就要用到它。
而且刘汝佳的书上说到,一个好的Acmer要是不会一点Python那就是太可惜了。废话不说,看看第一天的学习内容!
- 如何编写Python1、cmd下输入python就可以进入开发模式2、Python编辑器:IDLE,aptana,我这里就用IDLE
- 常量与变量1、常量的定义:用对象的方法,(虽然我还不知道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)" % name12 raise NameError, name13 14 import sys15 sys.modules[__name__] = _const()
2、变量,赋值
- 数与字符串数(int,long,float,bool,complex)字符串:1、转义字符
-
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 c10 11 #切片运算符[a:b] 是左闭右开的 a,b-112 _str1 = _str[:2]13 print _str114 15 _str1 = _str[:3]16 print _str117 18 _str1 = _str[:]19 print _str1
- 数据类型1、基本数据类型(数+字符串)2、列表
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 lislen10 11 settlen = len(sett)12 print settlen
- pickle腌制(长期使用某一对象的解决方案,1:加载到内存,2:第三方文件)
import picklelista = ["ming yue ","ji shi ","you"]listb = pickle.dumps(lista)print listblistc = 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 tf2.close()