Page 432 - CITS - Computer Software Application -TT
P. 432

COMPUTER SOFTWARE APPLICATION - CITS




               # condition is true
           else:
               # Executes this block if
               # condition is false
           Example:

           The block of code following the else statement is executed as the condition present in the if statement is false after
           calling the statement which is not in the block(without spaces).

           # Explicit function
           def digitSum(n):
               dsum = 0
               for ele in str(n):

                   dsum += int(ele)
               return dsum
           # Initializing list

           List = [367, 111, 562, 945, 6726, 873]
           # Using the function on odd elements of the list

           newList = [digitSum(i) for i in List if i & 1]
           # Displaying new list
           print(newList)

           Output :
           [16, 3, 18, 18]

           Nested-if Statement
           A nested if is an if statement that is the target of another if statement. Nested if statements mean an if statement
           inside another if statement.
           Yes, Python allows us to nest if statements within if statements. i.e., we can place an if statement inside another
           if statement.
           Syntax:
           if (condition1):

              # Executes when condition1 is true
              if (condition2):
                 # Executes when condition2 is true
              # if Block is end here
           # if Block is end here

           Example:
           In this example, we are showing nested if conditions in the code, All the If conditions will be executed one by one.
           # python program to illustrate nested If statement

           i = 10
           if (i == 10):





                                                           419

 CITS : IT&ITES - Computer Software Application - Lesson 120 - 137  CITS : IT&ITES - Computer Software Application - Lesson 120 - 137
   427   428   429   430   431   432   433   434   435   436   437