More_on_Python_Programming
More_on_Python_Programming
Control Structure
A control structure is a programming language construct which affects the
flow of the execution of a program.
Various types of control structures provided by Python are described below:
Control Structure
Simple If For
programs While
If-else
using
identifiers, if-elif-else
operators, statement
expressions,
etc.
Statement 4
Selective Statements
Some problems cannot be solved by performing
a set of ordered steps as seen in a True
sequential execution. When Condition Statement 1
programmers are required to execute a
particular set of statements depending upon a
particular test condition, a branching statement False
is required. Python provides the
following selection statements: Statement 1
(i) if statement
(ii) if-else
(iii) if-elif-else statement
The if Statement
The if statement is a decision-making statement and is used to control the flow
of execution of statements. An if statement contains a logical expression using
which data is compared. A decision is made based on the result of the
comparison. If the result of the expression is true, the statements within the if
block are executed and if the result of the expression is false, the statements
within the if blocks are not executed.
Syntax:
if (conditional expression):
statement(s)
Example 1:
>>> age = 30
In the above example, the conditional statement under if() will always evaluate
the result as True because the value of x is greater than 18. Hence, the output of
the above code would be ‘You are eligible to cast a vote’.
Key Points:
There is no limitation on the number of statements that can appear under
an if block.
if is a keyword, followed by a condition and ended with colon (:).
A statement or statements written that are indented with an if statement
are executed only if the condition evaluates to be true, otherwise the
control skips the control statements indented inside the if statement and
passes to the statement outside the if statements.
Note:
Any value in Python other than zero is considered to be True, and zero is
considered as false.