Page 73 - Touhpad Ai
P. 73

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
                                                                                                         if
                 Nested if statement                                                                               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
                                                                                                       Execute
                 evaluates to True. If the condition evaluates to False, then the control of execution is
                                                                                                   Statement block
                 passed to the next statement after the if 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
                                                                                                           inside if block
                 False, the code block associated with the else statement is executed.
                                                                                        Execute statement
                 The syntax of the if-else statement is as follows:
                                                                                         inside else block
                   if (Test Expression):
                            Statement block
                                                                                        Execute statement
                   else:
                                                                                       outside if...else block
                            Statement block
                    Program 15: To demonstrate the use of the if-else statement

                   # Asking the user for input
                   temperature = float(input("Please enter the temperature in Celsius: "))
                   # Checking if the temperature is above or below freezing point
                   if temperature <= 0:
                       print("It's freezing!")


                                                                                 Basic Concepts of Artificial Intelligence  71
   68   69   70   71   72   73   74   75   76   77   78