Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 133 - 2610_Springdales_C-7
P. 133

    Quick Quiz

                       1.  In Python programming, what is the purpose of an 'else' block in an if-else statement?

                                 a. It executes when the condition is true.     b. It executes when the condition is false.

                       2.   What will the following code snippet do: if x > 10: print("x is greater than 10")?

                                 a. It will print "x is greater than 10" when x is less than or equal to 10.

                                 b. It will print "x is greater than 10" when x is greater than 10.

                       3.   In a switch statement, what is used to specify different code blocks to be executed based on the value of
                          a variable?

                                 a. if-else                                    b. case labels

                       4.    What is the purpose of the 'break' statement in decision-making structures like switch or loops?

                                 a. It is used to exit the program.            b. It is used to exit the current code block or loop.

                                  Recap

                                   The if statement is the simplest conditional statement.
                                   In the if…else statement, if the condition evaluates to True, the indented block following the if
                                statement is executed, otherwise the indented block after the else statement is executed.
                                   Python allows the nested if statement.

                                   The if…elif…else ladder helps us to test multiple conditions and follows a top-down approach.

                                                       Assess Yourself

                            Choose the correct option.

                           1.  What will be the output of the following code:

                               x = 10

                               if x > 5:
                                   print("x is greater than 5")
                                      a. x is greater than 5                        b. 10 is greater than 5

                                      c. Nothing will be printed                    d. None of these

                                                                                    Conditional  Statements in Python  131

| 128 129 130 131 132 133 134 135 136 137 138 |