0% found this document useful (0 votes)
77 views63 pages

BPP Unit 2

The document discusses different types of control flow statements in Python including sequential, selection, and repetition statements. It provides examples of if, if-else, nested if-else, while and for loops. Key types of loops covered are condition-controlled loops where the number of iterations is unknown, and counter-controlled loops where it is known beforehand. Appropriate uses of different loop types are also highlighted.

Uploaded by

18WJ1A03M1
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)
77 views63 pages

BPP Unit 2

The document discusses different types of control flow statements in Python including sequential, selection, and repetition statements. It provides examples of if, if-else, nested if-else, while and for loops. Key types of loops covered are condition-controlled loops where the number of iterations is unknown, and counter-controlled loops where it is known beforehand. Appropriate uses of different loop types are also highlighted.

Uploaded by

18WJ1A03M1
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/ 63

UNIT - II

DECISION CONTROL STATEMENTS

Control flow statements:

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.

Python has three types of control structures:

 Sequential - default mode

 Selection - used for decisions and branching

 Repetition - used for looping, i.e., repeating a piece of code multiple times.

Sequential

Sequential statements are a set of statements whose execution process happens in a


sequence. The problem with sequential statements is that if the logic has broken in any one of
the lines, then the complete source code execution will break.

## This is a Sequential statement

a=20
b=10
c=a-b
print ("Subtraction is : “, c)

Output
1.02s
Subtraction is : 10
Selection/Decision control statements

In Python, the selection statements are also known as Decision control


statements or branching statements. The selection statement allows a program to test several
conditions and execute instructions based on which condition is true. Decision-making in
programming, much similar to decision-making in real life, is quite important as it helps us
decide what the program should do next. Decision-making helps in deciding the flow of
execution of the program.

Some Decision Control Statements are:

 Simple if

 if-else

 nested if

 if-elif-else

Simple if:

If statements are control flow statements that help us to run a particular code, but
only when a certain condition is met or satisfied. A simple if only has one condition to check.
If else in Python

If statement in Python tells the program what to do if the condition is true. In case the
condition is false, the program just goes on to execute what comes after if statements. In
situations where we want the program to execute some statement if the condition is true and
some other statement only if the condition is false, then we use if else in Python.

Following flowchart explains the working of if else in Python:


The syntax of the if...else statement:

if condition:

if-statement block

else:

else-statement block
Nested if-else statements

Nested “if-else” statements mean that an “if” statement or “if-else” statement is


present inside another if or if-else block. Python provides this feature as well, this in turn will
help us to check multiple conditions in a given program.An “if” statement is present inside
another “if” statement which is present inside another “if” statements and so on.

Nested if Syntax:

if(condition):

#Statements to execute if condition is true

if(condition):

#Statements to execute if condition is true

#end of nested if

#end of if

The above syntax clearly says that the if block will contain another if block in it and so on. If
block can contain ‘n’ number of if block inside it.

Let’s look at the nested if-else statement


Nested if-else Syntax:

if(condition):

#Statements to execute if condition is true

if(condition):

#Statements to execute if condition is true

else:

#Statements to execute if condition is false

else:

#Statements to execute if condition is false

Here we have included the “if-else” block inside an if block, you can also include an “if-else”
block inside “else” block.

Python Program to Find Largest of 3 Numbers

# input three integer numbers

num1=int(input("Enter num1: "))

num2=int(input("Enter num2: "))

num3=int(input("Enter num3: "))

# conditions to find largest

if num1>num2:

if num1>num3:

greater=num1

else:

greater=num3

else:
if num2>num3:

greater=num2

else:

greater=num3

# print the largest number

print("Greater = ",greater)

Output

Enter num1: 10

Enter num2: 20

Enter num3: 5

Greater = 20

if..elif..else statements

The elif is short for else if and is useful to avoid excessive indentation. If you have
multiple conditions to check and for each condition different code is required to execute, you
may use the elif statement of Python. The elif statement also takes an expression which is
checked after the first if statement. You may use multiple elif statements.

if expression:

statements

elif expression:

statements

else:

statements
Basic loop structures in python

In Python, loops can be used to solve awesome and complex problems. Loops are
used to Execute a block of statements repeatedly as long as the condition is TRUE. Or it Is an
iterator based loop, which steps through the items of iterable objects like lists, tuples, string
and executes a piece of code repeatedly for a number of times, based on the number of items
in that iterable object. There are two types of loops
1) while loop

2) for loop

While loop:

A while loop clause repetitively executes a set of statements until the given condition
turns out to be false. The general syntax for a Python while loop statement is:

variable

while (test_expression/condition) :

statement

increment/ decrement

The statement body can either be a single statement or a set or block of statements,
while the condition may or may not be an expression. In the Python while loop, the test
expression is evaluated at the start. The program execution enters the body of the while loop
if and only if the test_expression or condition evaluates to be true. The body of the while loop
is evaluated at every iteration. Evaluating the body of the while loop is continued until the
test_expression/condition evaluates to false. Now, the program control is passed to the line
immediately succeeding the while loop. In Python, indentation is used for determining the
body of the while loop.
for Loop

A for loop can iterate over every item in a list or go through every single character in
a string and won't stop until it has gone through every character. Writing for loops helps
reduce repetitiveness in your code, following the DRY (Don't Repeat Yourself) principle.
You don't write the same block of code more than once.

Syntax

for item in sequence:

statements(s)

range() function

Python range() function generates the immutable sequence of numbers starting from
the given start integer to the stop integer. It is a built-in function that returns a range object
consists of a series of integer numbers, which we can iterate using a for loop.

Syntax:

range([start,] stop [, step])

start: (Lower limit) It is the starting position of the sequence. The default value is 0 if not
specified.

stop: (Upper limit) generate numbers up to this number, i.e., An integer number specifying at
which position to stop (upper limit). The range() never includes the stop number in its result

step: Specify the increment value. Each next number in the sequence is generated by adding
the step value to a preceding number. The default value is 1 if not specified. It is nothing but
a difference between each number in the result.
Selecting appropriate loops in python

A programming language typically consists of several types of basic elements,


such as assignments, statements, and loops. The idea behind a loop is to repeat single actions
that are stated in the body of the loop. Different kinds of loops are common:

 as long as a specified condition is true


 until a certain condition is met
 for a fixed number of steps (iterations)
 endless loop and exit/break on condition

Condition controlled and counter controlled loop

Condition controlled loop:

A sentinel controlled loop is also called an indefinite repetition loop because


the number of iterations is not known before the loop starts executing. In a sentinel controlled
loop, a special value called sentinel value is used to change the loop control expression from
true to false in order to determine whether to execute the loop body. Sentinel controlled loop
is useful when we don’t know in advance how many times the loop will be executed. An
example of a sentinel controlled loop is the processing of data from a text file of unknown
size.

Counter Controlled Loop

A counter controlled loop is also known as definite repetition loop, since the
number of iterations is known before the loop begins to execute. The counter-controlled loop
has the following components:

 a control variable.
 the increment (or decrement) value by which the control variable is modified at each
iteration of the loop.
 the loop terminating condition that checks if looping should continue.
 Difference between Sentinel and Counter Controlled Loop in C is given
below:

SENTINEL COUNTER
BASIS OF CONTROLLED CONTROLLED
S.NO. COMPARISON LOOP LOOP

A sentinel controlled loop A counter controlled loop


is the indefinite repetition is the definite repetition
loop as the number of loop as the number of
repetitions is not known repetitions is known
before the loop begins before the loop begins
1. Definition executing executing

Controlled variable
Controlling variable used is know as Controlled variable used
2. variable sentinel variable. is know as counter.

Number of Unknown number of Known number of


3. iteration iteration iteration.

The value of the variable The value of the variable


4. Value of variable is not strict and it varies. is strict.

Limitation of The Limitation of the The Limitation of the


5. variable variable is strict. variable is strict also.
SENTINEL COUNTER
BASIS OF CONTROLLED CONTROLLED
S.NO. COMPARISON LOOP LOOP

A do….while loop is an A while loop is an


example of sentinel example of counter
6. Example controlled loop. controlled loop.
Nested Loop

A nested loop is a loop inside the body of the outer loop. The inner or outer
loop can be any type, such as a while loop or for loop. For example, the outer for loop can
contain a while loop and vice versa. The outer loop can contain more than one inner loop.
There is no limitation on the chaining of loops. In the nested loop, the number of iterations
will be equal to the number of iterations in the outer loop multiplied by the iterations in the
inner loop. In each iteration of the outer loop inner loop execute all its iteration. For each
iteration of an outer loop the inner loop re-start and completes its execution before the outer
loop can continue to its next iteration. Nested loops are typically used for working with
multidimensional data structures, such as printing two-dimensional arrays, iterating a list that
contains a nested list.
Loop Control Statements

Loop control statements change execution from its normal sequence. When execution leaves
a scope, all automatic objects that were created in that scope are destroyed.

Break Statement

It terminates the current loop and resumes execution at the next statement, just like the
traditional break statement in C. The most common use for break is when some external
condition is triggered requiring a hasty exit from a loop. The break statement can be used in
both while and for loops. If you are using nested loops, the break statement stops the
execution of the innermost loop and start executing the next line of code after the block.

Syntax

The syntax for a break statement in Python is as follows −

break
Flow Diagram

Continue Statement

It returns the control to the beginning of the while loop.


The continue statement rejects all the remaining statements in the current iteration of the
loop and moves the control back to the top of the loop. The continue statement can be used
in both while and for loops.

Syntax

continue

Flow Diagram

Pass Statement
It is used when a statement is required syntactically but you do not want any
command or code to execute. The pass statement is a null operation; nothing happens when it
executes. The pass is also useful in places where your code will eventually go, but has not
been written yet (e.g., in stubs for example)

Syntax

pass

Difference between Break and Continue in Python

Basis for
Break Continue
comparison

It eliminates the execution of the It will terminate only the current iteration
Task
remaining iteration of the loop of the loop.
‘break’ will resume control of the The ‘continue’ will resume the control of
Control after
program to the end of the loop the program to the next iteration of that
break/continue
enclosing that ‘break’. loop enclosing ‘continue’

It causes the early execution of the next


causes It early terminates the loop.
iteration.

The ‘continue’ does not stop the


The ‘break ‘stop the continuation of
continuation continuation of the loop and it stops the
the loop.
current.

Cannot be executed with the switch and


Other It is used with the ‘switch’, ‘label’
labels.

The else statement used with loops

Python allows the else keyword to be used with the for and while loops too.
The else block appears after the body of the loop. The statements in the else block will be
executed after all iterations are completed. The program exits the loop only after the else
block is executed.

Python Functions
Introduction:

A function is a block of organized, reusable code which can be called whenever


required. Functions are useful because they provide a high degree of modularity. Similar code
can be easily grouped into functions and you can provide a name to the function that
describes what the function is for. Functions are the simplest, and, sometimes the most useful,
tool for writing modular code.

The function helps us to organize code. The function accepts parameters as input,
processes them, and in the end, returns values as output. Let’s assume we defined a function
that computes some task. When we call that function from another function, the program
controller goes to that function, does some computation, and returns some value as output to
the caller function. You can call a function from another function

The following diagram shows how the function works.

Need for functions:


 With the help of functions, we can avoid rewriting the same logic or code again and
again in a program.
 Functions manage the inputs and outputs in computer programs.
 This Python Function help divide a program into modules. This makes the code
easier to manage, debug, and scale.
 Python Function allow us to change functionality easily, and different programmers
can work on different functions.
 Functions can be shared and used by other programmers
 Many persons can work on the same program by assigning different functions to each
of them.
 It encourages us to call the same function with different inputs over multiple times.

Types Of Python Functions

There are many types of Python Functions. And each of them is very vital in its own
way. The following are the different types of Python Functions:

Python Built-in Functions


Python User-defined Functions
Python Lambda Functions
Python Recursion Functions

Python Built-in Functions:

The Python interpreter has a number of functions that are always available for use.
These functions are called built-in functions. For example, print() function prints the given
object to the standard output device (screen) or to the text stream file. The help() method calls
the built-in Python help system. The len() function returns the number of items (length) in an
object. type() built-in, it returns the type of the given object.

Python User-defined Functions

A function is a piece of code that performs a particular task. A function is a block of


code that takes in some data and, either performs some kind of transformation and returns the
transformed data, or performs some tasks on the data, or both. Defining a function refers to
creating the function. This involves writing a block of code that we can call by referencing
the name of our function. A function has two main parts:
 function header
 function body

The syntax for defining a function:

def function_name(arguments): #function header

documentation strings

statement block # function body

return [expression]

Keyword def: This is the keyword used to say that a function will be defined now, and the
next word that is there, is the function name.

Function name: This is the name that is used to identify the function. The function name
comes after the def keyword. The rules for naming a function are the same as naming a
variable. It begins with either letter from A-Z, a-z in both upper & lower cases or an
underscore (_). The rest of its name can contain underscores (_), digits (0-9), any letters in
upper or lower case.

1. A reserved keyword may not be chosen as an identifier.

2. Good usage of grammar to ensure enhanced readability of code.

Parameter list: Parameter list are place holders that define the parameters that go into the
function. The parameters help to generalise the transformation/computation/task that is
needed to be done. In Python, parameters are enclosed in parentheses.

Function docstrings: These are optional constructs that provide a convenient way for
associated documentation to the corresponding function. Docstrings are enclosed by triple
quotes '''you will write the docstring here'''

Function returns: Python functions returns a value. You can define what to return by
the return keyword. In the example above, the function returns result. In case you do not
define a return value, the function will return None.
Function call:

Calling a function in Python is similar to other programming languages, using the


function name, parenthesis (opening and closing) and parameter(s).

Syntax:

Function_name()

(or)

function_name(arg1, arg2)

To call a function means that you are telling the program to execute the function. If
there is a return value defined, the function would return the value, else the function would
return None. To call the function, you write the name of the function followed by
parentheses. In case you need to pass parameters/arguments to the function, you write them
inside the parentheses.

Function Parameters and Arguments

A function or procedure usually needs some information about the environment, in


which it has been called. The interface between the environment, from which the function has
been called, and the function, i.e. the function body, consists of special variables, which are
called parameters. By using these parameters, it's possible to use all kind of objects from
"outside" inside of a function. The terms parameter and argument can be used for the same
thing: information that are passed into a function. A parameter is the variable listed inside the
parentheses in the function definition. An argument is the value that is sent to the function
when it is called.
Variable scope and lifetime

Not all variables are accessible from all parts of our program, and not all variables exist for
the same amount of time. Where a variable is accessible and how long it exists depend on
how it is defined. We call the part of a program where a variable is accessible its scope, and
the duration for which the variable exists its lifetime.

Scope: A variable is only available from inside the region it is created. This is called scope.

Life time of variable: duration for which the variable exists is called its lifetime.

Global and Local Variables:

A variable which is defined in the main body of a file is called a global variable. It
will be visible throughout the file, and also inside any file which imports that file. Global
variables can have unintended consequences because of their wide-ranging effects – that is
why we should almost never use them. Only objects which are intended to be used globally,
like functions and classes, should be put in the global namespace.
A variable which is defined inside a function is local to that function. It is accessible
from the point at which it is defined until the end of the function, and exists for as long as the
function is executing. The parameter names in the function definition behave like local
variables, but they contain the values that we pass into the function when we call it. When we
use the assignment operator (=) inside a function, its default behaviour is to create a new
local variable – unless a variable with the same name is already defined in the local scope.

Local Variable Vs. Global Variables

Parameter Local Global

Scope It is declared inside a function. It is declared outside the function.

If it is not initialized, a garbage value If it is not initialized zero is stored as


Value
is stored default.
Parameter Local Global

It is created when the function starts It is created before the program’s


Lifetime execution and lost when the functions global execution starts and lost when
terminate. the program terminates.

Data sharing is not possible as data of Data sharing is possible as multiple


Data sharing the local variable can be accessed by functions can access the same global
only one function. variable.

Parameters passing is required for Parameters passing is not necessary


Parameters local variables to access the value in for a global variable as it is visible
other function throughout the program

When the value of the local variable is When the value of the global variable
Modification of
modified in one function, the changes is modified in one function changes
variable value
are not visible in another function. are visible in the rest of the program.

Local variables can be accessed with


You can access global variables by
Accessed by the help of statements, inside a
any statement in the program.
function in which they are declared.

It is stored on the stack unless It is stored on a fixed location decided


Memory storage
specified. by the compiler.

Using the global statement:

Global keyword is used to modify the global variable outside its current scope and
meaning. It is used to make changes in the global variable in a local context. The keyword
‘Global’ is also used to create or declare a global variable inside a function. Usually, when
you create a variable inside a function (a local variable), it can only be used within that
function. That’s where the global keyword comes in the play, which helps to create global
variables inside the function and which can be accessible in a global scope.

Syntax:

Def func():

Global variable
Resolution of names

A scope defines the visibility of a name within a block. If a local variable is defined in
a block, its scope includes that block. If the definition occurs in a function block, the scope
extends to any blocks contained within the defining one, unless a contained block introduces
a different binding for the name. When a name is used in a code block, it is resolved using the
nearest enclosing scope. The set of all such scopes visible to a code block is called the
block’s environment. When a name is not found at all, a NameError exception is raised. If the
current scope is a function scope, and the name refers to a local variable that has not yet been
bound to a value at the point where the name is used, an UnboundLocalError exception is
raised. UnboundLocalError is a subclass of NameError.
The return statement:

A return statement is used to end the execution of the function call and “returns” the
result (value of the expression following the return keyword) to the caller. The statements
after the return statements are not executed. If the return statement is without any expression,
then the special value None is returned. The return keyword can send a value back to the
main program. A value could be a string, a tuple, or any other object. This is useful because it
allows us to process data within a function. Then, we can return the data to our main
program, so we can use it outside our function.
More definitions on functions

Type of arguments:

Argument: An argument is a variable (which contains data) or a parameter that is sent to the
function as input.

1. Formal arguments: When a function is defined it (may) has (have) some parameters
within the parentheses. These parameters, which receive the values sent from the
function call, are called formal arguments.
2. Actual arguments: The parameters which we use in the function call or the
parameters which we use to send the values/data during the function call are called
actual arguments.

In python, depending on the way or format we send the arguments to the function, the
arguments can be classified into four types:

1. Required arguments
2. Keyword arguments
3. Default arguments
4. Variable length arguments

Required Arguments

A required argument is indicating that it is necessary to provide a value or actual


argument in function call if parameters are mentioned in function definition. If a user do not
provide input value to the function parameter by passing actual parameter in function call in
correct positional order than an exception arises. In required arguments the number of
arguments passed in a function call must be equal to the number of parameters defined in
function definition

Keyword argument:

In python when functions are called, the values passed are assigned to the arguments based
on their positions, which means the first value passed will be assigned to the first argument.
This position-based distribution sometimes leaves the user confused because the user never
knows the format in which function was defined. Keyword arguments are often known as
"kwargs" are the ones that allow “key = value” syntax. This means it allows the caller to pass
the argument name with its value so that the caller does not get confused with the positioning
of the arguments. It ignores the order of the arguments.

Default Arguments in Python

In some cases, we may want to build a function that has some default values for
parameters in case if the user doesn’t provide values for them. In such cases, we can use the
default arguments. A default argument assumes a default value if a value is not supplied as an
argument while calling the function. This can be done using the assignment operator ‘=’. We
can provide default values for one or more parameters.

Note that any number of arguments in a function can have a default value. But, using
a non-default argument after default arguments will cause a SyntaxError. The reason for this
error is because argument values are assigned to parameters in order, based on their position.
The value for a non-default argument is mandatory, but the value for a default argument is
optional.

Variable-length Arguments: Sometimes you may need more arguments to process function
then you mentioned in the definition. If we don’t know in advance about the arguments
needed in function, we can use variable-length arguments also called arbitrary arguments.
For this an asterisk (*) is placed before a parameter in function definition which can
hold non-keyworded variable-length arguments and a double asterisk (**) is placed before a
parameter in function which can hold keyworded variable-length arguments. If we use one
asterisk (*) like *var, then all the positional arguments from that point till the end are
collected as a tuple called ‘var’ and if we use two asterisks (**) before a variable like
**var, then all the positional arguments from that point till the end are collected as
a dictionary called ‘var’.

Lambda Function

Lambda Function, also referred to as ‘Anonymous function’ is same as a regular


python function but can be defined without a name. Lambda Function, also referred to as
‘Anonymous function’ is same as a regular python function but can be defined without a
name. While normal functions are defined using the def keyword, anonymous functions are
defined using the lambda keyword. However, they are restricted to single line of expression.
They can take in multiple parameters as in regular functions.

The syntax for lambda function is:

lambda arguments: expression


Note: there can be any number of arguments but can contain only a single expression. There
is no return statement which is usually present in the def function syntax. The function will
simply return the expression value even when there is no return statement.

 Lambda functions have no name


 Lambda functions can take any number of arguments
 Lambda functions can return just one value in the form of an expression
 Lambda function definition does not have an explicit return statement but it always
contain an expression which is returned
 They are a one line version of a function and hence cannot contain multiple
expressions
 They cannot access variables other than those in their parameter list
 Lambda functions cannot even access global variables
 You can pass lambda functions as arguments in other functions
Documentation strings:

python documentation strings (or docstrings) provide a convenient way of associating


documentation with Python modules, functions, classes, and methods. It’s specified in source
code that is used, like a comment, to document a specific segment of code. Unlike
conventional source code comments, the docstring should describe what the function does,
not how.

 The doc string line should begin with a capital letter and end with a period.

 The first line should be a short description.

 If there are more lines in the documentation string, the second line should be blank,
visually separating the summary from the rest of the description.

 The following lines should be one or more paragraphs describing the object’s calling
conventions, its side effects, etc.

The docstrings are declared using ”’triple single quotes”’ or “””triple double quotes”””
just below the class, method or function declaration. All functions should have a docstring.

The standard components of docstrings included in function definitions are:

 a summary of the function

 the function inputs

 the function outputs

 an example of the function running including the result

The docstring is included right below the def line and is enclosed in triple quotes """ """.
The triple quotes are typically included on their own lines. The syntax to add a docstring in a
function definition is below.

def function_name(arguments):

"""

<docstring text>

"""
<code>

return expression
Recursive functions:

A function that calls itself is a recursive function. This method is used when a certain
problem is defined in terms of itself. Although this involves iteration, using an iterative
approach to solve such a problem can be tedious. The recursive approach provides a very
concise solution to a seemingly complex problem. It looks glamorous but can be difficult to
comprehend! Each recursive implementation has a base case, which is when the desired state
has been reached, and a recursive case where the desired state has not been reached and the
function enters another recursive step.

The most popular example of recursion is the calculation of the factorial. Mathematically
the factorial is defined as: n! = n * (n-1)!

We use the factorial itself to define the factorial. Hence, this is a suitable case to write a
recursive function. Let us expand the above definition for the calculation of the factorial
value of 5.

5! = 5 X 4!

5 X4 X 3!

5 X4 X 3 X 2!

5 X4 X 3 X 2 X 1!

5 X4 X 3 X 2 X 1

= 120
Pros and cons of recursion in Python

Pros:

 Faster when optimized: If you include recursion optimizations like tail-end recursion
and memorization, recursive approaches are faster in Python.
 Less code: Recursive solutions are more compact, which means you can write
recursive solutions faster and have less code to review when debugging.

 Declarative: Many developers find the logic of declaring the desired state to be more
understandable than iteration, which focuses on the steps needed to reach an unstated
goal.

 Efficient Sort and Search: Recursion is especially useful for Python data science as
it is the foundation of popular sort algorithms like merge sort.

Cons

 Maximum recursion depth: Python has a limited call stack that supports only 1000
nested steps. While this may seem like a lot, it becomes an issue when dealing with
large structures like lists and arrays. This can be overridden at your own rise
withsys.setrecursionlimit(1500).

 Supported, not suggested: Python allows recursive calls but does not include many
built-in optimizations. Unlike iterative optimizations, developers must code all-
recursive improvements themselves.

 Uses more memory: Each call saves the previous step in the call stack until the
recursive process is complete. This means recursive solutions use more memory than
iterative ones.

Difference Between Recursion and Iteration

Recursion and iteration both repeatedly executes the set of instructions. Recursion is
when a statement in a function calls itself repeatedly. The iteration is when a loop repeatedly
executes until the controlling condition becomes false. The primary difference between
recursion and iteration is that is a recursion is a process, always applied to a function.
The iteration is applied to the set of instructions which we want to get repeatedly executed.
BASIS FOR
RECURSION ITERATION
COMPARISON

Basic The statement in a body of function Allows the set of instructions to be


calls the function itself. repeatedly executed.

Format In recursive function, only Iteration includes initialization,


termination condition (base case) is condition, execution of statement
specified. within loop and update (increments
and decrements) the control
variable.

Termination A conditional statement is included The iteration statement is repeatedly


in the body of the function to force executed until a certain condition is
the function to return without reached.
recursion call being executed.

Condition If the function does not converge to If the control condition in the
some condition called (base case), iteration statement never become
it leads to infinite recursion. false, it leads to infinite iteration.

Infinite Repetition Infinite recursion can crash the Infinite loop uses CPU cycles
system. repeatedly.

Applied Recursion is always applied to Iteration is applied to iteration


functions. statements or "loops".

Stack The stack is used to store the set of Does not uses stack.
new local variables and parameters
each time the function is called.
BASIS FOR
RECURSION ITERATION
COMPARISON

Overhead Recursion possesses the overhead No overhead of repeated function


of repeated function calls. call.

Speed Slow in execution. Fast in execution.

Size of Code Recursion reduces the size of the Iteration makes the code longer.
code.

Module:

A module is a piece of software that has a specific functionality. A Python module is


a file that contains Python code. A module in programming allows us to logically organize
the python code. n Python, modules are used to divide the code into smaller parts. The
module in python has the .py extension. The name of the module will be the name of the file.
A python module can be defined as a python program file which contains a python code
including python function, class or variables. Modules are processed with two new statements
and one important built-in function which are:

 Import: Lets a client obtain a module as a whole


 From: Permits a client to fetch particular names from a module

 Reload: Gives a way to reload a code of module without stopping Python

Modules loading and execution:

When you use the import statement to import a module either in the interpreter, or a Python
script, or another module, then the interpreter searches for the imported module in the
mentioned sequence.

1. Firstly, it searches for a built-in module of that name.

2. In case the interpreter does not find a built-in module of that name, it then looks out
for this file in the directory list given by the variable called sys.path.

To check out the list of directories inside the variable sys.path, you can use the following
piece of code.

>>> import sys

>>> sys.path

The variable called sys.path is initialized from a bunch of locations. The first one is the
directory that contains the script which imports the module or the current directory. The next
one is the PYTHONPATH variable which contains a list of directories that follow the same
syntax as the PATH variable. And the final one is the default installation-dependent.

After the initialization process, the programs can modify the sys.path variable. The directory
which has the script, is placed at the top of the search path, even before the standard library
path, which means that instead of loading scripts with the same name from the standard
library, scripts from that directory will be loaded.

The PYTHONPATH is just an env variable that consists of a list of directories and can be set
using the following commands for Windows and Linux specifically.

set PYTHONPATH = c:\python20\lib;

set PYTHONPATH = /usr/local/lib/python

The import statement

To use the functionality, present in any module, you have to import it into your current
program. You need to use the import keyword along with the desired module name. When
interpreter comes across an import statement, it imports the module to your current program.
You can use the functions inside a module by using a dot(.) operator along with the module
name. First, let's see how to use the standard library modules.

More on Import Statements

Tere are more ways to import modules:

 from ... import statement

 from ... import * statement

 renaming the imported module

from ... import statement

The from...import statement allows you to import specific functions/variables from a module
instead of importing everything. In the previous example, when you
imported calculation into module_test.py, both the add() and sub() functions were imported.
But what if you only needed the add() function in your code?

Here is an example to illustrate the use of from...import

from calculation import add

print(add(1,2))
In above example, only the add() function is imported and used. Notice the use of add()? You
can now access it directly without using the module name. You can import multiple attributes
as well, separating them with a comma in the import statement. Take a look at the following
example:

from calculation import add,sub

from .. import * statement

You can import all attributes of a module using this statement. This will make all attributes of
imported module visible in your code.

Here is an example to illustrate the use of from .. import *:

from calculation import *

print(add(1,2))

print(sub(3,2))

Note that in the professional world, you should avoid using from..import and from..import*,
as it makes your code less readable.

Name or Rename Python Modules

Naming a Python module is simple. You can name them whatever you want. Just make sure
that the file name ends with .py extension. As already discussed, as soon as you create a
Python module, the name of the Python modules is stored inside a variable called __name__
present inside the module.

If you want to rename a Python module while importing it, you can use the ‘as’ keyword. It
simply creates an alias for the imported module and is bound directly to it.

For example,

>>> import Fibonacci as fib

>>> fib.fibonacci2(1000)
You can also use the ‘as’ keyword when using the ‘from’ keyword to import modules.

>>> from Fibonacci import fibonacci2 as fib2

>>> fib2(1000)

The above statements import the fibonacci2 function from the Fibonacci module and give it
an alias called fib2.

Please note that in order to make the execution efficient, each Python module is imported
only once in an interpreter session. Hence, if you change the modules in the middle of the
session, you need to restart the interpreter.

Making your own modules:

You can create as many modules as you want. Every file in the python is a module first create
a file and save it with .py extension.
The dir() built-in function

We can use the dir() function to find out names that are defined inside a module.
For example, we have defined a function add() in the module example that we had in the
beginning.

We can use dir in example module in the following way:

>>> dir(example)

['__builtins__',

'__cached__',

'__doc__',

'__file__',

'__initializing__',

'__loader__',

'__name__',

'__package__',

'add']

Here, we can see a sorted list of names (along with add). All other names that begin with an
underscore are default Python attributes associated with the module (not user-defined).

For example, the __name__ attribute contains the name of the module.

>>> import example

>>> example.__name__

'example'

All the names defined in our current namespace can be found out using the dir() function
without any arguments.

>>> a = 1

>>> b = "hello"

>>> import math


>>> dir()

['__builtins__', '__doc__', '__name__', 'a', 'b', 'math', 'pyscripter']

Python Namespace

A namespace is a collection of currently defined symbolic names along with


information about the object that each name references. You can think of a namespace as
a dictionary in which the keys are the object names and the values are the objects themselves.
Each key-value pair maps a name to its corresponding object. There is a chance the name of
the variable you are going to use is already existing as name of another variable or as the
name of another function or another method. In such scenario, we need to learn about how all
these names are managed by a python program. This is the concept of namespace.

Following are the three categories of namespace

 Local Namespace: All the names of the functions and variables declared by a program
are held in this namespace. This namespace exists as long as the program runs. < /p>

 Global Namespace: This namespace holds all the names of functions and other
variables that are included in the modules being used in the python program. It
encompasses all the names that are part of the Local namespace.

 Built-in Namespace: This is the highest level of namespace which is available with
default names available as part of the python interpreter that is loaded as the
programing environment. It encompasses Global Namespace which in turn
encompasses the local namespace.
Module-private variables

No variable of a module is really private. However, by convention, starting an


identifier with a single underscore (_), such as _secret, indicates that the identifier is meant to
be private. In other words, the leading underscore communicates to client-code programmers
that they should not access the identifier directly. Development environments and other tools
rely on the leading-underscore naming convention to discern which attributes of a module are
public (i.e., part of the module's interface) and which ones are private (i.e., to be used only
within the module). It is good programming practice to distinguish between private and
public attributes by starting the private ones with _, for clarity and to get maximum benefit
from tools. It is particularly important to respect the convention when you write client code
that uses modules written by others. In other words, avoid using any attributes in such
modules whose names start with _.

Advantages of modules –

 Reusability: Working with modules makes the code reusable.

 Simplicity: Module focuses on a small proportion of the problem, rather than


focusing on the entire problem.

 Scoping: A separate namespace is defined by a module that helps to avoid collisions


between identifiers.

Python Package

A Python package usually consists of several modules. Physically, a package is a folder


containing modules and maybe other folders that themselves may contain more folders and
modules. Conceptually, it’s a namespace. This simply means that a package’s modules are
bound together by a package name, by which they may be referenced.

A package folder usually contains one file named __init__.py that basically tells Python:
“Hey, this directory is a package!” The init file may be empty, or it may contain code to be
executed upon package initialization.

You’ve probably come across the term “library” as well. For Python, a library isn’t as clearly
defined as a package or a module, but a good rule of thumb is that whenever a package has
been published, it may be referred to as a library.
How to Use a Python Package

We’ve mentioned namespaces, publishing packages and importing modules. If any of these
terms or concepts aren’t entirely clear to you, we’ve got you! In this section, we’ll cover
everything you’ll need to really grasp the pipeline of using Python packages in your code.

Importing a Python Package

We’ll import a package using the import statement:

Let’s assume that we haven’t yet installed any packages. Python comes with a big collection
of pre-installed packages known as the Python Standard Library. It includes tools for a range
of use cases, such as text processing and doing math. Let’s import the latter:

You might think of an import statement as a search trigger for a module. Searches are strictly
organized: At first, Python looks for a module in the cache, then in the standard library and
finally in a list of paths. This list may be accessed after importing sys (another standard
library module).

The sys.path command returns all the directories in which Python will try to find a package.
It may happen that you’ve downloaded a package but when you try importing it, you get an
error:

In such cases, check whether your imported package has been placed in one of Python’s
search paths. If it hasn’t, you can always expand your list of search paths:
At that point, the interpreter will have more than one more location to look for packages after
receiving an import statement.
The globals(), locals() and reload() Functions

The globals() and locals() functions can be used to return the names in the global and local
namespaces depending on the location from where they are called.

If locals() is called from within a function, it will return all the names that can be accessed
locally from that function.

If globals() is called from within a function, it will return all the names that can be accessed
globally from that function.

The return type of both these functions is dictionary. Therefore, names can be extracted using
the keys() function.

When the module is imported into a script, the code in the top-level portion of a module is
executed only once.

Therefore, if you want to reexecute the top-level code in a module, you can use
the reload() function. The reload() function imports a previously imported module again. The
syntax of the reload() function is this −

reload(module_name)

Here, module_name is the name of the module you want to reload and not the string
containing the module name. For example, to reload hello module, do the following −

reload(hello)
function redefinition

Python is dynamic in nature. It is possible to redefine an already defined function.

You might also like