0% found this document useful (0 votes)
4 views

More_on_Python_Programming

Uploaded by

NAKSHATRA BANSAL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

More_on_Python_Programming

Uploaded by

NAKSHATRA BANSAL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

More on Python Programming

A computer program is a set of instructions known as statements. These


instructions are given to the computer to carry out an action. All the statements
are executed sequentially one after the other as they appear in a program but in
some situations we have to change the order of execution of the program based
on certain conditions. Therefore, we use control statements to control or change
the flow of execution.

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

Sequential Selective Statements Iterative Statements


Statements

Simple If For
programs While
If-else
using
identifiers, if-elif-else
operators, statement
expressions,
etc.

Sequential Statements Statement 1

The statements that are executed in a sequential order, i.e.


Statement 2
one after the other without any jumps, are called sequential
statements. A sequential structure is also known as a
straight line path. Statement 3

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

Iterative Control (Loop Control Structure)


False
Iteration means repetition of a set of statements, depending
upon a test condition. Till the time a condition
Condition
remains true, a set of statements are repeated again
and again. An iterative control structure is an True
arrangement of instructions and the iterative control
statements managing their execution.
Python provides the following iterative statements: Statement 1

(i) For statement


(ii) While 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

>>>if age > 18:

print(“You are eligible to cast a vote”)

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.

The if-else Statement


The if statement tells us that if a condition is true, it will execute a block of
statements and if a condition happens to be false, it won’t. We use the else
statement with if to execute a block of code when the condition is false.
Syntax:
if (conditional expression):
statement(s)
else:
statement(s)
First, the condition is evaluated. If the condition is true then block of indented
instructions are executed. If the result of the condition is false then the block of
instructions indented under else is executed.
Example 1:

The if-elif-else Statement


Sometimes, we need to execute different statements to perform in different
conditions. It requires the evaluation of several conditions. First of all, it checks
and evaluates the first condition. If it is true, it will execute the respective
statement(s), but if the condition is false, it goes to the if-else statement and
checks all the options, and if none are true then it executes the else statement(s).
Syntax:
if (conditional expression):
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)
Example:

The Nested if Statements


Nested means if statements or if-else statements are placed in the statement
block of the if statement or else statement. Python allows us to write an if
statement within another if statement.
Syntax:
if (conditional expression):
statement(s)
if (conditional expression2):
statement(s)
elif (conditional expression3):
statement(s)
else:
statement(s)
else:
statement(s)
Example:

Note:

A logical statement always evaluates as True/False.

Any value in Python other than zero is considered to be True, and zero is
considered as false.

You might also like