摘要:
第十一章、 异常 1) try/except/else格式 try: s = raw_input('--> ')except EOFError: print 'Why did you do an EOF on me?'except: print 'Error occurred.'else:print 'Done' except参数说明: except: Catch all (or all other) exception types. except name: Catch a specific exception only. 阅读全文
摘要:
第十章、 输入/输出 1) 文件poem = '''Programming is fun use Python!'''f = file('poem.txt', 'w') # open for 'w'ritingf.write(poem) # write text to filef.close() # close the file可以使用help(file)来了解详情。2) 储存器 pickle在文件中储存Python对象,cPickle(C语言,更快)import cPickle as pshopl 阅读全文
摘要:
第九章、 类与面向对象 1) 类 基本类/超类/父类被导出类或子类继承。 Inheritance继承 Inheritance is based on attribute lookup in Python (in X.name expressions). Polymorphism多态 In X.method, the meaning of method depends on the type (class) of X. Encapsulation封装 Methods and operators implement behavior; data hiding is a convention by 阅读全文
摘要:
第八章、 第一个python程序#!/usr/bin/env pythonimport osimport sysimport time source = [r'G:\s1', r'G:\s2']target_dir = r'G:\d' + os.sep today = target_dir + time.strftime('%Y%m%d')now = time.strftime('%H%M%S') comment = raw_input('Enter a commnet -->')if len 阅读全文
摘要:
Python中有三种内建的数据结构——列表、元组和字典。 1) Lists列表 [,] 列表是序列的一种 shoplist = ['apple', 'carrot', 'banana']print shoplist #['apple', 'carrot', 'banana']shoplist.append('orange') #末尾加入一个print shoplist #['apple', 'carrot', 'banana', 阅读全文
摘要:
第六章、 模块1) 模块sys模块字节编译的.pyc文件,优化编译后生成pyo文件2) from..import语句import sys print 'The command line arguments are:' for i in sys.argv: print i print '\n\nThe PYTHONPATH is', sys.path, '\n' 3) __name__只想在程序本身被使用的时候运行主块,而在它被别的模块输入的时候不运行主块from sys import * print 'The command line a 阅读全文
摘要:
第五章、 函数 定义语句后面要加冒号 1) 定义函数 def sayHello(): print 'Hello World!'sayHello() 2) 变量作用域 LEGB原则 L本地作用域 E上层结构中def或lambda的作用域 G全局作用域 B内置作用域3) 工厂/闭合函数 def maker(N): def action(X): return X ** N return action f = maker(2)print f #<function action at 0x00E87730>print f(3) #9print f(4) #16g =... 阅读全文
摘要:
第四章、 控制流 控制语句后面要加冒号: 1) if语句 if guess == number: print 'Congratulations, you guessed it.' # New block starts hereelif guess < number: print 'No, it is a little higher than that' # Another blockelse:print 'No, it is a little lower than that' if not False and True: #组合条件 pri 阅读全文
摘要:
第三章、 运算符与表达式 1) 运算符 + 加 - 减 * 乘 ** 幂 / 除 // 取整除 % 取模 << 左移 >> 右移 & 按位与 | 按位或 ^ 按位异或 ~ 按位翻转 < 小于 > 大于 <= 小于等于 >= 大于等于 == 等于 != 不等于 not 布尔“非” and 布尔“与” or 布尔“或”运算符优先级 建议用括号来设置优先级2) 表达式 length = 5 breadth = 2 area = length * breadth 阅读全文
摘要:
第二章、 类型常量5,1.23,9.25e-3,’This is a string’,”It’s a string!”1) 数整数:2长整数:浮点数:3.23,52.3E-4复数:-5+4j,2.3-4.6jac =-8.33 +1.2j print ac.real #-8.33 print ac.imag #1.2 print ac.conjugate() #(-8.33-1.2j) 二进制:0b1000八进制:0o307十六进制:0xFF2) 字符串单引号(')'Quote me on this'双引号(")"What's your na 阅读全文
摘要:
第一章、 简介官方介绍:Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程。Python简洁的语法和对动态输入的支持,再加上解释性语言的本质,使得它在大多数平台上的许多领域都是一个理想的脚本语言,特别适用于快速的应用程序开发。安装:Python:http://www.python.org/download/PyScripter:http://code.google.com/p/pyscripter/UltraEdit的Python语法高亮:http://www.ultraedit.com/downloads/extras.htmlPython 阅读全文