Python Control flow statements

Source: Internet
Author: User

One, the print statement

1.1 Basic output

1.2 Print comma

1.2 Output to file >> redirect

>>> a=2>>> Print a,2 #, means no line break 2 2
#!/usr/bin/python2.6 #coding =utf-8print "2", print "3", print "4" f = open (' Print.txt ', ' W ') print >>f, "Hello", print >>f, "World" f.close


Ii. Control Flow Statements

2.1 consists of the condition and execution code block.

2.1.1 Conditions can be divided into decisions, loops, and branches

2.2 Format (colon and 4 empty Geyong, try to use 4 spaces instead of tabs)

2.3 If and for functions are contorl flow

#!/usr/bin/python2.6 #coding =utf-8 ': Delimited condition and code block indent 4 Spaces ' if True:print 4


3. Boolean value

3.1 Control flow is closely related to true and false values

3.1.1 Don't misunderstand the true and the false with the Boolean value


#!/usr/bin/python2.6 #coding =utf-8 "" "is recommended to use BOOL () to determine the true or false, or omit not to write" "" x = 3if x: #等价于if x = = if bool (x) Print 4

3.2 Some of the most basic operators of Boolean values

3.2.1 and

3.2.2 or

3.2.3 is checks the share, determines whether the same object is referenced, and the value is the same

3.2.4 = = Check value

>>> 1 = = True true >>> 1== ' 1 ' False


3.2.5 not

3.2.6 several other comparison symbols

If True:print "true" Else:print "not true"

If True:print "true" Elif not True:print "not True" Else:pass

Third, if statement (Control flow statement)

4.1 If and if else elif pass

4.1.1 if and elif instead of switch

4.1.2 Pass


4.2 Three-dimensional expression

4.2.1 X if Else

4.2.2 Utilization List

4.2.33-dollar expression play just fine, because Python advocates simplicity

>>> 4 If True else 3 4if true:print 4else:print 3


>>> [4,3][true] #[false answer, true answer [condition]3


Four. While statement

1. The basic format of the while

While expression: #控制流的条件表达式 (expression) result, must be true

Statement (s)


2. The basic components of the while

2.1 Break ends while, if there is else, does not execute

2.2 Continue jumps out of the current loop, does not execute the code behind continue, but does not end while

2.3 Else normally ends while executing, but if there is a break inside the while, the else is not executed



3 Note: In general applications, the while must give an end condition, otherwise it is the legendary death cycle

#coding =utf-8x = 1while true:x +=1 print x if x > 20:breakelse:print ' End '


V. FOR statement

1, for the basic format

For item in Iterable:

Statement (s)


2. Basic components of For

3.2.1 Break

3.2.2 Continue

3.2.3 else, normal end for later execution, but if there is a break in for, the else is not executed


3. Note: The last iteration value of the for is preserved

#-*-Coding:utf-8-*-for x in "I am Li Lei". Split ("): Print Xelse:print" for end "#for endprint x #leifor x in" You is Haimmeimei ". Split ("): Print Xelse:print "for end" #for EndPrint x #haimmeimei


4. Further discussion of Boolean values


4.1 Lazy evaluation, short circuit logic, and then evaluated when needed.

4.2 From left to right, binate to back.

4.3 Take advantage of small tricks. Default value of OR, the preceding value does not exist, it can be assigned to a default value

False or DefaultValue


This article from "Small Fish Blog" blog, declined reprint!

Python Control flow statements

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.