Python control structures study notes (d) Python program

  After learning the basic data types Python, we will begin contacting control structure Python program to learn how to use Python control structures to change the order of execution of the program in order to meet the diverse functional requirements. If some small partner before learned C language, such as the java programming language, this will feel very familiar, because between them is the same, following on with a control structure Python understand it! 

First, the basic structure of the program

The basic procedure consists of three structures: sequential structure, branched structure and cyclic structure. among them:

  1. The sequence structure is in accordance with the program order of a linear operating mode.

          

Sequence Structure  

  2. The branch structure is a result of condition determination program according to choose one of various operation performed in the forward path, in accordance with the completeness of the branch path, the branch structure and a branched structure comprising two single branch structure, combined to form a biantennary structure multibranched structure .

                                           

 Single branch structure biantennary structure                    

  3. The cyclic structure is one implementation program is repeatedly executed according to the conditions rearwardly determination result, depending on the cycling conditions includes a condition loop condition through the loop and the loop structure.

 

                                  

 

         Conditional loop through the loop                     

Second, the branch structure of the program  

1. Single branch structure: if statement

Python syntax in an if statement is as follows:

  if <condition>:

    <Statement block>

注:在Java中,if 后的语句块是用大括号进行区分的,而 Python 是使用缩进来区分语句级别,所以在编写 Python 代码时,要慎用缩进!

2.二分支结构:if-else 语句

Python中 if-else 语句用来形成二分支结构,语法格式如下:

  if <条件>:

    <语句块1>

  else:

    <语句块2>

  二分支结构的一种简洁的表达方式: <表达式1> if <条件> else <表达式2>

注:最后一个 else 语句是没有条件的。

3.多分支结构:if-elif-else语句

Python的 if-elif-else 描述多分支语句,语句格式如下:

  if <条件1>:

    <语句块1>

  elif <条件2>:

    <语句块2>

  else:

    <语句块3>

 

 

注:与Java不同,Python中没有 else if 语句,而是以 elif 语句来代替相应的功能

三、程序的循环结构

1.遍历循环:for语句

Python使用保留字 for 实现“遍历循环”,基本使用方法如下:

  for <循环变量> in <遍历结构>:

    <语句块>

遍历循环还有一种扩展模式,使用方法如下:

  for <循环变量> in <遍历结构>:

    <语句块1>

  else:

    <语句块2>

在这种循环中,当 for 循环正常执行后,程序会继续执行 else 语句中的内容,因此可以使用<语句块2>来判断循环执行情况。

同时 for 还可以实现遍历功能,遍历结构可以是字符串、列表或range()函数等,如:

  for s in ‘hello‘

    print(s)

  遍历输出”h e  l l  o“

2.无限循环:while 语句

Python通过保留字 while 实现无线循环,基本使用方法如下:

  while <条件>:

    <语句块> 

条件判断为 True 时,循环体重复执行语句块中语句;当条件为False时,循环终止,执行与while同级别缩进的后续语句

3.循环保留字:break 和 continue

Python循环结构有两个保留字:breakcontinue,它们用来辅助控制循环执行。其中

  break 用来跳出最内层 for 或 while 循环,脱离该循环后程序从循环代码后继续执行(跳出一层循环)。

  continue 用来结束当前当次循环,即跳出循环体中下面尚未执行的语句,但不跳出当前循环(跳出一次循环)。

  

以上就是Python程序的控制结构了,在实际运用中,合理地使用它们,可以实现多种功能.

Guess you like

Origin www.cnblogs.com/Y-xp/p/11585801.html