简明python教程网址:http://sebug.net/paper/python
1 help(sys) 2 help(int) 3 help(list) 4 help(dict) 5 help(str) 6 help(file) 7 sys模块 8 print sys.__doc__ 9 help(sys.exit) 10 help(sys.stdin) 11 help(sys.stdout) 12 help(sys.stderr)1.运算符通常由左向右结合,即具有相同优先级的运算符按照从左向右的顺序计算。例如,2 + 3 + 4被计算成(2 + 3) + 4。一些如赋值运算符那样的运算符是由右向左结合的,即a = b = c被处理为a = (b = c)。2.
-25.5%2.25=1.53.**是幂运算,布尔(not、or、and)逻辑(&,|,^,~)4.输出变量
1 area = 5 2 print 'Area is', area 3 Area is 5[自动输出1个空格]5.
1 if condintion1: 2 func1 3 elif condition2: 4 func2 5 else : 6 func3注意:if语句在结尾处包含一个冒号——我们通过它告诉Python下面跟着一个语句块。6.
1 guess = int(raw_input('Enter an integer:'))我们为内建的raw_input函数提供一个字符串,这个字符串被打印在屏幕上,然后等待用户的输入。一旦我们输入一些东西,然后按回车键之后,函数返回输入。对于raw_input函数来说是一个字符串。我们通过int把这个字符串转换为整数,并把它存储在变量guess中。7.True、False、None首字母不能小写。8.
#while while condtion1: func1 else: func2 #for for i in range(1,5): func1 else: func2注意:else从句可有可无注释用#9.range(begin,end,step)range 以step为步长,延伸到end,但它不包含end。10.【Python的for循环更加简单、明白、不易出错。】在C/C++中,如果你想要写for (int i = 0; i < 5; i++),那么用Python,你写成for i in range(0,5)。11.def sayHello(): print 'Hello world!'sayHello()12.global x,y,z13.print 'zhanghe'*514.如果你的某个函数有许多参数,而你只想指定其中的一部分,那么你可以通过命名来为这些参数赋值——这被称作 关键参数 ——我们使用名字(关键字)而不是位置(我们前面所一直使用的方法)来给函数指定实参。func(50,c=100)先按照关键参数、在按照顺序15.除非你提供你自己的return语句,每个函数都在结尾暗含有return None语句。16.pass语句在Python中表示一个空的语句块。17.文档字符串的惯例是一个多行字符串,它的首行以大写字母开始,句号结尾。第二行是空行,从第三行开始是详细的描述。 强烈建议 你在你的函数中使用文档字符串时遵循这个惯例。18.每个Python模块都有它的__name__,如果它是'__main__',这说明这个模块被用户单独运行,我们可以进行相应的恰当操作。19.使用内建的dir函数来列出模块定义的标识符。标识符有函数、类和变量。
1 >>> import sys 2 >>> dir(sys) # get list of attributes for sys module 3 >>> dir() # get list of attributes for current module 4 >>> a = 5 # create a new variable 'a' 5 >>> dir() 6 >>> del a # delete/remove a name 7 >>> dir()20.列表21.我们在print语句的结尾使用了一个 逗号 来消除每个print语句自动打印的换行符。22.元组元组和列表十分类似,只不过元组和字符串一样是 不可变的 即你不能修改元组。#Filename:using_tuple.pyzoo = ('wolf','elephant','penguin')print 'Number of animals in the zoo is',len(zoo)
new_zoo = ('monkey','dolphin',zoo)print 'Number of animals in the new zoo is',len(new_zoo)print 'All animals in new zoo are',new_zooprint 'Animals brought from old zoo are',new_zoo[2]print 'Last animal brought from old zoo is',new_zoo[2][2]
output:Number of animals in the zoo is 3Number of animals in the new zoo is 3All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))Animals brought from old zoo are ('wolf', 'elephant', 'penguin')Last animal brought from old zoo is penguin如果你想要的是一个包含项目2的元组的时候,你必须在第一个(唯一一个)项目后跟一个逗号,你应该指明singleton = (2 , )。23.元组最通常的用法是用在打印语句中age = 22name = 'Swaroop'
print '%s is %d years old' % (name, age)print 'Why is %s playing with that python?' % name24.字典字典是dict类的实例/对象键值对在字典中以这样的方式标记:d = {key1 : value1, key2 : value2 }。注意它们的键/值对用冒号分割,而各个对用逗号分割,所有这些都包括在花括号中。记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序,那么你应该在使用前自己对它们排序。#Filename:using_dict.py
#'ab' is short for 'a'ddress 'b'ook
ab={ 'Swaroop' : 'swaroopch@byteofpython.info', 'Larry' : 'larry@eall.org', 'Matsumoto' : 'matz@ruby-lang.org', 'Spammer' : 'spammer@hotmail.com' #The last para has no ',' }
print "Swaroop's address is %s" % ab['Swaroop']
#Adding a key/value pairab['Guido'] = 'guido@python.org'
#Deleting a key/value pairdel ab['Spammer']
print '\nThere are %d contacts in the address-book\n'%len(ab)for name,address in ab.items(): print 'Contact %s at %s'%(name,address)
if 'Guido' in ab:#OR ab.has_key('Guido') print "\nGuido's address is %s"