python(2)-------The syntax of python
来源:互联网 发布:抓取淘宝数据包 编辑:程序博客网 时间:2024/06/13 15:30
if测试的一般形式:
if-elif-else语法举例(Python中的多路分支):
- myname='Sophia'
- if myname=='Jane':
- print "The is the first sister"
- elif myname=='Ella':
- print'This is the second sister'
- else:
- print 'This is Sophia'
python的代码块分隔符:
- x=1
- if x:
- y=2
- if y:
- print'block2'
- print'block1'
- print 'block0'
以上面这段代码为例,包含三个模块:第一个完全没有缩进,第二个缩进四格,第三个缩进八格,这里注意,python不在乎你怎么缩进代码。只在乎缩进是否一致!!!由此我们终于可以知道了,为什么if不需要C++中{}的约束,也不需要matlab中类似endif的约束了,因为它需要的知识相同缩进。相对应的,如果你硬要在if外面加个大括号反倒有语法错误了!
- 可以横跨数行的语法:
- 另外在做语句判定时,我们不得不讲的是逻辑真值判定,这里有三个操作:and or not
我们来看些例子:
- 1 and 2
- 2
- >>> 1 and 0
- 0
- >>> 0 and 1
- 0
- >>> 5 and 6
- 6
- >>> 6 and 5
- 5
- >>> 1<2 and 2>3
- False
- >>> 1<2 and 2<3
- True
- >>> 1<2 , 2>3
- (True, False)
- >>>
- >>>
- >>> 2 or 3, 3 or 2, 1 or 0, 0 or 0 #return left operand if true
- (2, 3, 1, 0)
- >>> 2 and 3, 3 and 2, 1 and 0, 0 and 1 #return left operand if false
- (3, 2, 0, 0)
最后,总结一下本小结的知识要点,答案都在上面,请大家自行练习:
1. while循环
while循环的一般格式:
for example:
- break,continue,pass,else:
举个例子:
- def fun1():
- print 'run fun1'
- def fun2():
- print 'run fun2'
- x='Sophia'
- while x:
- if len(x)%2:
- fun1()
- else:
- fun2()
- x=x[1:]
- if len(x)<=1:
- break
- print'end'
运行结果:
- run fun2
- run fun1
- run fun2
- run fun1
- run fun2
- end
2. for循环
for循环的一般语法:
example1:
- sum = 0
- for x in [1,2,3,4]:
- sum = sum+x;
- print sum
- S=('Sophia','test','prog')
- for x in S:
- print x,
结果:
- >>>
- 10
- Sophia test prog
example2:
- a = []
- for i in range(10):
- a.append(i)
- print a
- S='Sophia'
- b=[]
- i=0
- while i<len(S):
- b.append(S[i])
- i+=1
- print b
- i=0
- for j in range(1,len(S)-1):
- if b[j]<b[j-1]:
- print a[i]
- i+=1
结果:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
['S', 'o', 'p', 'h', 'i', 'a']
2
- 嵌套循环:
- s1=[1,2]
- s2='scam'
- res=[]
- for x in s1:
- for x in s2:
- res.append(x)
- print res
结果:
['s', 'c', 'a', 'm', 's', 'c', 'a', 'm']
- 现在我们引入python中文件打开的方式,首先说下暴力方式:
- x=1
- if x:
- y=2
- if y:
- print'block2'
- print'block1'
- print 'block0'
for循环进行文件迭代:
这就是传说中的文件迭代器,当然还存在其他的迭代器,比如字典有字典的迭代器,如上一节中所讲:
- 循环计数器:range
range常用在for循环中产生索引,也可以用在需要整数列表的任何地方:
>>> range(5),range(2,5),range(0,10,2)
([0, 1, 2, 3, 4], [2, 3, 4], [0, 2, 4, 6, 8])
- 非完全遍历:
- >>> S='Sophia'
- >>> range(0,len(S),2)
- [0, 2, 4]
- >>> for i in range(0,len(S),2):
- print S[i]
- S
- p
- i
- 并行遍历:zip和map
zip创建配对后元组列表:
map与zip的唯一不同是,map用类似的方法把序列的元素配对后,为较短的序列以None补齐。
另外还可以用zip构造字典:
- keys = ['a','b','c']
- vals = [1,2,3]
- Dic={}
- for (k,v) in zip(keys,vals):
- Dic[k]=v
结果:
>>>Dic
{'a': 1, 'c': 3, 'b': 2}
- 偏移元素:enumerate
- S = 'Spam'
- offset=0
- for item in S:
- print item, 'appears at offset', offset
- offset+=1
- #same function can be realized by enumerate:
- E = enumerate(S)
- print E.next()
- print E.next()
- print E.next()
- print E.next()
结果:
enumerate函数返回一个生成器对象,该对象有一个next方法,每次列表遍历时,会返回一个(index,value)的元组。
第三节、文档
之前有朋友一直在问,Python的语法即便非常清晰,其预置功能数量依然惊人:内置函数和异常、预定义对象属性和方法、标准库模块等。我们之前只谈到了皮毛而已,那么困扰初学者的问题来了:怎样找到这些内置工具呢?本节提供了一些python可用的文档资源。
表1. python文档资源
#注释、dir函数和help函数之前我们已讲过,不予赘述,这里我们来看看其他几项。
- __doc__是自动附加在对象上的文档,而且在运行时还可保存查看(类似#),从语法上来说,这类注释是写成字符串,放在模块文件、函数积累语句的顶端,在任何可执行代码前的。python会自动封装这个字符串。
- PyDoc:HTML报表:PyDoc提供的GUI接口可以将报表通过HTML网页格式来呈现。
- 标准手册集:ActivePython27.chm(不同版本有不同名称)
最后呢,还要给大家奉送上一些常见陷阱:
- 不要忘记冒号
- 从第一行开始写程序
- 空白行在交互模式提示符下非常significant
- 同一模块内缩进一致
- 不要尝试在Python中写C代码,比如if (x==1):其实语法上没关系,但是是多余的,另外语句后不要打分号
- 推荐使用简单的for循环,而不是while和range。因为for循环速度比较快,有时比等效的while快两倍
- python(2)-------The syntax of python
- The Zen of Python
- The Zen of Python
- The Zen of Python
- The Zen of Python
- The Zen of Python
- The Zen of Python
- The Zen of Python
- The Zen of Python
- The Zen of Python
- The Zen of Python
- The Zen of Python
- The Zen of Python
- The Zen of Python
- The Zen of Python
- The Zen of Python
- The Zen of Python
- The Zen of Python
- 从二叉排序树------浅谈C指针
- source insight 3.5 函数名出现乱码的解决办法
- 省市查询实现
- RAC 10201 (error while loading shared libraries: libpthread.so.0)解决
- Windows环境下编译liblas库以读写Lidar文件
- python(2)-------The syntax of python
- dell装机bios问题 os install mode enable failed to detect one or more drives during post
- input事件的处理
- apue.h及编程环境配置
- python(3)------The object type of python
- InlineHook 通用
- 我排第几
- centos升级firefox
- MySql数据库基本操作方法【增】【删】【显示】【进入】