YouTip LogoYouTip

Python3 Conditional Statements

# Python3.x Python3 Conditional Statements Python conditional statements execute code blocks based on the result (True or False) of one or more statements. You can get a simple understanding of the execution process of conditional statements through the following diagram: !(#) Code execution process: !(https://static.jyshare.com/images/mix/python-if.webp) ### Conditional Keywords | Keyword / Function | Description | Example | | --- | --- | --- | | `if` | Conditional statement, executes the code block when the condition is True | `if x > 0:` | | `elif` | Multi-condition branch (else if) | `elif x == 0:` | | `else` | Executes when no conditions are met | `else:` | | `pass` | Empty statement, used as a placeholder to ensure syntax completeness | `if x > 0: pass` | | `match` | Structural pattern matching (Python 3.10+, similar to switch) | `match x: case 1: ...` | * * * ## 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, "statement_block_1" will be executed. * If "condition_1" is False, "condition_2" will be evaluated. * If "condition_2" is True, "statement_block_2" will be executed. * If "condition_2" is False, "statement_block_3" will be executed. In Python, **elif** is used instead of **else if**, so the keywords for if statements are: **if – elif – else**. **Note:** * 1. A colon `:` is required after each condition, indicating that the statement block to be executed when the condition is met follows. * 2. Indentation is used to define statement blocks. Statements with the same indentation level form a block. * 3. Python does not have a `switch...case` statement, but `match...case` was added in Python 3.10, which serves a similar function. See below for details. GIF Demo: !(#) ### Example Here is a simple if example: ## Example #!/usr/bin/python3 var1 = 100 if var1: print("1 - if expression condition is true")print(var1)var2 = 0 if var2: print("2 - if expression condition is true")print(var2)print("Good bye!") Executing the above code, the output is: 1 - if expression condition is true100Good bye! As can be seen from the result, since the variable `var2` is 0, the corresponding statement inside the if block was not executed. The following example demonstrates age calculation for a dog: ## Example #!/usr/bin/python3 age = int(input("Enter your dog's age: "))print("")if age2: human = 22 + (age -2)*5 print("Corresponding human age: ", human)### Exit prompt input("Press enter to exit") Save the above script in a file named `dog.py` and execute it: $ python3 dog.py Enter your dog's age: 1Equivalent to 14 years old for a human.Press enter to exit The following are common comparison operators used in if statements: | Operator | Description | | --- | --- | | `<` | Less than | | `` | Greater than | | `>=` | Greater than or equal to | | `==` | Equal to, compares if two values are equal | | `!=` | Not equal to | ## Example #!/usr/bin/python3# Program demonstrates the == operator# Using numbers print(5 == 6)# Using variables x = 5 y = 8 print(x == y) The output of the above example: FalseFalse The `high_low.py` file demonstrates
← Python3 LoopPython3 Step1 β†’