python2 4 day

注释:

单行注释#

多行注释“”“   ”“”或‘’‘   ’‘’

变量是对数据对象的一个引用(类似Windows的快捷方式)

一切数据都是对象

ptyhon内部的引用计数:sys.getrefcount

  >>> import sys
  >>> d="hi"
  >>> sys.getrefcount("hi")
  11
  >>> e="hi"
  >>> sys.getrefcount("hi")
  12
  >>> f="hi"
  >>> sys.getrefcount("hi")
 13
 >>> e=1
 >>> sys.getrefcount("hi")
 12
 >>> f=2
 >>> sys.getrefcount("hi")
 11
 >>> d=3
 >>> sys.getrefcount("hi")#如果值为0,说明被销毁
 10

 

 

多重赋值

 >>> a,b,c="hi","world","hehe"
 >>> a
 'hi'
 >>> b
 'world'
 >>> c
 'hehe'
 >>> 

删除del(把变量名指向的对象链接给拆掉)

  >>> a,b,c="hi","world","hehe"
  >>> del a,b,c
  >>> a
 
  Traceback (most recent call last):
    File "<pyshell#31>", line 1, in <module>
      a
  NameError: name 'a' is not defined
  >>> b

  Traceback (most recent call last):
   File "<pyshell#32>", line 1, in <module>
     b
 NameError: name 'b' is not defined
 >>> c
 
 Traceback (most recent call last):
   File "<pyshell#33>", line 1, in <module>
     c
 NameError: name 'c' is not defined

多用三个内置:dir type help

1 >>> import sys#要查看python某个模块可以干什么,先要导入模块
2 >>> dir(sys)
3 ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_mercurial', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_traceback', 'exc_type', 'exc_value', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'py3kwarning', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']
1 >>> x=[1,'a']#使用dir()函数可以查看对像内所有属性及方法
2 >>> dir(1)
3 ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
 >>> a=1
 >>> type(1)
 <type 'int'>#打印变量的类型
 >>> import time#要查看python某个模块可以干什么,先要导入模块
 >>> help(time)#查看该模块下的方法的具体用法

xrange函数说明:用法与range完全相同,所不同的是生成的不是一个数组,而是一个生成器。

xrange做循环的性能比range好,因为不需要一上来就开辟一块很大的内存空间,尤其是返回很大的时候,尽量用xrange吧,除非你是要返回一个列表

range()和xrange() 在Python 2里是两种不同的实现。但是在Python 3里,range()这种实现被移除了;保留了xrange()的实现,且将xrange()重新命名成range()。

  >>> [x for x in xrange(5)]#这个等效于下面那个
  [0, 1, 2, 3, 4]
  
  >>> new_list=[]#
  >>> for x in xrange(5):#
      new_list.append(x)#
      print new_list#
  
     
 [0]
 [0, 1]
 [0, 1, 2]
 [0, 1, 2, 3]
 [0, 1, 2, 3, 4]
  >>> [x+1 for x in xrange(5)]#这个等效于下面那个
  [1, 2, 3, 4, 5]
  
  >>> new_list=[]#
  >>> for x in xrange(5):#
      new_list.append(x+1)#
      print new_list#
       
 [1]
 [1, 2]
 [1, 2, 3]
 [1, 2, 3, 4]
 [1, 2, 3, 4, 5]

 

posted @ 2015-08-09 21:16  沐风先生  阅读(209)  评论(0编辑  收藏  举报