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