Python conditions, loops, termination

Source: Internet
Author: User

1. Indent

Python uses the TAB key to differentiate the logic of the Code, that is, indentation, which is equal to four spaces in one indent


2.if conditions

Format:

If judging condition:

EXECUTE statement ...

Elif:

Execute statement ...

else:

Execute statement ...


Example:

A = Ten

B = -

if a>b:

Print("a>b")

elif a<b:

Print("a<b")

Else :

Print("a=b")


3.while Cycle

Format:

While judging condition:

EXECUTE statement ...


Example:

A = 3

while a> 0 :

Print("1111")

a-= 1



4.for Cycle

Format:

For Iterating_var in sequence:

Statements (s)


Example:

list1= [] Span style= "Font-size:12px;font-family:simsun, Stsong;color:rgb (0,0,255); Background-color:rgb (255,255,255);" >1 Span style= "Font-size:12px;font-family:simsun, Stsong;color:rgb (0,0,255); Background-color:rgb (255,255,255);" >3 Span style= "Font-size:12px;font-family:simsun, Stsong;color:rgb (0,0,255); Background-color:rgb (255,255,255);" >4 Span style= "Font-size:12px;font-family:simsun, Stsong;color:rgb (0,0,255); Background-color:rgb (255,255,255);" >2 "

for a inch List1:

Print(a)


Common methods:

1) Enumerate: subscript and corresponding value

Example:

Dict1 =Dict(a=1,b=2,C=3,D=4)

for key1, value1 in Enumerate ( DICT1):

Print(key1,value1)


2) print (range (1,10)): Print all at once 1-10 (memory)

Example:

Print (range(1,ten))


3) Print (xrange (1,10)): Print 1-10 on call

Example:

forIinchxrange(1,Ten):

Print (i)


Example:

xrange ( 1 10

Print (i)

Print List (i)



5.break and Continue termination

1) Continue: Jumps to the beginning of the next cycle

Example:

forIinchxrange(1,6):

If i = = 3:

Continue

Print ("i=%d" % i)


2) Break: Jump out of the loop

Example:

forIinchxrange(1,6):

If i = = 3:

break

Print ("i=%d" % i)


Python conditions, loops, termination

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.