160 likes | 631 Views
Conditional statements are used to control the flow of the program.<br>Conditional Statement in Python perform different computations or actions depending on whether a specific Boolean constraint evaluates to true or false.<br>
E N D
Conditional statements are used to control the flow of the program. Conditional Statement in Python perform different computations or actions depending on whether a specific Boolean constraint evaluates to true or false. if, elif and else are the conditional statements in python.
An "if statement" is written by using the if keyword. Python if Statement is used for decision-making operations. It contains a body of code which runs only when the condition given in the if statement is true. If the condition is false, then the optional else statement runs which contains some code for the else condition. Syntax of if statement:if expression: statement 1 Else is completely an optional block. If statement
There are a few important items to remember about if statements: There are a few important items to remember about if statements: The colon (:) is important and essential. The header of the compound statement is isolated from the body. The line after the colon has to be indented. It is common in Python to use four indenting spaces. All rows indented after the colon will be executed whenever the BOOLEAN EXPRESSION is valid.
a = 15b = 20if b > a: print("b is greater than a")O/P:b is greater than a Examples of if statement
Syntax:if expression1: statement(s) if expression2: statement(s) elif expression3: statement(s) elif expression4: statement(s) else: statement(s) else: statement(s) Nested if statement
num = 15if num >= 0: if num == 0: print("Zero") else: print("Positive number") else: print("Negative number") O/P: Positive number Example
It is used for decision-making operations. It contains a body of code which runs only when the condition given in the if statement is true. If the condition is false, then the optional else statement runs which contains some code for the else condition. Syntax:if BOOLEAN EXPRESSION: STATEMENTS_1 else: STATEMENTS_2 if .......else Statement
Example: x=15y=70if x > y: print(“x is greater ")else: print(“y is greater”)O/P: y is greater
Theelif keyword is pythons way of saying "if the previous conditions were not true, then try this condition". elif is an abbreviation of else if. Again, exactly one branch will be executed. There is no limit of the number of elif statements but only a single (and optional) final else statement is allowed and it must be the last branch in the statement: Elif statement
Syntax: ifcondition 1:STATEMENTS_A elifconditon 2:STATEMENTS_B else: STATEMENTS_C Example:a = 33b = 33if b > a: print("b is greater than a")elif a == b: print("a and b are equal")
For more follow us on our social media platforms: Instagram : learnbay_datascience Facebook : learnbay LinkedIn : Learnbay Twitter : Learnbay1 Thanks for Watching!!!