按 :这三天密集监考,没有大块的时间干正事,就一口气写了5篇《 复杂网络分析库NetworkX学习笔记 》,看点击量估计会有朋友有兴趣学习这个库,但可能很多朋友都没有接触过Python。我以前也说过,Python一点也不难学,可以说是我接触过的最简单的编程语言了(我前后学用过C、C++、C#、Basic四门语言,现在则主要用Python)。为了方便感兴趣的朋友学习Python,我上网搜索了一些Python教程,大厚本的语法书估计各位都比较忙,也没时间看,我就专找那种速成性的,还真找到一些:有《 24小时学会Python 》、《 1小时学会Python 》,还有这个《 10分钟学会Python 》(英文原版见 http://www.korokithakis.net/tutorials/python )。咱就来最快的吧:10分钟学会它! 以下是转载的全文(向原作者和译者表示感谢): 1. 准备工作 哦,你是要学习Python编程语言但是又苦于找不到一个简洁但是全面 的教程么?这个教程就是要试图在10分钟内让你掌握Python。可能它有点不像一个教程,或者说应该介于教程和cheatsheet[可以快速查找的一 个简单表单,不知道怎么翻译,译注]之间,所以在这里我只能向你展示一些最基本的概念,旨在让你能够快速入门。显然,如果你真要学习一门编程语言,你需要 使用它编码一段时间。我假定你已经有一些熟知的编程知识,因此在这里我就不再讲那些与语言无关的编程知识。教程中的关键字我都让它高亮显示,这样你就可以 一眼就看清楚。另外,为了保持教程的简洁,一些知识就只在代码中展示,只有一些简单的注释。 2. 特性 Python 是一个强类型(也就是类型都是强制指定的),动态,隐式类型的(即,你不需要声明变量),大小写敏感(var和VAR是两个不同的变量),面向对象(一切 皆是对象)的语言。[专业术语翻译有点别扭,译注] 3. 语法 Python 没有命令结束标志,并且,使用缩进来区分程序块。块开始的时候缩进开始,块结束的时候缩进结束。声明需要一个:来引领一个缩进块。注释使用#开始并且只占 一行。多行注释使用多行字符串。赋值使用等号(=),测试是否相等使用两个等号(==)。可以分别使用+=或者-=来增加或者减少变量值。这在多 个数据结构上都适用,包括字符串。你也可以在一行上使用多个变量。举例来说: myvar = 3 myvar += 2 myvar -= 1 This is a multiline comment. The following lines concatenate the two strings. mystring = Hello mystring += world. print mystring Hello world. # This swaps the variables in one line(!). myvar, mystring = mystring, myvar 4. 数据类型 在Python中可用的数据类型有列表、元组以及字典。在集合库中集合 也可用。列表就像是以维数组(但是你还可以有列表的列表),字典就是关联数组(或者叫哈希表),元组就是不可变一维数组(Python中数组可以是任何类 型,因此你可以在列表、字典或者元组中混合使用数字、字符串等数据类型)。在所有的数组类型中第一个元素的标号都是0,负数表示从后向前数,-1则表示最 后一个元素。变量可以指向函数。用法如下: sample = , (a, tuple)] mylist = mylist = List item 1 again mylist = 3.14 mydict = {Key 1: Value 1, 2: 3, pi: 3.14} mydict = 3.14 mytuple = (1, 2, 3) myfunction = len print myfunction(mylist) 你可以使用:取到数组的一个范围,:前留空则表示从第一个元素开始,:后留空则表示直到最 后一个元素。负值表示从后索引(即-1是最后一个元素)。如下所示: mylist = print mylist print mylist print mylist print mylist 5. 字符串 字符串可以使用单引号或者双引号,你可以在使用一个引号的里面嵌套使用另一种引号(也就是说,He said 'hello'.是合法的)。多行字符串则使用三引号(单双皆可)。Python还可以让你设置Unicode编码,语法如下:uThis is a unicode string.使用值填充一个字符串的时候可以使用%(取模运算符)和一个元组。每个%s使用元组中的一个元素替换,从左到右。你还可以使用字典。如下 所示: print Name: %snNumber: %snString: %s % (myclass.name, 3, 3 * -) Name: Poromenos Number: 3 String: --- strString = This is a multiline string. # WARNING: Watch out for the trailing s in %(key)s. print This %(verb)s a %(noun)s. % {noun: test, verb: is} This is a test. 6. 流程控制 流程控制使用while,if,以及for。没有select[和哪种语言对比?不知道。译 注],使用if代替。使用for来列举列表中的元素。要得到一个数字的列表,可以使用range(number)。这些声明的语法如下: rangelist = range(10) print rangelist for number in rangelist: # Check if number is one of # the numbers in the tuple. if number in (3, 4, 7, 9): # Break terminates a for without # executing the else clause. break else: # Continue starts the next iteration # of the loop. It's rather useless here, # as it's the last statement of the loop. continue else: # The else clause is optional and is # executed only if the loop didn't break. pass # Do nothing if rangelist == 2: print The second item (lists are 0-based) is 2 elif rangelist == 3: print The second item (lists are 0-based) is 3 else: print Dunno while rangelist == 1: pass 7. 函数 函数使用def关键字。可选参数在必须参数之后出现,并且可以被赋一默认值。对命名参数而言,参数名参数 名被赋一个值。函数可以返回一个元组(打开元组你就可以实现返回多个值)。Lanbda函数是个特例,它由一个表达式构成。参数使用引用传递,但是可变类 型(元组,列表,数字,字符串等等)不能被改变。举例如下: # arg2 and arg3 are optional, they have default values # if one is not passed (100 and test, respectively). def myfunction(arg1, arg2 = 100, arg3 = test): return arg3, arg2, arg1 ret1, ret2, ret3 = myfunction(Argument 1, arg3 = Named argument) # Using print with multiple values prints them all, separated by a space. print ret1, ret2, ret3 Named argument 100 Argument 1 # Same as def f(x): return x + 1 functionvar = lambda x: x + 1 print functionvar(1) 8. 类 Python部分支持类的多重继承。私有变量和方法可以使用至少两个_开始并且至少一个_结束来声明,比 如__spam(这只是约定,语言中并没有强制规定)。我们可以给类的实例赋任意的变量。请看下例: class MyClass: common = 10 def __init__(self): self.myvariable = 3 def myfunction(self, arg1, arg2): return self.myvariable # This is the class instantiation classinstance = MyClass() classinstance.myfunction(1, 2) 3 # This variable is shared by all classes. classinstance2 = MyClass() classinstance.common 10 classinstance2.common 10 # Note how we use the class name # instead of the instance. MyClass.common = 30 classinstance.common 30 classinstance2.common 30 # This will not update the variable on the class, # instead it will create a new one on the class # instance and assign the value to that. classinstance.common = 10 classinstance.common 10 classinstance2.common 30 MyClass.common = 50 # This has not changed, because common is # now an instance variable. classinstance.common 10 classinstance2.common 50 # This class inherits from MyClass. Multiple # inheritance is declared as: # class OtherClass(MyClass1, MyClass2, MyClassN) class OtherClass(MyClass): def __init__(self, arg1): self.myvariable = 3 print arg1 classinstance = OtherClass(hello) hello classinstance.myfunction(1, 2) 3 # This class doesn't have a .test member, but # we can add one to the instance anyway. Note # that this will only be a member of classinstance. classinstance.test = 10 classinstance.test 10 9. 异常处理 Python中的异常处理使用try-except 程序块: def somefunction(): try: # Division by zero raises an exception 10 / 0 except ZeroDivisionError: print Oops, invalid. fnExcept() Oops, invalid. 10. 包的导入 使用import 导入外部包,你也可以为单独的函数使用from import 这种形式来导入。下面是一个例子: import random from time import clock randomint = random.randint(1, 100) print randomint 64 11. 文件I/O Python有一个很大的内建库数组来处理文件的读写。下面的例子展示如何使用Python的文件I/O来序列化(使用 pickle把数据结构转换成字符串)。 import pickle mylist = # Open the file C:binary.dat for writing. The letter r before the # filename string is used to prevent backslash escaping. myfile = file(rC:binary.dat, w) pickle.dump(mylist, myfile) myfile.close() myfile = file(rC:text.txt, w) myfile.write(This is a sample string) myfile.close() myfile = file(rC:text.txt) print myfile.read() 'This is a sample string' myfile.close() # Open the file for reading. myfile = file(rC:binary.dat) loadedlist = pickle.load(myfile) myfile.close() print loadedlist 12. 其他 条 件语句可以链接使用。1 a 3检查a是否介于1和3之间。 可以使用del删除数组中的元素或者变量。 List comprehensions[不知怎么翻译,译注]提供一个强大的方法来创建和操作list(列表)。它由一个后跟一个for语句的表达式组成,这个 for语句后跟一个或者多个if@ 或者 @for语句,就像这样: lst1 = lst2 = print print # Check if an item has a specific property. # any returns true if any item in the list is true. any(i % 3 for i in ) True # Check how many items have this property. sum(1 for i in if i == 3) 3 del lst1 print lst1 del lst1 全局变量可以声明在函数外边并且不需要任何特殊 的声明就可以直接读取使用。但是如果你要改变该全局变量的值,你必须在函数的开始使用global关键字声明它,否则,Python将创建一个局部变量并 给他赋值(要特别注意这点,不知道的话很容易犯错)。比如: number = 5 def myfunc(): # This will print 5. print number def anotherfunc(): # This raises an exception because the variable has not # been assigned to before printing. Python knows that it a # value will be assigned to it later and creates a new, local # number instead of accessing the global one. print number number = 3 def yetanotherfunc(): global number # This will correctly change the global. number = 3 13. 结束语 这个教程并没有列出Python的全部细节(甚至连一个子集都算不上)。Python还有一系列的库以及许多其他功能,这就需要你通 过其他途径来学习了,比如很优秀的在线教程Dive into Python。我只希望这个教程能让你对Python快速上手。如果你觉得有什么需要改进或者添加的地方,或者你想看到的其他任何东西(类,错误处理,任 何东西),那就在这里留言吧。
by 图灵刘江 转载请注明出处:http://blog.csdn.net/turingbook 【图灵本月主打书】 iPhone开发基础教程 Amazon榜首图书,让我们抢先拥抱软件开发的未来 Flex 3权威指南 来自Adobe公司的官方教程 刚才看 HackerNews 获悉,昨天, ACM(计算机学会)将 最新一届图灵奖颁发给了麻省理工学院的女教授 Barbara Liskov 。她将获得Intel和Google赞助的25万美元奖金。 这位美国第一位计算机科学女博士,如今成为了有史以来第二位女性图灵奖得主(第一位是前年得奖的 Frances Allen )。如果说前年和 去年 两届图灵奖得主我们很多同学都不熟悉的话,那么Liskov你要是没听说过,那就别大言不惭地说自己是程序员啦,面壁三天去先。什么?你不知道图灵奖?那,那你来CSDN干嘛? ACM网站正式的颁奖理由是: For contributions to practical and theoretical foundations of programming language and system design, especially related to data abstraction, fault tolerance, and distributed computing. (对编程语言和系统设计的实践与理论基础,尤其是数据抽象、容错和分布式计算方面的贡献。) Wikipedia上对她的简介是这样的(这个条目是我刚才写的,呵呵): 芭芭拉利斯科夫 (1939年),本名Barbara Jane Huberman。美国计算机科学家,2008年图灵奖得主,2004年约翰冯诺依曼奖得主。美国工程院院士,美国艺术与科学院院士。ACM会士。现任麻省理工学院电子电气与计算机科学系教授。 1961年在加州大学伯克利分校获得数学学士学位。1968年在斯坦福大学获得博士学位,她是美国第一个计算机科学女博士。导师为1971年图灵奖得主约翰麦卡锡,论文题目是国际象棋残局程序。 利斯科夫领导了许多重要的项目,包括小型低成本交互式的分时操作系统Venus,第一个支持数据抽象的面向对象编程语言CLU的设计与实现,第一个支持分布式程序实现的高级语言Argus,面向对象数据库系统Thor,还有最近的Byzantine分布式容错系统。其中,CLU语言对现代主流语言如C++/Java/Python/Ruby/C#都有比较深远的影响。而她从这些实际项目中提炼出来的数据抽象思想,已经成为软件工程中最重要的精髓之一。 她另外一个在程序设计中有广泛应用的成就,是与周以真(Jeannette Wing)一起提出的Liskov替代原则,是面向对象最重要的几大原则之一。 【图灵图书推荐】图灵奖得主著作:Jim Gray 事务处理:概念与技术(英文版) Ian Sutherland 高速CMOS电路设计:Logical Effort方法(英文版) 可以说,Liskov是历史上为数不多的与程序设计直接相关,或者说是程序员出身的图灵奖得主。她的博士论文就是一个程序,还是一个游戏程序嘛。而Liskov替代原则,只要你用面向对象,就很可能天天在用。 关于CLU语言,还可以多说几句。在Wikipedia CLU条目 上有这样的一段话: Python and Ruby borrowed several concepts from CLU (such as the yield statement and multiple assignment) CLU and Ada were major inspirations for C++ templates. CLU's exception handling mechanisms also influenced newer languages like Java and C++. All objects in a CLU program live in the heap, and memory management is automatic. These elements directly influenced Java. Python and C# include generators (iterators in C#), which first appeared in CLU as iterators. Lua took multiple assignment and multiple returns from function calls from CLU. 搜索了一下,国外有 DDJ 和 MIT 网站有报道。国内 新浪 和 搜狐 基本上就是翻译了上述外国网站上的内容,内容很业余,但是比起前几年过了多少天也没反应,已经强很多了。DDJ的报道还是比较深入的,还有一个小的QA: Q : When you began your career in computer science, it was still a relatively young field. How have you seen this discipline evolve over time -- at MIT and elsewhere? A : The change has been tremendous. When I started, most of the field was unexplored and there were obvious problems everywhere -- lots of low-hanging fruit, but also very fundamental issues that were poorly understood and very confusing. Today the field is on a very sound foundation. There are still many problems to work on, but now this work happens in the context of all that has gone before. When I started, this context was missing, so you just struck out on your own. Q : Looking back at your career, what is the single accomplishment of which you are most proud? A : Probably the development of the concept of data abstraction and the CLU programming language. This work was done at MIT in the 1970s. Q : Where do you plan to focus your research going forward? A : Today I am working primarily on distributed systems -- systems that run on many computers connected by a network like the Internet. My focus recently has been on the security of online storage. I believe that more and more users will store their information online, but the storage they use needs to be implemented so that they don't lose their information, their information is available when they need it, and they can be confident that their confidential information will not be leaked. Q : As the first woman to earn a PhD in computer science, what advice would you give to other women who are considering going into this field? A : I have found computer science to be a wonderful field to work in. I think the main reason is that the kind of thinking and problem-solving it requires matches my abilities. I believe that finding work to do that you like and are good at is the most important way to find a satisfying career. Young women (and young men) who find that computer science is a match for them should pursue it. There is lots of interesting work remaining to be done. Q : When you began studying computer science at Stanford, computers were big mainframes and the Internet was still in the distant future. Today, computers fit in the palm of our hands -- many are much smaller -- and the Internet is ubiquitous. Given that you have watched these transformations over the last five decades from a front-row seat, what do you think the next half-century will hold? A : I don't have a crystal ball! It seems obvious that computers and the Internet will continue to be very important to individuals, companies and society. But I don't know the exact form this will take. 【延伸阅读】 2007年图灵奖揭晓 Frances Allen荣获2006年图灵奖 帮助寻找图灵奖得主Jim Gray 图灵奖得主、FORTRAN之父逝世一位回头浪子的传奇人生