之所以学习Python,第一个是他比较简单,寒假时间充裕,而且听说功能也很不错,最重要的是,我今年的项目就要用到它。

而且刘汝佳的书上说到,一个好的Acmer要是不会一点Python那就是太可惜了。废话不说,看看第一天的学习内容!

  • 如何编写Python
    1、cmd下输入python就可以进入开发模式
    2、Python编辑器:IDLE,aptana,我这里就用IDLE
  • 常量与变量
    1、常量的定义:用对象的方法,(虽然我还不知道python怎么实现对象编程)

     class _const(object):
    class ConstError(TypeError): pass def __setattr__(self,name,value):
    if self.__dict__.has_key(name):
    raise self.ConstError, "Can't rebind const(%s)" % name
    self.__dict__[name] = value def __delatter__(self,name):
    if name in self.__dict__:
    raise self.ConstError, "Can't unbind const(%s)" % name
    raise NameError, name import sys
    sys.modules[__name__] = _const()

    2、变量,赋值

  • 数与字符串
    数(int,long,float,bool,complex)
    字符串:
    1、转义字符
  •  print 'It\'s a dog'
    
     print "hello\nhello"

    2、单引号,双引号,三引号
    (单引号保护双引号,双引号保护单引号,三引号保留换行格式)
    3、自然字符串(r):取消转义

     print "hello boy\nhello boy"
    
     print r'hello boy\nhello boy'

    4、字符串简单操作(重复*,索引[],切片)

     #字符串重复
    string="Yinjian" print string*20 _str = "YinJianPython" c = _str[0]
    print c #切片运算符[a:b] 是左闭右开的 a,b-1
    _str1 = _str[:2]
    print _str1 _str1 = _str[:3]
    print _str1 _str1 = _str[:]
    print _str1
  • 数据类型
    1、基本数据类型(数+字符串)
    2、列表
    stu = ["Yinjian","xixi"]
    print stu[1] stu[1] = "xixi"
    print stu[1]

    3、元组(不可修改)

     stu = ("Yinjian","Tom")
    
     print stu[1]
    
     stu = (1,2,3)
    print stu[1]

    4、集合(建立关系,去重)

     set1 = set("skdfjsofd")
    set2 = set("dsflksdf") print set1&set2 print set1|set2 print set1-set2 new = set(set1)
    print new
     lis = [1,2,2,3,3,"hello","hello","xixi"]
    
     sett = set(lis)
    
     print sett
    
     lislen = len(lis)
    
     print lislen
    
     settlen = len(sett)
    print settlen
  • pickle腌制(长期使用某一对象的解决方案,1:加载到内存,2:第三方文件)
    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()

 

05-12 07:36