python-Control Flow

Source: Internet
Author: User

If

#!/usr/bin/python Number=23Guess=int (Input ('Enter an Interger:'))#input Returns the result of a string type, which needs to be converted to the int type with int ()ifGuess = =Number :Print('You guessed in.')elifGuess <Number :Print('No, it's a little higher than that')Else:    Print('No, it's a little lower than that')Print(' Done')

While

#!/usr/bin/python Number= 23 whiletrue:guess=int (Input ('Enter an integer:'))    ifguess==Number :Print('You guessed it')         Break    elifguess<Num:Print('No, it's a little higher than that')    Else:        Print('No, it's a little lower than that')Else:#else the statements is executed when the condition of the while is false, but if there is a break, it will not execute the    Print('The while loop are over')Print(' Done')

For

# !/usr/bin/python  for  in range (1,5):    print(i)else:# Else the statements is executed at the end of the for loop, but if there is a break it will not execute      print('the Forloop is-over')

Range has three parameters, and the third parameter is 1 by default, which indicates the step size

Range returns a list that is opened before closing, such as:

Range (1,5) returns [1,2,3,4]

Range (1,5,2) returns [1,3]

The use of break and continue is the same as in C + +

python-Control Flow

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.