Python Process Control Statement-IF statement

Source: Internet
Author: User

An If statement

ifThe statement is used to test a condition, and if the condition is true, we run a block of statements (called if-blocks ), that is, there is only one if judgment, and when it is true, run a statement, otherwise it will not run

The IF statement in the following code controls the indentation of print, the print statement without indentation is not controlled by the IF statement, as long as it is under the IF statement and indented, then the statement is controlled by the IF statement

#!/usr/bin/python#Filename:if.py Number= 123Guess= Int (Raw_input ("Enter an integer:"))ifGuess = =Number :Print 'Congratulations, you guessed it.'    Print "But don't win any prizes!"Print ' Done'

Run the code to test, when we enter 123, the IF statement is true, will run the indentation of the two print statement, if the IF statement is false, then the IF statement below the two indentation of the print statement, will continue to execute the following non-indented code

If-else statements

ifThe statement is used to test a condition, and if the condition is true, we run a block of statements (called if-blocks ), or else we process another statement (called the else-block ). The else clause is optional.

This means that when the IF statement is formed, the code below the IF statement is executed, and if it does not, then execute the code below the else, If-else cannot be mixed with other non-indented code (because the code without indentation is not controlled by if or else)

#!/usr/bin/python#Filename:if.py Number= 123Guess= Int (Raw_input ("Enter an integer:"))ifGuess = =Number :Print 'Congratulations, you guessed it.'    Print "But don't win any prizes!"Else:    Print 'No, it's a little lower than that'Print ' Done'

If-elif-else statements

The statement in this format indicates that when the if is not established, it is judged whether the ELIF statement is established (there can be many elif statements) if many ELIF statements continue to be judged, until they are established, and if they are not, then the execution of the Else statement

# !/usr/bin/python # Filename:if.py  = 123
Guess= Int (Raw_input ("Enter an integer:"))ifGuess = =Number :Print 'Congratulations, you guessed it.'    Print '(but don't win any prizes!)'elifGuess <Number :Print 'No, it's a little higher than that'Else:    Print 'No, it's a little lower than that'Print ' Done'

Run the program to test, the results are as follows: If the word is set to execute if the following statement, if not set up to determine whether the Elif statement is set, if set to execute the following statement, if not set to execute else

Note that we use the indentation level to tell Python which block each statement belongs to. That's why indenting is so important in python. I want you to stick to the "one tab per indent" rule.

Note that if the statement contains a colon at the end--we tell Python to follow a block of statements following it.

elifand else subordinate clauses must have a colon at the end of the logical line, followed by a corresponding block of statements (including, of course, the correct indentation).

You can also use another statement in an if block if , and so on-this is called a nested if statement.

#!/usr/bin/python#Filename:if.py Number= 123Guess= Int (Raw_input ("Enter an integer:"))ifGuess >Number :ifGuess = = 322:        Print 'Congratulations, you guessed it.'        Print '(but don't win any prizes!)'Print ' Done'

If nesting indicates that the first layer of judgment needs to be established to continue to judge whether the second if the judgment is set up, if there are many layers, then continue to judge, know no so far, as long as a certain layer of judgment is not established, then the subsequent judgment and the following controlled statements no longer execute

Python Process Control Statement-IF statement

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.