Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 111 - 2611_SmartGPT Pro V(5.0) C-7
P. 111

    In the previous class, you learnt that Python is a high-level, interpreted programming language
                     known for its simplicity and readability. You also explored how to download and install it on your
                     system and wrote and executed simple short programs in Python. In this chapter, you are going
                     to move onwards with Python basics and write more advanced programs.

                               PYTHON STATEMENTS-FLOW OF CONTROL

                     The order in which program statements are executed is called the flow of control. It can be
                     sequential, selective or iterative.

                     In all the programs you have written so far, the statements execute sequentially—meaning one
                     after the other, from top to bottom in the program. This type of flow is called sequential flow.

                     In selective flow of control, also called Decision Making, the interpreter chooses which statement
                     to execute above others, whereas in iterative flow, some statements are executed multiple
                     times. You have already learnt about sequential statements, let’s learn about the other flow of
                     control statements.

                               PYTHON SELECTION STATEMENTS

                     You make many decisions every day—like “What should I eat today?” or “Which shirt should I
                     wear?” Each of these decisions is based on a certain criterion. For example, your food choice might
                     depend on your mood or whether you’re following a specific diet. Once you’ve made the decision,

                     you follow it with an action. Therefore, decision-making is a two-step process: first, deciding what
                     to do based on a criterion and second, taking an action.

                     IF STATEMENT
                     Decision-making by a computer is based on the same two-step
                                                                                                       Start
                     process. In Python, decisions are made using the if statement,
                     also known as the selection statement. When an if statement is
                     executed, the computer evaluates a condition (a logical expression).           Input Age

                     If the condition is True, the action inside the if block is performed.
                     If not, it will be skipped.
                                                                                                                    False
                     Syntax of the if statement:                                                     Is Age>18?

                     if (condition):
                                                                                                          True
                       Statement(s)
                                                                                                   Print ‘Eligible’
                     The  if statement  starts with  the  keyword  if, followed by  a
                     condition and ends with a colon(:). The condition is essentially an
                     expression (comparison) that gives True or False as an answer.                    Stop

                                                                                               Flow of Control in Python  109

| 106 107 108 109 110 111 112 113 114 115 116 |