1 / 16

Python conditional statement

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>

Télécharger la présentation

Python conditional statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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

  3. 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.

  4. a = 15b = 20if b > a: print("b is greater than a")O/P:b is greater than a Examples of if statement

  5. 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

  6. num = 15if num >= 0:     if num == 0:        print("Zero")     else:         print("Positive number") else:     print("Negative number") O/P: Positive number Example

  7. 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

  8. Example: x=15y=70if x > y: print(“x is greater ")else: print(“y is greater”)O/P: y is greater

  9. 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

  10. 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")

  11. For more follow us on our social media platforms: Instagram : learnbay_datascience Facebook : learnbay LinkedIn : Learnbay Twitter : Learnbay1 Thanks for Watching!!!

More Related