0% found this document useful (0 votes)
17 views18 pages

Python Programming: Control Flow Basics

Uploaded by

p711922
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)
17 views18 pages

Python Programming: Control Flow Basics

Uploaded by

p711922
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

Introduction to Python Programming (MBABA313)

MODULE 2
Program Flow

2.1 Iteration
Repeated execution of a set of statements is called iteration. The for loop
and while loop are used to do iterations.
Computers are often used to automate repetitive tasks. Repeating
identical or similar tasks without making errors is something that
computers do well and people do poorly.

2.2 Assignment
We can store values in variables with an assignment statement.

An assignment statement consists of


 A variable name on left hand side.
 An equal sign (= called the assignment operator or assignment
token).
 A value to be stored on right hand side.

where ‘num’ is variable name will have the integer value 50 stored in it.

The box in Figure below shows the num variable in this example stores 50
until we replace with 82. When a new value is assigned to a variable, the
old one is forgotten.

The Augmented assignment operators are:

Augmented Assignment Statement Equivalent Assignment Statement


spam+=1 spam=spam+1
spam-=1 spam=spam-1
spam*=1 spam=spam*1
spam/=1 spam=spam/1
spam%=1 spam=spam%1

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 1


Introduction to Python Programming (MBABA313)

2.3 Flow control statements


i) Conditional (if) or if Statement:
When the condition which is a Boolean expression evaluates to True, the if statement block
will be executed. Otherwise, it is skipped.

In python, an if statement consists of:


 The if keyword
 A condition which is Boolean expression that evaluates to True or False
 A colon
 Starting on the next line, an indented block of code (called if clause)

Flowchart:

ii) alternative (if-else) or else statement


The else clause is executed only when the if statement condition is False.

In python, an else statement consists of:


 The else keyword
Chandrakala S, Asst. Professor, SJCIT, Chickballapur 2
Introduction to Python Programming (MBABA313)

 A colon
 Starting on the next line, an indented block of code (called else clause)
when the if condition is True, statement block1 will be executed and when the condition
is False, statement block 2 will be executed.

Flowchart

iii) chained conditional (if-elif-else) or elif statement:


when we have more than one condition then elif statement is used.
The elif statement is an “else if” statement that always follows an if or another elif
statement.

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 3


Introduction to Python Programming (MBABA313)

In python, an elif statement consists of:


 The elif keyword
 A condition which is Boolean expression that evaluates to True or False
 A colon
 Starting on the next line, an indented block of code (called elif clause)

Flowchart

ex:

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 4


Introduction to Python Programming (MBABA313)

2.4 The for loop

The for loops are used when we have a block of code which we want to
repeat a fixed number of times.

A for statement includes the following:


 The for keyword
 One or more variable names called loop variables
 The in keyword
 start - It is optional. An integer number specifying at which
position to start. By default, it is 0.
 stop - It is required. An integer number specifying at which
position to stop (but not included).
 step - It is optional. An integer number specifying the
incrementation. By default, it is 1.
 A colon
 Starting on the next line, an indented block of code (called the for
clause or loop body)
Flow chart

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 5


Introduction to Python Programming (MBABA313)

We can even use a negative number for the step argument to make the for
loop count down instead of up.

The for loop processes each item in a list. Each item in turn is (re-)assigned
to the loop variable, and the body of the loop is executed. Running through
all the items in a list is called traversing the list, or traversal.

Or

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 6


Introduction to Python Programming (MBABA313)

2.6 The while statement


we can make block of code execute over and over again with a while loop
statement.
The while clause will be executed as long as the while condition is True.

Syntax:
while condition:
statement block
In python, while loop statement consists of:
 The while keyword
 A condition which is Boolean expression that evaluates to True or
False
 A colon
 Starting on the next line, an indented block of code (called while
clause)

The body of the loop should change the value of variable so that eventually
the condition becomes false and the loop terminates. Otherwise, the loop
will repeat forever which is called an infinite loop.
Flowchart

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 7


Introduction to Python Programming (MBABA313)

It means, while i is less than 5, continue executing the body of the loop.
Within the body, each time, increment i by 1.

Or

2.6 Tables
One of the things loops are good for is generating tables.

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 8


Introduction to Python Programming (MBABA313)

Note:
Escape Characters
An escape character lets us use characters that are otherwise impossible
to put into a string.
An escape character consists of a backslash(\) followed by the character
we want to insert to the string.

2.7 Two-dimensional tables


A two-dimensional table is a table where we read the value at the
intersection of a row and a column.
A multiplication table is a good example.

The end=" " argument in the print function suppresses the newline, and
uses space instead.

2.8 break statement


The break statement is used to immediately leave the body of its loop.
If the execution reaches a break statement, it immediately exits the loop.
In code, a break statement simply contains the break keyword.

Syntax:

Flowchart

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 9


Introduction to Python Programming (MBABA313)

2.9 continue statement


With the continue statement we can stop the current iteration of the loop,
and continue with the next.
when program execution reaches a continue statement, it jumps back to
the start of the loop and re-evaluates the loop’s condition.
In code, a continue statement simply contains the continue keyword.

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 10


Introduction to Python Programming (MBABA313)

Flowchart

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 11


Introduction to Python Programming (MBABA313)

2.10 Paired data


Making a pair of things in Python is as simple as putting them into
parentheses.

In the above example fruits list has 3 elements, each of them pairs. we’
have used a pair of variable names i.e. name and count.

Functions
2.11 Functions
A function is a block of code which only runs when it is called. A function
is like a mini program within a program.
The basic need of functions is to group a set of statements that gets
executed repeatedly many times. Functions will convert a big complex
program into small and simple sub programs.

.A function consists of:


 The keyword def.
 The function name.
 The parameters enclosed in parentheses
 A colon
 Starting on the next line, an indented statement block.

The parameter list may be empty, or it may contain any number of


parameters separated from one another by commas.
A programmer can create his own function as per requirement called user
defined function.

2.12 Functions that require arguments

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 12


Introduction to Python Programming (MBABA313)

Most functions require arguments: the arguments provide for


generalization.
For example, if we want to find the absolute value of a number, we have
to indicate what the number is.
Python has a built-in function for computing the absolute value:

In this example, the arguments to the abs function are 5 and -5.
Some functions take more than one argument. For example the built-in
function pow takes two arguments, the base and the exponent.

The built-in function max takes any number of arguments:

Parameters are passed during the definition of function. A parameter is


the variable listed inside the parentheses in the function definition. The
arguments are passed during the function call.

2.13 Variables and parameters are local


Local Scope
 Parameters and variables that are assigned in a called function are
said to be exist in that function’s local scope.
 A variable that exists in a local scope is called a local variable.
 A local scope is created whenever a function is called. When the
function returns, the local scope is destroyed, and these variables are
forgotten.

Global scope
 Variables that are assigned outside all the functions are said to exist
in the global scope.
 A variable that exists in a global scope is called a global variable.

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 13


Introduction to Python Programming (MBABA313)

 The global scope is created when our program begins. When program
terminates, the global scope is destroyed.

Scopes matter for several reason:


i) global scope cannot use any local variable:

ii) global variables can be read from a local scope:

iii) local scope cannot use variables in other local scope:

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 14


Introduction to Python Programming (MBABA313)

iv) local and global variables with same name

2.14 Return values


The value that a function call evaluates to is called the return value of
the function. We can specify what the return value should be with a
return statement. This statement means: evaluate the return expression,
and then return it immediately as the result (the fruit) of this function.
A return statement consists of:
 return keyword
 the value or expression that the function should return.

2.15 Functions that return values


A function with return value is called fruitful function.

On the other hand, temporary variable like A below often make


debugging easier.

Output:

A function without return value is called void function.

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 15


Introduction to Python Programming (MBABA313)

2.16 Functions can call other functions


Functions can call other functions. In Python, one function can call
another function to break a problem into smaller, manageable tasks. This
makes code easier to read, reuse, and maintain.

2.17 Flow of execution


1. The order in which statements are executed is called the flow of
execution.
2. Execution always begins at the first statement of the program.
3. Statements are executed one at a time, in order, from top to bottom.
4. Function definitions do not alter the flow of execution of the program,
but remember that statements inside the function are not executed until
the function is called.
5. Function calls are like a detour in the flow of execution. Instead of going
to the next statement, the flow jumps to the first line of the called function,
executes all the statements there, and then comes back to pick up where
it left off.
[Link] one function can call another. The one function may execute the
another function satements.
7. When it gets to the end of the program, it terminates.

Note: When we read a program, don’t read from top to bottom. Instead,
follow the flow of execution. This means that we will read the def
statements as we are scanning from top to bottom, but we should skip the
statements of the function definition until we reach a point where that
function is called.

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 16


Introduction to Python Programming (MBABA313)

Programs:
Compound Interest:

Recursion
The function which includes call to itself are called recursive functions.

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 17


Introduction to Python Programming (MBABA313)

#Program to compute binomial co-efficient. (Given n and r).

Chandrakala S, Asst. Professor, SJCIT, Chickballapur 18

You might also like