Python语言学习记录:
1、 3.0之前“/”默认为整除:1/2=0  ,可以通过执行语句: from __future__ import division 使得做除法运算。
    整除符号“//” : 1.0//2.0=0.0
    幂方运算符: **    -3**2=-9     (-3)**2=9   也可以用函数 pow  : pow(2,3) 也等于8
2、导入并调用模块和函数: import math
                        math.floor(32.9) = 32.0
   不用在每次调用函数的时候都加上模块名:from math import floor    那么可以直接使用函数:floor(32.9) = 32.0
                                    也可以通过变量来引用函数: foo=math.floor    然后直接 foo(32.9)
   没有返回值的return语句等价于return None。除非你提供你自己的return语句,每个函数都在结尾暗含有return None语句
3、 repr 函数会创建一个字符串(也就是说返回一个字符串),它以合法的Python表达式的形式来表示值。

>>> print repr("123hello")    而print "123hello"  输出 123hello
         '123hello'
      >>> print repr(1000L)
         '1000L'

     其实 repr(x)等于 `x`(3.0版本中不支持使用)
        你可以通过定义类的__repr__方法来控制你的对象在被repr函数调用的时候返回的内容。
    
    str会把值转换成为合理形式的字符串。 str是一种类型 和int long一样
        >>> print str(1000L)
            1000
4、三引号(''')里面的字符串可以不需要反斜线进行转义,与r相同
'''hello world
   "it's world" '''         或者 r'c:\python'
在3.0中所有字符串都是Unicode字符串,使用u前缀表示。(普通字符串以8位的ASCII码存储,而Unicode字符则存储为16位Unicode字符)
原始字符串不能以反斜线结尾, print r"sdfs\"会报错  应该 print r"sdfs" '\\'


5、记住列表的赋值语句不创建拷贝。你得使用切片操作符来建立序列的拷贝。

shoplist = ['apple', 'mango', 'carrot', 'banana']
 mylist = shoplist[:]                  # make a copy by doing a full slice
 del mylist[0]                         # remove first item
 print 'shoplist is', shoplist
 print 'mylist is', mylist
 # notice that now the two lists are different


6、类的__init__方法类似于C++、C#和Java中的 constructor 。

class Person:
     def __init__(self, name):
         self.name = name
     def sayHi(self):
         print 'Hello, my name is', self.name

7、类的方法和静态方法:属于类本身,可以直接用类名来调用;不属于某个对象。

@staticmethod
 def smeth():
     print 'this  is a static method'
 @classmethod
 def cmeth(cls):
     print 'this is a class method of ',cls


有两种类型的 域 ——类的变量和对象的变量,它们根据是类还是对象 拥有 这个变量而区分。
类的变量 由一个类的所有对象(实例)共享使用。只有一个类变量的拷贝,所以当某个对象对类的变量做了改动的时候,这个改动会反映到所有其他的实例上。
对象的变量 由类的每个对象/实例拥有。因此每个对象有自己对这个域的一份拷贝,即它们不是共享的,在同一个类的不同实例中,虽然对象的变量有相同的名称,但是是互不相关的。

class Person:
     '''Represents a person.'''
     population = 0     #类的变量


     def __init__(self, name):
         '''Initializes the person's data.'''
         self.name = name     #对象的变量
         print '(Initializing %s)' % self.name


         # When this person is created, he/she
         # adds to the population
         Person.population += 1


     def __del__(self):
         '''I am dying.'''
         print '%s says bye.' % self.name


         Person.population -= 1


         if Person.population == 0:
             print 'I am the last one.'
         else:
             print 'There are still %d people left.' % Person.population


     def sayHi(self):
         '''Greeting by the person.


         Really, that's all it does.'''
         print 'Hi, my name is %s.' % self.name


     def howMany(self):
         '''Prints the current population.'''
         if Person.population == 1:
             print 'I am the only person here.'
         else:
             print 'We have %d persons here.' % Person.population


         swaroop = Person('Swaroop')
         swaroop.sayHi()
         swaroop.howMany()
        
         kalam = Person('Abdul Kalam')
         kalam.sayHi()
         kalam.howMany()




Python中所有的类成员(包括数据成员)都是 公共的 (static public),所有的方法都是 有效的 。
只有一个例外:如果你使用的数据成员名称以 双下划线前缀 比如__privatevar,Python的名称管理体系会有效地把它作为私有变量。
这样就有一个惯例,如果某个变量只想在类或对象中使用,就应该以单下划线前缀。而其他的名称都将作为公共的,可以被其他类/对象使用。记住这只是一个惯例,并不是Python所要求的(与双下划线前缀不同)。
同样,注意__del__方法与 destructor 的概念类似。  调用del语句


8、继承

class SchoolMember:
     '''Represents any school member.'''
     def __init__(self, name, age):
         self.name = name
         self.age = age
         print '(Initialized SchoolMember: %s)' % self.name


     def tell(self):
         '''Tell my details.'''
         print 'Name:"%s" Age:"%s"' % (self.name, self.age),


 class Teacher(SchoolMember):  #如果在继承元组中列了一个以上的类,那么它就被称作 多重继承 。
     '''Represents a teacher.'''
     def __init__(self, name, age, salary):
         SchoolMember.__init__(self, name, age)   #Python不会自动调用基本类的constructor,你得亲自专门调用它。
         self.salary = salary
         print '(Initialized Teacher: %s)' % self.name


     def tell(self):
         SchoolMember.tell(self)
         print 'Salary: "%d"' % self.salary


 class Student(SchoolMember):
     '''Represents a student.'''
     def __init__(self, name, age, marks):
         SchoolMember.__init__(self, name, age)
         self.marks = marks
         print '(Initialized Student: %s)' % self.name


     def tell(self):
         SchoolMember.tell(self)
         print 'Marks: "%d"' % self.marks


 t = Teacher('Mrs. Shrividya', 40, 30000)
 s = Student('Swaroop', 22, 75)
 print         # prints a blank line
 members = [t, s]
 for member in members:
     member.tell() # works for both Teachers and Students




9、在print语句上使用逗号来消除自动换行

while True:
     line = f.readline()
     if len(line) == 0: # Zero length indicates EOF
         break
     print line,

10、函数参数提供默认值:只有在形参表末尾的那些参数可以有默认参数值 。例如,def func(a, b=5)是有效的,但是def func(a=5, b)是 无效 的。

def func(a, b=5, c=10):
     print 'a is', a, 'and b is', b, 'and c is', c


 func(3, 7)
 func(25, c=24)


func(c=50, a=100
11、dir()函数:当你为dir()提供一个模块名的时候,它返回模块定义的名称列表。如果不提供参数,它返回当前模块中定义的名称列表。

>>> dir()
 ['__builtins__', '__doc__', '__name__', '__package__', 'sys']
 >>> a=5
 >>> dir()
 ['__builtins__', '__doc__', '__name__', '__package__', 'a', 'sys']

12、DocStrings:文档字符串。文档字符串的惯例是一个多行字符串,它的首行以大写字母开始,句号结尾。第二行是空行,从第三行开始是详细的描述。
def printMax(x, y):

#以下就是文档字符串
     '''Prints the maximum of two numbers.


     The two values must be integers.'''
     x = int(x) # convert to integers, if possible
     y = int(y)


     if x > y:
         print x, 'is maximum'
     else:
         print y, 'is maximum'


 printMax(3, 5)
 print printMax.__doc__      # help(printMax)
 输出:




13、exec语句用来执行储存在字符串或文件中的Python语句。
        >>> exec 'print "Hello World"'
    eval语句用来计算存储在字符串中的有效Python表达式
       >>> eval('2*3')
        6