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. 类
解析函数中的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
数:
整型
长整型
浮点型: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
相关文章推荐
- python学习笔记(一)
- Python机器学习实践指南 笔记(1)-Python机器学习的生态系统
- python学习笔记——多线程(MT)
- python学习笔记:三
- Python学习笔记——基础知识
- python 学习笔记——奇怪的reverse函数
- Python学习笔记(1)
- 流畅python学习笔记第十八章:使用asyncio包处理并发(一)
- python 第六天学习笔记
- python Tkinter学习笔记 基础UI控件 01
- Python学习笔记 -- 序列(一)序列类型
- python第三天学习笔记
- Python学习笔记10-Python MysqlHelper ,MySql 辅助类
- Spark (Python版) 零基础学习笔记(二)—— Spark Transformations总结及举例
- Python之网络编程学习笔记
- Python学习笔记6:面向对象(一)
- python学习笔记(三)
- 30. Python脚本学习笔记三十 更多关于SWIG
- Python第二周 学习笔记(2)
- Selenium2 Python 自动化测试实战学习笔记(四)