Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views9 pages

Python Notes SET 2

The document explains control flow in Python, focusing on conditional statements like if, elif, and else, as well as loops such as for and while. It details the syntax and usage of these statements, including nested if statements and the use of break and continue to alter loop execution. Additionally, it introduces the pass statement as a placeholder for future code implementation.

Uploaded by

precioustohw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Python Notes SET 2

The document explains control flow in Python, focusing on conditional statements like if, elif, and else, as well as loops such as for and while. It details the syntax and usage of these statements, including nested if statements and the use of break and continue to alter loop execution. Additionally, it introduces the pass statement as a placeholder for future code implementation.

Uploaded by

precioustohw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Control flow

A program’s control flow is the order in which the program’s code executes. The control flow of a
Python program is regulated by conditional statements, loops, and function calls. This section covers
the if statement and for and while loops.

The if Statement
Often, you need to execute some statements only if some condition holds, or choose statements to
execute depending on several mutually exclusive conditions. The Python compound statement if,
which uses if, elif, and else clauses, lets you conditionally execute blocks of statements.

There can be zero or more elif parts, and the else part is optional. The keyword ‘elif’ is short for ‘else
if’, and is useful to avoid excessive indentation.

In Python, the body of the if statement is indicated by the indentation. The body starts with an
indentation and the first unindented line marks the end.

Python if Statement Syntax


if test expression:

statement(s)

Here, the program evaluates the test expression and will execute statement(s) only if the test
expression is True.

If the test expression is False, the statement(s) is not executed.

In the above example, num > 0 is the test expression.

The body of if is executed only if this evaluates to True.

When the variable num is equal to 5, test expression is true and statements inside the body of if are
executed.

The print() statement falls outside of the if block (unindented). Hence, it is executed regardless of
the test expression.
Python if...else Statement
Syntax of if...else

if test expression:

Body of if

else:

Body of else

The if..else statement evaluates test expression and will execute the body of if only when the test
condition is True.

If the condition is False, the body of else is executed. Indentation is used to separate the blocks.

Example of if...else

In the above example, when num is equal to 5, the test expression is true and the body of if is
executed and the body of else is skipped.

If num is equal to -5, the test expression is false and the body of else is executed and the body of if is
skipped.

If num is equal to 0, the test expression is true and body of if is executed and body of else is skipped.

Python if...elif...else Statement


Syntax of if...elif...else

if test expression:

Body of if

elif test expression:

Body of elif

else:

Body of else

The elif is short for else if. It allows us to check for multiple expressions.
If the condition for if is False, it checks the condition of the next elif block and so on.

If all the conditions are False, the body of else is executed.

Only one block among the several if...elif...else blocks is executed according to the condition.

The if block can have only one else block. But it can have multiple elif blocks.

When variable num is positive, Positive number is printed.

If num is equal to 0, Zero is printed.

If num is negative, Negative number is printed.

Python Nested if statements


We can have a if...elif...else statement inside another if...elif...else statement. This is called nesting
in computer programming.

Any number of these statements can be nested inside one another. Indentation is the only way to
figure out the level of nesting.

Python Nested if Example


Python Loop
A for loop in Python is a control flow statement that is used to repeatedly execute a group of
statements as long as a certain condition is satisfied.

For loop
What is for loop in Python?
The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects.
Iterating over a sequence is called traversal.

Syntax of for Loop

for val in sequence:

loop body

Here, val is the variable that takes the value of the item inside the sequence on each iteration.

Loop continues until we reach the last item in the sequence. The body of for loop is separated from
the rest of the code using indentation.

The range() function


We can generate a sequence of numbers using range() function. range(10) will generate numbers
from 0 to 9 (10 numbers).

We can also define the start, stop and step size as range(start, stop,step_size). step_size defaults to
1 if not provided.

for loop with else


A for loop can have an optional else block as well. The else part is executed if the items in the
sequence used in for loop exhausts.
The break keyword can be used to stop a for loop. In such cases, the else part is ignored.

Hence, a for loop's else part runs if no break occurs.

Here is an example to illustrate this.

Here, the for loop prints items of the list until the loop exhausts. When the for loop exhausts, it
executes the block of code in the else and prints No items left.

This for...else statement can be used with the break keyword to run the else block only when the
break keyword was not executed. Let's take an example:

While loop
The while loop in Python is used to iterate over a block of code as long as the test expression
(condition) is true.

We generally use this loop when we don't know the number of times to iterate beforehand.

Syntax of while Loop in Python

while test_expression:

Body of while
In the while loop, test expression is checked first. The body of the loop is entered only if the
test_expression evaluates to True. After one iteration, the test expression is checked again. This
process continues until the test_expression evaluates to False.

In Python, the body of the while loop is determined through indentation.

The body starts with indentation and the first unindented line marks the end.

Python interprets any non-zero value as True. None and 0 are interpreted as False.

In the above program, the test expression will be True as long as our counter variable counter is less
than or equal to num (10 in our program).

We need to increase the value of the counter variable in the body of the loop. This is very important
(and mostly forgotten). Failing to do so will result in an infinite loop (never-ending loop).

Finally, the result is displayed.

While loop with else


Same as with for loops, while loops can also have an optional else block.

The else part is executed if the condition in the while loop evaluates to False.

The while loop can be terminated with a break statement. In such cases, the else part is ignored.
Hence, a while loop's else part runs if no break occurs and the condition is false.

Here is an example to illustrate this.


Here, we use a counter variable to print the string Inside loop three times.

On the fourth iteration, the condition in while becomes False. Hence, the else part is executed.

Break and continue


In Python, break and continue statements can alter the flow of a normal loop.

Loops iterate over a block of code until the test expression is false, but sometimes we wish to
terminate the current iteration or even the whole loop without checking test expression. The break
and continue statements are used in these cases.

Python break statement


The break statement terminates the loop containing it. Control of the program flows to the
statement immediately after the body of the loop.

If the break statement is inside a nested loop (loop inside another loop), the break statement will
terminate the innermost loop.

Syntax of break

Break
In this program, we iterate through the "string" sequence. We check if the letter is ‘i’, upon which
we break from the loop. Hence, we see in our output that all the letters up till ‘i’ gets printed. After
that, the loop terminates.

Python continue statement


The continue statement is used to skip the rest of the code inside a loop for the current iteration
only. Loop does not terminate but continues on with the next iteration.

Syntax of Continue

Continue

This program is same as the above example except the break statement has been replaced with
continue.

We continue with the loop, if the string is ‘i’, not executing the rest of the block. Hence, we see in
our output that all the letters except ‘i’ gets printed.
Pass statement
In Python programming, the pass statement is a null statement. The difference between a comment
and a pass statement in Python is that while the interpreter ignores a comment entirely, pass is not
ignored. However, nothing happens when the pass is executed. It results in no operation (NOP).

Syntax of pass

pass

We generally use it as a placeholder.

Suppose we have a loop or a function that is not implemented yet, but we want to implement it in
the future. They cannot have an empty body. The interpreter would give an error. So, we use the
pass statement to construct a body that does nothing.

You might also like