CONTROL STRUCTURES IN PYTHON
CONTROL STRUTURES
The function that we have developed so far had the property that each instruction in a function was executed exactly once. Further, the intructions in these functions were executed in the order in which they appeared in the functions. Such functions are called straight line functions. However, real life problem would usually require non-sequential and repetitive execution of instructions. Python provides various control structures for this purpose. In this, we will study the following control structures with suitable examples: if , for, and while.
General Form of if Conditional Statement
The general form of if conditional statement is as follows:
if < condition >:
< Sequence S of statements to be executed >
Here, condition is a Boolean expression, which is evaluated when the if statement is executed. If this condition evaluates to True, then the sequences S of statements is executed, and the control is transferred to the statement following the if statement. However, if the Boolean expression evaluates to False, the sequence of statements is ignored and the control is immediately transferred to the statement following If statement.
Note that < Sequence S of statements to be executed > following the colon is indented (i.e., shifted right). Next, suppose we wa to restrict the use of a system. To keep the things simple, all the v users are assigned a common password and password validation is the on task this program performs. We can verify the password entered by a against the password stored in the system using an if statement. If both the password match, the program prints a welcome message, else an error message indicating password mismatch is displayed.Let us see how the script in achieves this.
if password == 'za':
message = 'Login successful.'
if password !='za':
message = 'Password mismatch'
General Form of if-else Conditional Statement
The general forms of if-else statement is as follows:
if < condition >:
< Sequence S2 of statements to be executed>
General Form of if-elif-else Conditional Statement
The general form of if-elif-else statement is as follows:
if < condition1 >:
< Sequence S1 of statements to be executed >
elif < condition2 >:
< Sequence S2 of statements to be executed >
elif < condition3 >:
< Sequence S3 of statements to be executed >
.
.
.
else:
< Sequence Sn of statements to be executed >
In the above description, elif is an abbreviation used in Python for else if. By now, it must be clear that the clauses ellf and else of the If statement are optional, and that the sequence of statements defined under a clause is executed in a sequence. As the physical alignment of statements determines which statements form a block of code, one needs to be very careful about indentation while writing Python programs.
Nested if-elif-else Conditional Statement
Sometimes we need a control structure within another control structure. Such a mechanism is called nesting For example, the function maximum3 finds the maximum of three numbers using nested structure. Here, an Eclatoe has been used within another 1 clause. First, we test whether is greater than n2. If so, the condition n1 > n3 is evaluated, and if True, nl is declared as the maximum number. The other cases are dealt with similarly.
1 Response to "CONTROL STRUCTURES IN PYTHON"
Nice Blog!!
Python Online Training In Hyderabad
Python Online Training
Post a Comment