4.reading Material_if.docx

Full Transcript

**Understanding Conditional Statements in Python** **Video tutorial :** [**https://www.youtube.com/watch?v=FvMPfrgGeKs**](https://www.youtube.com/watch?v=FvMPfrgGeKs) [**https://www.w3schools.com/python/python\_conditions.asp**](https://www.w3schools.com/python/python_conditions.asp) [**https://...

**Understanding Conditional Statements in Python** **Video tutorial :** [**https://www.youtube.com/watch?v=FvMPfrgGeKs**](https://www.youtube.com/watch?v=FvMPfrgGeKs) [**https://www.w3schools.com/python/python\_conditions.asp**](https://www.w3schools.com/python/python_conditions.asp) [**https://www.programiz.com/python-programming/if-elif-else**](https://www.programiz.com/python-programming/if-elif-else) [**https://www.geeksforgeeks.org/python-if-else/**](https://www.geeksforgeeks.org/python-if-else/) Conditional statements are a fundamental concept in programming. They allow the execution of certain blocks of code based on whether a condition is true or false. In Python, the primary conditional statements are if, elif, and else. **if Statement** The if statement is used to test a specific condition. If the condition evaluates to True, the code block within the if statement is executed. Syntax: if condition: \# code to execute if condition is true Example: x = 10 if x \> 5: print(\"x is greater than 5\") In this example, since x is 10, which is greater than 5, the message \"x is greater than 5\" will be printed. **else Statement** The else statement can be used following an if statement. It defines a block of code to be executed if the condition in the if statement is False. Syntax: if condition: \# code to execute if condition is true else: \# code to execute if condition is false Example: x = 2 if x \> 5: print(\"x is greater than 5\") else: print(\"x is not greater than 5\") In this example, since x is 2, which is not greater than 5, the message \"x is not greater than 5\" will be printed. **elif Statement** The elif statement (short for \"else if\") can be used to check multiple conditions. It must follow an if statement and precede an else statement. Syntax: if condition1: \# code to execute if condition1 is true elif condition2: \# code to execute if condition2 is true else: \# code to execute if neither condition1 nor condition2 is true Example: x = 7 if x \> 10: print(\"x is greater than 10\") elif x \> 5: print(\"x is greater than 5 but less than or equal to 10\") else: print(\"x is 5 or less\") In this example, since x is 7, the message \"x is greater than 5 but less than or equal to 10\" will be printed. **Nested Conditional Statements** You can also nest conditional statements within each other to test multiple conditions. Example: x = 12 if x \> 10: if x % 2 == 0: print(\"x is greater than 10 and is even\") else: print(\"x is greater than 10 and is odd\") else: print(\"x is 10 or less\") In this example, since x is 12, the message \"x is greater than 10 and is even\" will be printed because 12 is both greater than 10 and an even number.

Use Quizgecko on...
Browser
Browser