python013 Python3 Condition Control

Source: Internet
Author: User

Python3 condition Control
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
    • 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 if with 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.

Instance
The following is a simple if instance:

#!/usr/bin/python3var1 = 100if Var1:print ("1-if expression condition is true") print (var1) var2 = 0if Var2:print ("2-if expression condition is tr UE ") print (VAR2)

Print ("Good bye!")

Execute the above code and the output is:

1-if expression condition is True100good 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 example shows a dog's age calculation:
Instance

#!/usr/bin/python3age = Int (input ("Please enter your dog's Age:")) Print ("") if < 0:    print ("You are kidding me!") elif age = = 1:    print ("the equivalent of 14-year-old person.") ") elif age = = 2:    print (" equivalent to 22-year-old person. ") Elif Age > 2:    human = + (age-2)    print (" corresponding to human Ages: ", Human) # # # Exit Prompt input (" Click the Enter key to exit ")

Save the above script in the dog.py file and execute the script:

$ python3 dog.py Please enter the age of your dog: 1 is equivalent to 14 years old person. Click the ENTER key to exit

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

Instance

The #!/usr/bin/python3# program demonstrates the = = operator # using the digital print (5 = = 6) # using the variable x = 5y = 8print (x = = y)

The result of the above example output:

Falsefalse

The high_low.py file demonstrates the comparison operation of numbers:

Instance

#!/usr/bin/python3 # This example demonstrates a digital guessing game number = 7guess = -1print ("Digital guessing game!") While guess! = number:guess = Int (Input ("Enter the number you guessed:")) if guess = = Number:print ("Congratulations, you guessed it!") Elif Guess < Number:print ("Guess the numbers are small ...") Elif guess > Number:print ("Guess the numbers are big ...")

Execute the above script, and the result of the instance output is as follows:

$ python3 high_low.py digital guessing game! Please enter the number you guessed: 1 guess the number is small ... Please enter the number you guessed: 9 guess the number is big ... Please enter the number you guessed: 7 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

#!/usr/bin/python3num=int (Input ("Enter a number:")) if Num%2==0:if num%3==0:print ("The number you enter can be divisible by 2 and 3") Else:print ("you enter The number can be divisible by 2, but not divisible by 3 ") Else:if Num%3==0:print (" The number you enter can be divisible by 3, but not divisible by 2 ") Else:print (" The number you enter cannot be divisible by 2 and 3 ")

Save the above program to the test_if.py file, and the output will be as follows:

$ Python3 test.py Enter a number: 6 The number you enter can be divisible by 2 and 3.

List of Notes
The following instance x is 0-99 to take a number, Y is 0-199 to take a number, if x>y output x, if x equals y output x+y, otherwise output y.

#!/usr/bin/python3import RANDOMX = random.choice (range) y = Random.choice (range) if x > Y:print (' x: ', x) elif x = = Y:print (' x+y ', x + y) else:print (' Y: ', y)

"" "an extension to the above example" "

#!/usr/bin/python3 "" "for an extension of the above example" "" Print ("======= Welcome into the dog Age comparison System ========") while True:    try: Ages        = Int (Input (" Please enter the age of your Dog: ")        print (" ")" Old        = float "If ages        < 0:            print (" Are you kidding me? "). ")        elif age = = 1:            print (" equivalent to human 14 ") Break            elif Age        = = 2:            print (" equivalent to human 22 years ") Break        Else :            human = + (age-2)            print ("equivalent to Human:", human) break    except ValueError:        print ("Invalid input, Please enter a valid age ") # # #退出提示input (" Click the Enter key to exit ")

python013 Python3 Condition Control

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.