python语言控制结构笔记_Python自学笔记-第三章控制结构与异常

1.控制结构

Python通过if语句实现了条件分支,通过while语句与for...in语句实现了循环。还有另外的条件表达式,类似Java或C语言中的三元操作符。

1.1.条件分支

Python条件分支语句的最通常的语法:

if boolean_expression1:

suite1

elif boolean_expression2:

suite2

...

else:

else_suite

如果需要考虑某个分支的使用,可以使用pass作为该分支的suite。

x=4

if x>3:

print('a')

else:

print('b')

a

某些情况下,可以将一条if...else语句缩减为单一的条件表达式,其语法可以如下:

expression1 if boolean_expression else expression2

如果boolean_expression为True,条件表达式的结果为expression1,否则为expression2

x=4 if 5>4 else 3

print('x =',x)

x = 4

1.2.循环

Python提供了while循环与for...in循环。

1.2.1.while循环

while循环完整的语法如下:

while boolean_expression:

while_suite

else:

else_suit

只要boolean_expression为True,while块的suite就会执行。如果boolean_expression为False或变为False,则循环会停止,此时,如果存在可选的else分支,就会执行。如果循环不能正常终止,就会跳过所有可选的else分支

x=0

while x<10:

x +=1

print('while_suite = ',x)

else:

print('else_suit =',x)

while_suite = 1

while_suite = 2

while_suite = 3

while_suite = 4

while_suite = 5

while_suite = 6

while_suite = 7

while_suite = 8

while_suite = 9

while_suite = 10

else_suit = 10

x=0

while x<10:

x +=1

print('while_suite = ',x)

if x>5:

break

else:

print('else_suit =',x)

while_suite = 1

while_suite = 2

while_suite = 3

while_suite = 4

while_suite = 5

while_suite = 6

1.2.2.for循环

for...in 循环的完整语法也包括else分支:

for expression in iterable:

for_suite

else:

else_suite

通常,expression是一个单独的变量,或者是一个变量序列(一般以元组形式出现)。

di = {'a':1,'b':2,'c':3}

for k,v in di.items():

print('k =',k,'v =',v)

k = a v = 1

k = b v = 2

k = c v = 3

1.3.复杂条件

boolean_expression1条件判断一般可以使用以下几种方式:

1.3.1.比较运算符

这里一般包括 < ,<=, ==, >=, >

x=4

y=5

if x

suite1

1.3.2.同一性运算符

包括is,not is

x = y = [1,2,3]

z=[1,2,3]

if x is y:

print('suite1')

elif x is z:

print('suite2')

else:

print('else_suite1')

suite1

1.3.3.成员资格运算符

主要是in,not in

y = [1,2,3,4,5,6]

x = 8

if x not in y:

print('suite1')

else:

print('else_suite1')

suite1

1.3.4.布尔运算符

主要包括 and ,or,not,这3个布尔运算符可以混合搭配多个条件判断。

x=5

y=6

z = k = [1,2,3]

if x < y and k is z:

print('suite1')

else:

print('else_suite1')

suite1

1.4.迭代工具

1.4.1.并行迭代

zip函数可以作用于任意多的序列,可以处理不等长的序列,知道最短的序列‘用完’就会停止

a = [1,2,3,4,5]

b = ['a','b','c','d']

print(zip(a,b))

for k,v in zip(a,b):

print('k =',k,'v =',v)

k = 1 v = a

k = 2 v = b

k = 3 v = c

k = 4 v = d

1.4.2.按索引迭代

string='abcdefg'

for index,strs in enumerate(string):

print('index =',index,'strs =',strs)

index = 0 strs = a

index = 1 strs = b

index = 2 strs = c

index = 3 strs = d

index = 4 strs = e

index = 5 strs = f

index = 6 strs = g

2.异常

Python通过产生异常来指明发生错误或异常条件。

2.1.捕获与产生异常

异常的捕获是使用try...except块实现的,其通常语法格式:

try:

try_suite

except exception_group1 as variable1:

except_suite1

...

except exception_groupN as variabledN:

except_suiteN

else:

else_suite

finally:

finally_suite

其中至少要包含一个except块,但else与finally块都是可选的。在try块的suite正常秩序完毕时,会执行else块的suite--如果发生异常,就不会执行。如果存在一个finally块,则最后总会执行。

每个except分支的异常组可以是一个单独的异常,也可以是包含在括号中的异常元组。对每个异常组,as variable部分是可选的。如果使用,该变量就会包含发生的异常,并可以在异常块的suite中进行存取。

d573c21bb9d475bf603df42d8aa18937.png

在使用多个except块时,必须对其进行排序,从最具针对性的异常到最普通的异常(异常体现中最顶层)的异常

如果直接写成except:,则表示不设置异常组。这种情况下,包括哪些继承至BaseException而非Exception的异常。

如果没有哪个except块匹配该异常,Python会沿着调用栈回溯,并寻找适当的异常处理程序。如果找不到合适的异常处理程序,程序将终止,并在控制台打印异常以及回溯信息。

如果没有异常产生,那么任意可选的else块都将执行。

2.2.产生异常

有两种产生异常的语法:

raise exception(args)

raise

第二种语法,也就是没有指定异常时,raise将重新产生当前活跃的异常--如果当前没有,就会产生一个TypeError

raise Exception

---------------------------------------------------------------------------

Exception Traceback (most recent call last)

in ----> 1 raise Exception

Exception:

raise Exception('test')

---------------------------------------------------------------------------

Exception Traceback (most recent call last)

in ----> 1 raise Exception('test')

Exception: test

try:

raise Exception('test')

except Exception:

print('Base')

Base

try:

raise Exception('test')

except Exception:

print('Base')

raise

Base

---------------------------------------------------------------------------

Exception Traceback (most recent call last)

in 1 try:

----> 2 raise Exception('test')

3 except Exception:

4 print('Base')

5 raise

Exception: test

2.3.自定义异常

自定义异常时自定义的数据类型(类)。基本语法如下:

class exceptionName(baseException):pass

基类应该为Exception类或继承自Exception的类。

class FoundExcetion(Exception):pass

x=1

if x>0:

raise FoundExcetion

---------------------------------------------------------------------------

FoundExcetion Traceback (most recent call last)

in 2 x=1

3 if x>0:

----> 4 raise FoundExcetion

FoundExcetion:

文章来源: www.oschina.net,作者:惊天动地的胖子,版权归原作者所有,如需转载,请联系作者。

原文链接:https://my.oschina.net/u/3275937/blog/3167433

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值