1.1 python程序结构
python语法由语句和表达式组成,表达式处理对象并嵌套在语句中。
序 |
描述 |
1 |
python程序由模块构成 |
2 |
模块包含语句 |
3 |
语句包含表达式 |
4 |
表达式建立并处理对象 |
1.1.1 python语句集
python语句与更大的程序单元:函数、类、模块、异常。
序 |
语句 |
描述 |
例子 |
1 |
赋值 |
=,创建引用值 |
s=’梯阅线条’ |
2 |
调用 |
(),执行函数、方法 |
len(‘梯阅线条’) |
3 |
打印调用 |
print(),打印对象 |
print(‘梯阅线条’) |
4 |
if/elif/else |
选择分支 |
if s:print(s) |
5 |
for/else |
序列迭代 |
for x in s:print(x) |
6 |
while/else |
while循环 |
while x>0:x– |
7 |
pass |
空占位符 |
if s:pass |
8 |
break |
退出循环 |
while True: if x<0:break |
9 |
continue |
进入下次循环 |
while True: if x<0:continue |
10 |
def |
定义函数和方法 |
def (a,b,c=1,*d):print(a+b+c+d[0]) |
11 |
return |
返回函数方法结果 |
def (a,b,c=1,*d):return a+b+c+d[0] |
12 |
yield |
生成器函数 |
def gen(n):for i in n:yield i*2 |
13 |
global |
命名空间 |
x=’old’ def f():global x,y;x=’new’ |
14 |
nonlocal |
命名空间 |
def outer(): x=’old’ def f(): nonlocal x;x=’new’ |
15 |
import |
导入模块 |
import sys |
16 |
from |
导入属性 |
from sys import stdin |
17 |
class |
定义类 |
class Subclass(Superclass): staticData=[] def f(self):pass |
18 |
try/except/finally |
异常处理 |
try: act() except: print(‘act error’) finally: print(‘always’) |
19 |
raise |
触发异常 |
raise EndSearch(location) |
20 |
assert |
断言 |
assert X>Y,’X is small’ |
21 |
with/as |
文件管理器 |
with open(filepath) as f:pass |
1.1.2 python语句书写
序 |
语句书写 |
描述 |
1 |
冒 (:) |
复合语句,冒 结尾下一行缩进 |
2 |
不用分 (;) |
一行语句结束自动终止,不需要分 |
3 |
代码块 |
相同缩进的语句属于同一代码块,缩进结束则代码块结束 |
4 |
跨行 |
用括 括起来,括 内允许跨行,比如()、[]、{},其实,三引 也可以跨行 |
版权声明?:
更多内容参考python学习笔记或软件测试开发目录。
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!