Python conditional statement - PowerPoint PPT Presentation

About This Presentation
Title:

Python conditional statement

Description:

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. – PowerPoint PPT presentation

Number of Views:2409

less

Transcript and Presenter's Notes

Title: Python conditional statement


1
(No Transcript)
2
  • 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.

3
(No Transcript)
4
If statement
  • 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 statementif expression statement
    1
  • Else is completely an optional block.

5
(No Transcript)
6
  • 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 f
    rom the body. 
  • The line after the colon has to be indented. 
  • It is common in Python to use four indenting space
    s. 
  • All rows indented after the colon will be executed
     whenever the BOOLEAN EXPRESSION is valid.

7
Examples of if statement
  • a 15b 20if b gt a print("b is greater than
    a")O/Pb is greater than a

8
Nested if statement
  • Syntaxif expression1 statement(s) if
    expression2 statement(s) elif expression3
    statement(s) elif expression4
    statement(s) else statement(s) else
    statement(s)

9
(No Transcript)
10
Example
  • num 15if num gt 0     if num 0
           print("Zero")     else
            print("Positive number") else
        print("Negative number")
  • O/P Positive number

11
if .......else Statement
  • 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.
  • Syntaxif BOOLEAN EXPRESSION STATEMENTS_1
    else STATEMENTS_2

12
(No Transcript)
13
  • Example x15y70if x gt y print(x is
    greater ")else print(y is greater)O/P y
    is greater

14
Elif statement
  • The elif 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

15
  • Syntax if condition 1 STATEMENTS_A elif
    conditon 2 STATEMENTS_B else STATEMENTS_C
  • Examplea  33b  33if b gt a  print("b is
    greater than a")elif a b  print("a and b
    are equal")

16
Thanks for Watching!!!
  • For more follow us on our social media platforms
  • Instagram learnbay_datascience
  • Facebook learnbay
  • LinkedIn Learnbay
  • Twitter Learnbay1
Write a Comment
User Comments (0)
About PowerShow.com