[关闭]
@Channelchan 2017-11-27T05:54:34.000000Z 字数 2278 阅读 137624

Python 流程控制

分支语句

if、elif、else

image.png-19.1kB

Python分支(条件)语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。

Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false。

if 语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。

Python 编程中 if 语句用于控制程序的执行,基本形式为:

  1. if 判断条件:
  2. 执行语句……
  3. else
  4. 执行语句……
  1. # if-else实例
  2. Stocks=['APPL','MSFT','GOOG']
  3. if 'APPL' in Stocks:
  4. print ('Stocks has APPL')
  5. else:
  6. print ('Stocks has not APPL')
Stocks has APPL
  1. # 当判断条件为多个值时,使用以下形式:
  2. Stocks=['APPL','MSFT','GOOG']
  3. if 'YHOO' in Stocks:
  4. print ('Stocks have YHOO')
  5. elif 'IBKR' in Stocks:
  6. print('Stocks have IBKR')
  7. elif 'SBUX' in Stocks:
  8. print ('Stocks have SBUX')
  9. else:
  10. print ('Stocks don`t have YHOO,IBKR,SBUX')
Stocks don`t have YHOO,IBKR,SBUX

循环语句

while

image.png-61.6kB

Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:

while 判断条件:

执行语句……
  1. # while实例
  2. count = 0
  3. while (count < 6):
  4. print ('The count is:', count)
  5. count+=1
  6. print ("Over")
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
Over

for

image.png-103.4kB

Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。

  1. # for迭代列表实例
  2. count=[0,1,2,3,4,5]
  3. for c in count:
  4. print ('The count is:',c)
  5. print('Over')
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
Over
  1. # for迭代字典实例
  2. d={'AAPL':120,'MSFT':62.5,'GOOG':800}
  3. for k in d:
  4. print (k,':',d[k])
AAPL : 120
MSFT : 62.5
GOOG : 800

循环控制语句(break、 continue、 pass)

break

image.png-65kB

break语句用来终止循环语句,即循环条件没有False条件或者序列还没被完全递归完,也会停止执行循环语句。

  1. # break 实例
  2. for word in 'QuantitativeTrader':
  3. if word == 'i':
  4. break
  5. print ('Current word :', word)
Current word : Q
Current word : u
Current word : a
Current word : n
Current word : t

continue

image.png-69.8kB

continue 语句用来告诉Python跳过当前循环的剩余语句,然后继续进行下一轮循环。

  1. # continue实例
  2. for word in 'Quantitation': # First Example
  3. if word == 'i':
  4. continue
  5. print('Current word :', word)
Current word : Q
Current word : u
Current word : a
Current word : n
Current word : t
Current word : t
Current word : a
Current word : t
Current word : o
Current word : n

pass

Python pass是空语句,是为了保持程序结构的完整性。

  1. # pass实例
  2. for word in 'Quantitation':
  3. if word == 'i':
  4. pass
  5. print ('执行了pass')
  6. print('Current word :', word)
Current word : Q
Current word : u
Current word : a
Current word : n
Current word : t
执行了pass
Current word : i
Current word : t
Current word : a
Current word : t
执行了pass
Current word : i
Current word : o
Current word : n

列表解析

列表解析根据已有列表,高效创建新列表的方式。

  1. list_count = []
  2. for x in range(1,5,1):
  3. list_count.append(x**2)
  4. print (list_count)
[1, 4, 9, 16]
  1. # 一行代码完成新列表生成
  2. list_comprehension = [x**2 for x in range(1,5,1)]
  1. list_comprehension
[1, 4, 9, 16]
  1. key = ['a','b','c','d']
  1. key_value = list(zip(key, list_comprehension))
  1. # 一行代码完成新字典生成
  2. dict_comprehension = {k:v for k,v in key_value}
  1. dict_comprehension
{'a': 1, 'b': 4, 'c': 9, 'd': 16}
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注