home PYTHONJAVA
 

Python3 Conditional Control

Python conditional statements are blocks of code that are executed by the execution result of one or more statements (True or False).

You can use the following figure to briefly understand the execution of conditional statements:

if statement

The general form of an if statement in Python is as follows:

if condition_1: statement_block_1 elif condition_2: statement_block_2 else: statement_block_3
  • If "condition_1" is True, the "statement_block_1" block statement will be executed
  • If "condition_1" is False, it will determine "condition_2"
  • if "condition_2" "Statement_block_2" block statement will be executed for True
  • If "condition_2" is False, the "statement_block_3" block statement will be executed

In Python, elif is used instead of else if, so the if statement keyword is: if – elif – else.

Note:

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

Instance

The following is a simple if instance:

Instance

#!/usr/bin/python3 var1 = 100 if var1: print ("1 - if The expression condition is true") print (var1) var2 = 0 if var2: print ("2 - if The expression condition is true") print (var2) print ("Good bye!")

Execute the above code, the output is:

1 - if The expression condition is  true
100
Good bye!

From the result, you can see that the variable var2 is 0, so the corresponding statement in if is not executed.

The following example demonstrates the age calculation of a dog:

Instance

#!/usr/bin/python3 age = int(input("Please enter the age of your dog: ")) print("") if age < 0: print("Are you teasing me?!") elif age == 1: print("Equivalent 14 Old man ") elif age == 2: print("Equivalent to 22 years old.") elif age > 2: human = 22 + (age -2)*5 print("Corresponding to human age: ", human) ### Exit prompt input("Click the enter button 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 

equivalent 14 The old man. 
Click enter Exit

The following are the commonly used operation operators in if:

operator Description
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to whether the comparison objects are equal
!= not equal to

Instance

#!/usr/bin/python3 # The program demonstrates the == operator # Using numbers print(5 == 6) # Using variables x = 5 y = 8 print(x == y)

The above example output results:

False
False
The

high_low.py file demonstrates the comparison of numbers:

Instance

#!/usr/bin/python3 # This example demonstrates a digital guessing game number= 7< Span class="hl-code"> guess = -1 print("< Span class="hl-string">Digital guessing game!") while guess != number: guess = int< Span class="hl-brackets">(input("Please enter the number you guessed:")) if guess == number: print("< Span class="hl-string">Congratulations, you guessed it!") elif guess < number: print("< Span class="hl-string">The number guessed is small...") elif guess > number: print("< Span class="hl-string">The number of guesses is big...")

Executing the above script, the example output is as follows:

$ python3 high_low.py
Digital guessing game!
Please enter the number you guessed:1
The number guessed is small...
Please enter the number you guessed:9
The number of guesses is big...
Please enter the number you guessed:7
Congratulations, you guessed it! 

if Nesting

In a nested if statement, you can put the if...elif...else structure in another if...elif...else structure.

if expression1:
Statement
if expression2:
Statement
elif expression3:
Statement
else:
Statement
elif expression4:
Statement
else:
Statement

Instance

# !/usr/bin/python3 num=int(input("Enter a number:")) if num%2==0: if num%3==0: print ("The number you enter can be divisible 2 with 3") else: print ("The number you enter can be divisible2,But can't be divisible 3") else: if num%3==0: print ("The number you enter can be divisible 3,But can't be divisible 2") else: print ("The number you entered cannot be divisible 2 with 3")

Save the above program to the test_if.py file. After execution, the output will be:

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





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.