Python Log Python control flow (sequential, branching, looping)

Source: Internet
Author: User
Tags ming terminates

Python Control Flow:

All programs are running in idle

Three types of control flow in 1.Python

(1). What is control flow

Python program code execution is orderly, and some code programs will fall from the top in order to execute, some programs will jump to execute, some program code will choose different branches to execute, and some program code will be executed in a loop. So what kind of program will be executed from top to bottom, which will choose Branch execution and what can be done in loop? In Python there is a corresponding control statement to identify, control statements he can control the execution of certain sections of code, we call these different functions of control statements Control flow


(2). What control flows are in Python

#实例:

#控制流功能

#要实现: Repeat the same procedure for 3 paragraphs

#方式一:

I=0print ii=i+1print ii=0print ii=i+1print ii=0print ii=i+1print I

Run from top to bottom

#方式二:

for k in range (0,3): #重复语句 i=0 Print i i=i+1 print I

#输出结果:

010101


#再比如, to achieve: If Xiao Ming ate, output xiaoming is good, if Xiao Ming did not eat, output Xiao Ming's bad function.

#平常情况按顺序执行的话, it is not possible to implement such a function, we can use the branch structure in the control flow

Xiaoming= "Eat" if xiaoming== "eat": Print "Xiao Ming is very good" else:print "Xiao Ming is Bad"

#输出结果:

Xiaoming is a good boy.


#控制流的类型

#控制流的类型有三种, one is the sequential structure, one is the branch structure, the other is the cyclic structure

#顺序结构:

A=7print Aa=a-1print Aa=a+6print A

#输出结果:

7612

#分支结构:

A=7if a==8:print "She" Else:print "He"

#输出结果:

He

#循环结构:

A=7 #不为0则为真while A: #当a为真时一直执行 print "Helloo" a=a-1 #a = 0 o'clock Stop loop

#输出结果:

Helloohelloohelloohelloohelloohelloohelloo


2. Understanding the branching structure-if


(1). Recognize the IF statement

The IF statement in Python is used to determine the execution of that block of statements, and below we recognize the IF statement by instance

#实例:

#if语句

#if语句的格式用法:

If this is the case:

Execute this part of the statement

Elif or something like this:

Execute ELIF Partial statement

else or none of the above:

Execute this part of the statement


(2). If statements use an instance:

#一种情况的if用法

A=8if a==8: #这里的等号注意是 = = print "Hello"

#输出结果:

Hello
A=8if a!=8:print "hehe"

#输出结果:

>>>>>>

Do not execute

#两种选择情况下的if用法

A=8if a==8:print "She" Else:print "He"

#输出结果:

She


#三种选择情况下的if用法

A=5if a==7:print "I" Elif a>7:print "he" elif a<6:print "Hello" else:print "she"

#输出结果:

Hello

#if语句使用要点

Important: The branches try not to repeat and try to include all possibilities

‘‘‘

For example, we have to be divided into good grades, such as the conditions for such division is more reasonable 0<= scores <80 for poor, 80<= results <90 for the better,

90<= scores <=100 for excellent.

And this division of conditions is unreasonable:0< results <80 for poor, 80<= results <90 for good, 90<= results <100. For example, if a person's score is 0 points, 100 points, 80 points, 90 points of this critical condition, there is no way to judge the execution of which part of the statement.

‘‘‘

The tipping point is not repeated

A=80if 80<a<=100:print "Good" elif 0<a<=80:print "Poor"


#输出结果:

Poor


3. Understanding the Loop structure while

(1). What is a while statement

The while statement in Python is also used to control the repeated execution of a statement.


#while语句使用结构

‘‘‘

The while condition is true:

Loop through "This part of the statement" until the condition does not meet the true

Else

If the condition is false, execute the partial statement

#else部分可以省略

‘‘‘

(2) Use of. While statements

#两个简单的while语句实例

#第一个是最简单没有else部分的

A=truewhile a:print "ABC"

#输出结果:

An infinite number of ABC


#第二个是有else部分的

B=falsewhile b:print "ABC" #循环部分, for true has been circulating Else:print "DEF" #else部分不循环, run once terminated

#输出结果:

Def

#我们再来看一下比较复杂一点的有嵌套的while语句

A=1while a<10:if a<=5: #循环部分, select Print a Else:print "Hello" a=a+1 #循环部分 via the IF branch Select Else:print "Test"

#输出结果:

12345hellohellohellohellotest


Summary: You can use the indentation position to determine the consistency of the front and back, as shown above the two else position, the corresponding structure is different


4. Understanding the For statement of the loop structure

(1). What is a For statement

Another form of looping statement that can be used to traverse an object and also has an optional else block included, primarily for handling break statements contained in a for statement. If the For loop is not terminated by a break, the statement in the else block is executed.

Break terminates the For loop when needed, and continue skips the statement behind it, starting the next round.


The format for the For statement is as follows:

>>>for <i> in < objects collection;:

If < conditions;:

(break)

If < conditions;:

(Continue)

< other statements >

Else

<>


(2) Use of. For statement

#for语句

‘‘‘

For statement format:

For I in collection:

Perform this section

Else

Perform this section

‘‘‘

#实例:

#第一个for语句

For i in [1,2,8,9,0]: Print I

#输出结果:

12890

#学会使用range函数, the second for statement, generates a sequence of collections, from beginning to end

For I in Range (1,7): #range的集合是从1到n-1, excluding the end print "Hello"

#输出结果:

6 Hello instead of 7

For I in Range (1,10,3): # (1,10,3) from 1 to 10,3 for step, is the size of the number, the default is 1 print i #就是取1, 4, 7, not the end

#输出结果:

147


#最后看一个带嵌套的for语句

For I in Range (1,10): #循环9次 if i%2==0:print i print "even" else:print i print "odd"

#输出结果:

1 Odd 2 even 3 odd 4 even 5 odd 6 even 7 odd 8 even 9 odd


5.Break statements

Function: Break the execution of the program, the usual term of the loop structure, when the loop structure breaks should be forced to stop the abort cycle, and then exit the loop

(1). Use of the break statement

#break语句用法

The break statement is meant to stop the loop execution, and the break statement is used in the loop statement where the break occurs and the loop execution is stopped directly.


#break语句用在while循环中

A=1while A:print a a=a+1 #循环体 if A==10:break #没有break的话就会陷入死循环

#输出结果:

From 1 to 9, break when a=9


#break语句在for循环中

For I in Range (5,9): Print I if i>6:break# when the break condition is reached, the current condition is still executed once and then stopped

#输出结果:

5,6


#break语句在双层循环语句中

A=10while a<=12: #外循环 a=a+1 for I in Range (1,7): Print i #内循环 if i==5: Break

#输出结果:

Inner loop is repeated three times by outer loop: a=10,a=11,a=12

Repeat three times i:1,2,3,4,5

A=10while a<=12:a=a+1 for I in Range (1,7): Print i if I==5:break if a==11: #a =11 stopped. Break

#输出结果:

12345


6.continue statements

(1). What is a continue statement

Forced to stop this execution of this cycle, jump directly to the next time, that is, stop this time, perform the next

Unlike all loops after a break is stopped directly


(2). How to use the Continue statement

#continue语句的使用

‘‘‘

The continue statement is a statement that is placed in a loop statement to end the loop.

‘‘‘

#continue语句在while循环中

#首先我们得知道循环是分很多次的, and the continue statement terminates the loop instead of terminating it.

A=1while a<7:a=a+1 if a==3:continue print a

#输出结果:

2456

#continue语句在for循环中, and compare the following two program A and program B

#程序a

For I in Range (1,7): If i==3:continue print I

#输出结果:

12456


#程序b

For I in Range (1,7): Print I if i==3:continue

#输出结果:

123456

#continue语句在双层循环语句中

A=1while a<7:a=a+1 if a==4: #a =1,a=2,a=3,a=5,a=6 continue for I in range (7,10): if i==9: Continue print I

#输出结果:

Output 5 times 7,8


(3). The difference between a break statement and a continue statement

#continue语句与break语句的区别

‘‘‘

The continue statement refers to the end of the execution of the remaining statements in this loop, and then the next round of loops.

The break statement refers to the direct end of the loop, including the end of all the secondary loops that are left to execute the loop.

‘‘‘

#区分程序c和程序d


#程序c

For I in Range (10,19): If i==15:continue print I

#输出结果:

10,11,12,13,14,16,17,18


#程序d

For I in Range (10,19): If i==15:break print I

#输出结果:

10,11,12,13,14,15


This article is from the "8626774" blog, please be sure to keep this source http://8636774.blog.51cto.com/8626774/1676357

Python Log Python control flow (sequential, branching, looping)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.