Page 191 - Ai_V3.0_c11_flipbook
P. 191

Program 13: To demonstrate the concept of Sequence statement

                     # Perform an Addition
                     num1 = 10
                     num2 = 20
                     result = num1 + num2
                     # Display the result

                     print("The result of", num1, "+", num2, "is:", result)
                 Output:

                     The result of 10 + 20 is: 30
                 Conditional Statements
                 Conditional statement, also referred to as a selection statement, decision-making statement or branching statement,
                 are used to control the flow of execution based on certain conditions. Conditional statements in Python allow you to
                 execute certain blocks of code based on specific conditions.
                 In Python, there are various types of conditional statements, which are as follows:
                  • •  if statement
                  • •  if...else statement
                  • •  if…elif…else statement
                  • •  Nested if statement                                                               if        False
                                                                                                     Condition
                 The if Statement
                 The if statement is the simplest conditional statement. Here, a statement or a collection   True
                 of statements within the if block are executed only if a certain condition or expression
                 evaluates to True. If the condition evaluates to False, then the control of execution is   Execute
                 passed to the next statement after the if block.                                  Statement block

                 The syntax of the if statement is as follows:
                                                                                                 Execute statement
                     if (Test Expression):
                                                                                                  outside if block
                          Statement block

                  Program 14: To demonstrate the use of the if statement

                     x = 10
                     if x > 5:
                         print(x,"is greater than 5")
                 Output:
                     10 is greater than 5

                 The if-else Statement                                                         if         True
                 The if-else statement executes different blocks of code based on whether a   condition
                 condition is true or false. If the condition evaluates to True, the code block
                 associated with the if statement is executed. If the condition evaluates to   False    Execute statement
                 False, the code block associated with the else statement is executed.                    inside if block
                 The syntax of the if-else statement is as follows:                    Execute statement
                                                                                        inside else block
                     if (Test Expression):
                          Statement block
                                                                                       Execute statement
                     else:
                                                                                      outside if...else block
                          Statement block
                                                                                         Python Programming     189
   186   187   188   189   190   191   192   193   194   195   196