Python3 condition Control (ix)

Source: Internet
Author: User

A python conditional statement determines the code block that executes by executing the result of one or more statements (true or false).

You can easily understand the execution of conditional statements by:

If statement

The general form of the IF statement in Python is as follows:

if condition_1:    statement_block_1elif  condition_2:    statement_block_2Else  :    statement_block_3

Description

    • If "condition_1" is True, the "statement_block_1" block statement is executed
    • If "condition_1" is false, the "condition_2" will be judged
    • If "Condition_2" is True, the "statement_block_2" block statement is executed
    • If "Condition_2" is false, the "STATEMENT_BLOCK_3" block statement is executed

Python replaces the else ifwith elif , so the keyword for the IF statement is:if–elif–else.

Attention:

    • 1. Use a colon (:) after each condition to indicate the next block of statements to be executed after the condition is met.
    • 2, using indentation to divide the statement block, the same indentation number of statements together to form a block of statements.
    • 3. There are no switch–case statements in Python.

A simple If instance:

1 #!/usr/bin/python32  3VAR1 = 1004 ifvar1:5    Print("1-if expression condition is true")6    Print(VAR1)7  8VAR2 =09 ifvar2:Ten    Print("2-if expression condition is true") One    Print(VAR2) A Print("Good bye!")

Execute the above code and the output is:

1 if expression condition is true 2 3 Good bye!

From the result you can see that the statement within the corresponding if is not executed because the variable var2 is 0.

The following are the operations operators commonly used in the IF:

Instance:

1 #!/usr/bin/python32  3 #This example demonstrates a digital guessing game4Number = 75Guess =-16 Print("digital guessing game!")7  whileGuess! =Number :8guess = Int (input ("Please enter the number you guessed:"))9  Ten     ifGuess = =Number : One         Print("Congratulations, you guessed it! ") A     elifGuess <Number : -         Print("guess the number is small ...") -     elifGuess >Number : the         Print("guess the numbers are big ...")

The result of the instance output is as follows:

1 digital guessing game! 2 Please enter the number you guessed: 13 Guess the number is small ... 4 Please enter the number you guessed: 95 Guess the numbers are big ... 6 Please enter the number you guessed: 77 Congratulations, you guessed it!
If nesting

In a nested if statement, the IF...ELIF...ELSE structure can be placed in another if...elif...else structure.

if expression 1:    statement if  expression 2:        statement     elif  expression 3:        statement      else:        statement elif  expression 4:    statement Else:    statement

Instance:

1 #!/usr/bin/python32  3Num=int (Input ("Enter a number:"))4 ifnum%2==0:5     ifnum%3==0:6         Print("The numbers you enter can be divisible by 2 and 3.")7     Else:8         Print("The number you enter can be divisible by 2, but not divisible by 3 .")9 Else:Ten     ifnum%3==0: One         Print("The number you enter can be divisible by 3, but not divisible by 2 .") A     Else: -         Print("the number you entered cannot be divisible by 2 and 3.")

The output is:

1 Enter a number: 62 The number you enter can be divisible by 2 and 3.

Python3 condition Control (ix)

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.