openstack编程:python基础知识总结阅读本文可以带着下面问题:1.python的注释符号是什么?2.python输出该如何写?3.什么是内建函数?4.python的运算符都包含哪些?5.Python字符串该如何表示?6.列表和元组怎么表示?7.if语句该如何表示?8.while语句如何表示?9.如何捕获异常?10.函数如何表示?11.Python的源文件名是什么?12.常用函数都有哪些?总结一1.程序输出:和C中printf很像:1.>>> print "%s is number %d." % ("Python", 1)2.Python is number 1.复制代码将输出重定向到系统标准错误:1.>>> import sys2.>>> print >> sys.stderr, 'fatal error.'3.fatal error.复制代码将输出重定向到一个文件:1.>>> logfile = open('./out.log', 'a')2.>>> print >> logfile, 'hello world'3.>>> logfile.close()复制代码2.程序输入和内建函数raw_input():1.>>> user = raw_input('Enter your name:')2.Enter your name:chenjianfei3.>>> passwd = raw_input('Enter your password:')4.Enter your password:123456复制代码内建函数int()可将数字字符串转化为int:1.>>> num = raw_input('Input a num:')2.Input a num:1003.>>> print '2*num = %d' % num*24.Traceback (most recent call last):5. File "<stdin>", line 1, in <module>6.TypeError: %d format: a number is required, not str7.>>> print '2*num = %d' % (int(num)*2)8.2*num = 200复制代码从交互式解释器中获取帮助:1.>>>help(raw_input)复制代码3.注释# 后面是注释文档字符串注释:可以在模块,类,或者函数的开始添加一个字符串,起到在线文档的作用.1.def foo():2. "This is a doc string."3. return True;复制代码4.运算符+ -*/ 传统除法(地板除)// 浮点除法(四舍五入)% 取余** 乘方(优先级最高)比较运算符:< <= > >= == != <>逻辑运算符:and or not注意下面是合法的:1.>>> 3 < 4 < 52.True3.>>> 3 < 4 and 4 < 54.True复制代码5.变量和赋值:Python中变量名规则和C一样.python是动态类型语言,也就是说不需要预先声明变量的类型.变量的类型和值在赋值的被初始化.1.>>> count = 02.>>> miles = 10.53.>>> name = 'bob'4.>>> kilometers = miles*1.609复制代码增量赋值:+= *= ...python不支持++和--6.数字五种基本类型:int 有符号整数:-234, 0x80, -0x80long 长整数:-234L, 0x888Lbool 布尔类值:True(1), False(0)float 浮点值:3.1415, -4.2E-10, 4.2e10complex 复数:3+10j, -123-838.33J从长远来看int和long将会无缝结合.在Python2.3以后再也不会报整型溢出的错误,结果会自动转化长整型.所有L后缀可有可无.7.字符串:Python使用成对的单引号或是双引号,三引号可以用来包含特殊字符.使用[]和[ : ]得到子字符串.[ index : count]字符串有其特有的索引规则:第一个索引号是0,最后一个是-1.加号(+)用来字符串连接运算.乘号(*)用于字符串的重复.1.>>> pystr = 'Python'2.>>> iscool = 'is cool!'3.>>> pystr[0]4.'P'5.>>> pystr[2:5]6.'tho'7.>>> iscool8.'is cool!'9.>>> iscool[:2]10.'is'11.>>> iscool[3:]12.'cool!'13.>>> iscool[-1]14.'!'15.>>> pystr + iscool16.'Pythonis cool!'17.>>> pystr + ' ' + iscool18.'Python is cool!'19.>>> pystr * 220.'PythonPython'21.>>> '-' * 2022.'--------------------'23.>>> pystr = '''''python24.... is cool'''25.>>> pystr26.'python \n is cool'27.>>> print pystr28.python29.... is cool30.>>>复制代码8.列表和元组列表元素用[]包裹,元组元素用()包裹.列表元素的个数和元素的值都可以改变.元组可以看成是只读的列表.通过切片运算([], [:])可以得到子集. 列表操作:1.>>> aList = [1, 2, 3, 4]2.>>> aList3.[1, 2, 3, 4]4.>>> aList[0]5. 16.>>> aList[2:]7.[3, 4]8.>>> aList[:3]9.[1, 2, 3]10.>>> aList[1] = 511.>>> aList12.[1, 5, 3, 4]复制代码元组操作:1.>>> aTuple = ('chenjianfei', 25, 170, 'hello')2.>>> aTuple3.('chenjianfei', 25, 170, 'hello')4.>>> aTuple[:3]5.('chenjianfei', 25, 170)6.>>> aTuple[1] = 37.Traceback (most recent call last):8. File "<stdin>", line 1, in <module>9.TypeError: 'tuple' object does not support item assignment复制代码9.字典字典是Python中的映射数据类型.类似Perl中的关联数组和哈希表,有(key-value)对构成.用大括号{}包括.1.>>> aDict = {'host': 'localhost'}2.>>> aDict3.{'host': 'localhost'}4.>>> aDict['port'] = 80805.>>> aDict6.{'host': 'localhost', 'port': 8080}7.>>> aDict.keys()8.['host', 'port']9.>>> aDict['host']10.'localhost'11.>>> for key in aDict:12.... print key, aDict[key]13....14.host localhost15.port 808016.>>> aDict['host'] = 3317.>>> aDict18.{'host': 33, 'port': 8080}复制代码10.代码块和缩进对齐代码块通过缩进对齐表达代码逻辑而不是使用大括号。
11.if语句1.if expression:2. if_suite复制代码如果expression的值是True或是非零,则代码组if_suite被执行。
否则不执行。
代码组是Python中的术语,它由一条或是多条语句组成,表示一个子代码块。