您的位置:首页 > 编程语言 > Python开发

python 学习笔记

2012-08-27 14:48 183 查看
1. 数据类型:

数:

整型
长整型
浮点型:52.3E-4表示52.3 * 10-4
复数:(-5+4j)和(2.3-4.6j)

字符串:

单引号
双引号
三引号
转义字符
自然字符串:r"Newlines are indicated by \n",不转义
字符串是不可变的
自动级联字符串

2. 标识符的命名

变量是标识符的例子。 标识符 是用来标识 某样东西 的名字。在命名标识符的时候,你要遵循这些规则:
标识符的第一个字符必须是字母表中的字母(大写或小写)或者一个下划线(‘ _ ’)。
标识符名称的其他部分可以由字母(大写或小写)、下划线(‘ _ ’)或数字(0-9)组成。
标识符名称是对大小写敏感的。例如,myname和myName不是一个标识符。注意前者中的小写n和后者中的大写N.
有效 标识符名称的例子有i、__my_name、name_23和a1b2_c3。
无效 标识符名称的例子有2things、this is spaced out和my-name。

3. 数据结构

列表

元组

字典

序列

参考

字符串的方法

4. 类

#!/usr/bin/python
# Filename: person.py
# Person Class, functions, variables

class person:
population = 0
def __init__(self, name):
self.name = name
#        person.population = person.population +1
self.__class__.population += 1
print "%s was born." % self.name

def __del__(self):
#        person.population = person.population -1
self.__class__.population -= 1
print "%s was dead." % self.name

def sayHi(self):
print "Hello, I am %s. Nice to meet you." % self.name

def howMany(self):
print "%s said: there are %d persons." % (self.name,person.population)

cindy = person('cindy')
cindy.sayHi()
cindy.howMany()

cherry = person('cherry')
cherry.sayHi()
cherry.howMany()

cindy.howMany()
cherry.howMany()


解析函数中的population的问题:用self.__class__.population是正常的。可是用person.population时会有如下的错误:

cherry was dead.

Exception AttributeError: "'NoneType' object has no attribute 'population'" in <bound method person.__del__ of <__main__.person instance at 0xb772b5cc>> ignored
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: