python流程控制语句

if 判断语句

if boolean_expression1:
    suitel
elif boolean_expression1:
    suite2
....
else:
    else_suite

elif 语句是可选的


三元表达式

通常在为某变量设置默认值时通常用到如下格式:

if X:
     A = y
else:
    A = z

可以改写为三元表达式模式:

A = y if X else z

while循坏

  • 用于编写通用迭代结构
  • 顶端测试为真即会执行循坏体,并会重复多次测试直到为假后执行循坏后的其他语句

语法格式

while boolean_expression
    while_suite
else:
    else_suite

else 分支为可选部分

只要boolean_expression结果为True循坏就执行

boolean_expression结果为Flse时终止循坏,此时有else分支,则执行else.

补充:

break:跳出最内层循坏

continue :跳出本层循坏的本次循坏,执行本层循坏下次循坏。

pass:占位语句

例子: 逐一显示指定类型中的所有元素

l = [1,2,3,4]

In [2]: count = 0
In [6]: while count < len(l):
print(l[count])
count +=1
...:     
 1
 2
 3
 4

for循坏

语法格式

for expression1 in iterable:
    for_suite
else:
    else_suite

expression1通常是一个单独的变量或者是一个变量序列。 如果以元组或者列表用于expression1,则其中的每个数据项都会拆分到表达式的项,如下:

T = [(1,2),(3,4)]
for (a,b) in T:
    print(a,b)

备注:对于可迭代对象,使用for循坏是最合理的


python的迭代

迭代:重复做一件事

iterable(可迭代对象)

对象包含_iter_()或者_getitem_()就是一个可迭代对象。

迭代器:对可迭代对象使用_iter_方法就可以得到一个迭代器,要使迭代器指向下一个元素可以使用next(),迭代器是一次性,迭代不可逆,一旦没有元素迭代了就会抛出stopIteration异常

下面是一个迭代器例子:

In [7]: l1 = [1,2,3]

In [8]: l2 = iter(l1)

In [9]: print(l2)
<list_iterator object at 0x7fb24f6ee860>

In [10]: next(l2)
Out[10]: 1

In [11]: next(l2)
Out[11]: 2

In [12]: next(l2)
Out[12]: 3

In [13]: next(l2)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-13-e421ec37cfe2> in <module>()
----> 1 next(l2)

StopIteration:

python列表解析

列表解析是python迭代机制的一种应用,它常用于实现创建新的列表,因此要放置于[]中

语法:

[expression for iter_var in iterable]

[expression for iter_var in iterable if cond_expr]

例子说明:

In [14]: l3 = [i**2 for i in range(10) ]

In [15]: print(l3)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In [16]: l3 = [i**2 for i in range(10) if i >5 ]

In [17]: print(l3)
[36, 49, 64, 81]

例子:
文件a.txt内容
# apple 10 3
# tesla 100000 1
# mac 3000 2
# lenovo 30000 3
# chicken 10 3
#
# 2.
要求使用列表解析,从文件a.txt中取出每一行,做成下述格式
# [{‘name’:'apple','price':10,'count':3},{...},{...},...]
file_path = 'a.txt'
encoding = 'utf-8'
with open(file_path,encoding=encoding) as f:
l1 = f.readlines()

print([dict(zip(['name', 'price', 'count'], i)) for i in [i.split() for i in l1]])




生成器表达式

生成器表达式并不是真正创建数字列表,而是返回一个生成器对象,次对象每次计算出一个条目后,把这个条目yield出来。

序列过长,并且每次只需要获取一个元素时,应当考虑使用生成器表达式而不是列表解析

语法:

(expression for iter_var in iterable)

(expression for iter_var in iterable if cond_expr)

例子说明:

In [18]: l3 = (i**2 for i in range(10) if i >5 )

In [19]: print(l3)
<generator object <genexpr> at 0x7fb24f66f258>

In [20]: next(l3)
Out[20]: 36

In [21]: next(l3)
Out[21]: 49

In [22]: next(l3)
Out[22]: 64

In [23]: next(l3)
Out[23]: 81

In [24]: next(l3)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-24-7c7f71d1f0ed> in <module>()
----> 1 next(l3)

StopIteration:
原文地址:https://www.cnblogs.com/xiangryan/p/7050179.html