Control flow of the Python language (if...elif...else,while,for,break,continue)

Source: Internet
Author: User

1.if...elif...else ...

1Number = 232guess = Int (raw_input ('Enter an integer:'))3 4 ifGuess = =Number :5     Print('Congratulations, you guessed it.')#New block starts here6     Print("(but don't win any prizes!)")#New block ends here7 elifGuess <Number :8     Print('No, it's a little higher than that')#another block9 Else:Ten     Print('No, it's a little lower than that' ) One  A Print(' Done' ) - #This last statement are always executed, after the IF statement is executed

2.while

1Number = 232running =True3 4  whileRunning:5guess = Int (raw_input ('Enter an integer:'))6 7     ifGuess = =Number :8         Print 'Congratulations, you guessed it.'9running = False#This causes the and loop to stopTen     elifGuess <Number : One         Print 'No, it's a little higher than that' A     Else: -         Print 'No, it's a little lower than that' - Else: the     Print 'The while loop was over .' -     #Do anything else want -  - Print ' Done'

When the loop condition becomes false, the else block is executed.

You might want to ask if such an else block will always be executed and what else will be done. Don't worry, there's a solution in 4.break.

3.for

1  for  in range (1, 5):2     print  i3else:  4     print'the ForLoopwas over'

Range (n, m [, steplen]) = [n,n+steplen,n+steplen*2,..., n+ (Ceil ((m-n)/steplen)-1) *steplen]

That is, starting with N, the Steplen is the step length, which is a sequence of less than m (not including m). By default, Steplen=1.

4.break

Terminates the current loop statement.

Note If you terminate from a for or while loop, any corresponding else will not be executed.

5.continue

Skips the remaining statements in the current loop block, and then proceeds to the next round of loops.

Control flow of the Python language (if...elif...else,while,for,break,continue)

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.