[Python 3 Series] control flow

Source: Internet
Author: User

The real power of programming is not just running instructions, just like a list of tasks. Depending on the result of an expression evaluation, the program can decide what to do next. The control flow statement determines which Python statements are executed under what conditions.


Boolean value

Although there are countless possible values for integer, floating-point, and string data types, there are only two values for the Boolean data type: True and false. When entered as a Python code, the Boolean value True and false are not like strings, with no quotes on either side, and they always start with uppercase letters. A Boolean value can also be used in an expression and can be saved in a variable. If the casing is incorrect, or if you try to use True and false as the variable name, Python gives the error message.

>>> abc=true>>> abctrue>>> Falsetraceback (most recent): File ' <stdin> ', line 1, in <module>nameerror:name ' false ' are not defined>>> falsefalse


Comparison operators

Compares two values and evaluates to a Boolean value.

values of integral or floating-point types are never equal to strings.

= = is the difference between = = is used to determine consistency, and = is used to assign values.

▎ Common comparison operators:

= = equals

! = does not equal

< less than

> Greater than

<= less than or equal to

>= greater than or equal to

Test Example:

>>> 40==40true>>> 40!=42true>>> 40<=42true>>> 40>=42false>>> 40 >=40True>>> 40== ' false>>> ' tom ' = = ' Tom ' false>>> ' tom '! = ' Tom ' True


Boolean operator

The and and or operators always accept two Boolean values (or expressions), so they are considered "two-dollar" operators.

Evaluates to TRUE if two Boolean values are True,and operators, otherwise evaluates to False.

Whenever a Boolean value is true, the OR operator evaluates the expression to true. If all is false, the value is evaluated as false.

Unlike and and OR, the not operator only acts on a Boolean value (or expression). The NOT operator evaluates to the opposite Boolean value.

>>> (4<5) and (5<6) true>>> (4>5) or (5>6) false>>> (4<5) and (5>6) false> >> (4<5) and not (5>6) true>>> (4<5) and not (5>6) False


Control flow Statements

The beginning of a control flow statement is usually a "condition", followed by a block of code called a clause.

1) If...elif...else judgment

Only if the IF statement is false, subsequent clauses are executed.

There can be more than one ELIF clause in a control flow.

[[email protected] test]# cat if.pya=6if a<5:print (' a<5 ') elif a==5:print (' a=5 ') elif a==6:print (' a=6 ') Else:print (' a>6 ') [[email protected] test]# Python3 if.pya=6


2) while and for loops

Both while and for can be looped, but the for loop is relatively concise.

While example:

[[email protected] test]# cat while.pya=0while a<5:print (' Hello World ') A=a+1[[email protected] test]# python3 While.pyhello World. Hello World. Hello World. Hello World. Hello World.

For example:

[[email protected] test]# cat for.pyfor A in range (0,6): Print (' Hello world ') [Email protected] test]# Python3 For.pyhello World. Hello World. Hello World. Hello World. Hello World.


3) Break and continue interrupts

When the loop is executed, if execution encounters a break statement, it exits the loop immediately.

[[email protected] test]# cat break.pya=0while a<5:print (' Hello World ') a=a+1 if A==3:break[[email protected] test]# python3 Break.pyhello World. Hello World. Hello World.

When the loop is executed, if execution encounters a continue statement, it jumps back to the beginning of the loop and continues the loop.

[email protected] test]# cat continue.pya=0while a<5:if a==3:a=a+1 continue print (' Hello world ' ) A=a+1[[email protected] test]# python3 Continue.pyhello World. Hello World. Hello World. Hello World.


4) Range ()

The range () function can be passed multiple parameters. The upper limit is 3 parameters. The first is the value that starts with the variable, the second argument is the upper bound (not included), and the third argument is the "step".

>>> for I in Range (0,15,3): .... print (i) ... 036912


Terminating programs

You can use the built-in function to import a module using the import statement. You can use the import statement directly, or you can use the From import statement.

Using the From import statement, you do not need a module prefix when invoking a function in a module.

It is recommended to use the import statement because the full name makes the code more readable.

You can let the program terminate or exit by calling the Sys.exit () function. Because this function is in the SYS module, the SYS must be imported before it can be used.

[[email protected] test]# cat sys.pyimport sysa=1while true:print (' Type exit to exit. ')  Text=input () If text== ' exit ': Sys.exit () print (str (a) + ' > ' +text) a=a+1[[email protected] test]# Python3 Sys.pytype exit to Exit.hello1> Hellotype exit to exit.world2> Worldtype exit to Exit.exit


This article is from the "garbled Age" blog, please be sure to keep this source http://juispan.blog.51cto.com/943137/1943869

[Python 3 Series] control flow

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.