You are on page 1of 14

UNIT-3

CONTROL FLOW, FUNCTION


Conditionals:
Boolean values and Operators
Conditional (if)
Alternative (if-else)
Chained Conditional (if-elif-else)
Nested Conditionals
Iteration:
State of a variable
Looping Statement
Loop Control Statements
Fruitful functions:
Return value
Parameters
Local and Global Scope
Function Composition
Recursion
Illustrative programs:
Square Root
GCD
Power Exponentiation
Linear Search
Binary Search.
CONDITIONAL
Boolean values:
There are two types of Boolean values True or False.
The Boolean expression can be represent using the operator.
Example:
>>> 3==3
True
>>> 4==3
False
For Boolean expression:
>>> 5+2=7
True
Input and output:
Input:
Python it is possible to input the data using keyboard and function( input() ) is used.
Example:
Program:
Print (Enter first number)
a = int( input() )
Print (Enter second number)
b = int( input() )
c = a+b
Print (The result is)
Print (c)
Output:
Enter first number
10
Enter second number
20
The result is
30
Output statement:

Using .format of the data can be display on the purpose curly bracket { }

Example:
Program:
n = 10
Print (There are { } number)
Output:
There are 10 numbers
Conditional Statement:
The if statement is the simplest form of the conditional statement.
Syntax:
if condition:
statement
Example:
if x == y:
Print (It is equal)
Alternative Statement:
The alternative statement are the types of if statement in which if else are used.
Syntax:
if condition :
statement
else condition :
statement
Example:
if a>b:
Print (a is greater)
else:
Print (b is greater)
Chained Conditional:
The chained conditional execution will be such that each condition is checked in order.

The elif is basically abbreviation of else , if


There is no limit on the number of elif statement.

In elif case the else condition should be at the end.

Syntax:
if condition:
statement
elif condition:
statement
Example program :
def mark (m):
if m>=75:
Print(distinction)
elif m>=60:
Print (first class)
elif m>=50:
Print (second class)
else:
Print (grade: fail)
m=int (input(Enter the mark scored:) )
Print (your grade is ,mark(m))
Output:
>>>Enter the mark scored : 100
Your grade is distinction.
Nested Condition:
Specified one condition inside another condition is called as nested condition.
Example program:
def compare two num (a,b)
if a==b:
Print (both the number are equal)
else:
if a<b:
Print (first number is less than second)
else:
Print (second number is greater than first)
ITERATION
Iteration is a technique that allows to execute block if statement repeatedly.
Repeated execution of a set of statement is called iteration.
The programming construct used for state, while, for, break, continue and so on.

State:

The simplest form of statement is assignment statement. The statement is specified using
=operator.

Example:

>>> x=10
>>>Print (x)
10
>>>Print (x)
100
While:
The body of while contains the statement which will change the value of the variable used in
the test condition.
Example:
While i< = 10
i = i+1
Flow chart:

Test

condition False

Bodyof while
Exit from loop

Using the condition the given statement is True or False.


Expression is False then exit the while statement.
Expression is True then execute again to the value.
Example program:
Print (enter the value of value of n)
n= int( input( ) )
i=1
Print (the multiple value is )
whilei<=n:
Print (i ,i*i)
i = i+1
output:
Enter the value of n
5
The multiple value is
1 1
2 4
3 9
4 16
5 25

For loop:
Loop continues until the last item in sequence.
The Body of the loop is separated from the rest of the code using indentation.
Example:
For val in number
val = val+1
Example program:
Program:
Print (enter the value of n)
n= int(input() )
sum = 0
for I in range (1,n+1):
sum = sum + i
i= i+1
Print (the sum of, n, number is, sum)
Output:
Enter the value of n
5
The sum of 5 number is 15.
Continue statement:
The continue statement is used to skip some statement inside the loop and decision making statement
such as if and if else.
When continue statement force to execute the next iteration of the loop to executer.
Syntax:
continue
Flow chart:

False
condition

True

Exit loop

condition

Remaining
statement
Example Program:
i=0
whilei<10
i=i+1
ifi % 2 ==0
continue
print [1]
output:
1
3
5
7
9

FRUITFUL FUNCTION
Fruitful function are the function that return values while using fruitful function, the return
values must be handled properly by assigning it to a variable or use it as part of expression.

Two types of function:

The function that return some value.


The function that does not return value.

Return value:
The value can be returned from function using the keyword return.
Syntax:
Return [expression list]
Example program:
def area ( r )
result = 3.14 * r ** 2
Print (the area of circle)
return (result)
output:
>>>area ( 10 )
The area of circle
314.0
Parameter:
We can pass different number of parameter to the function.
following example illustrate the parameter passing to the function .
Creating simple calculator:
def add(x,y)
returnx+y
print (main menu)
print (1,add)
print (enter first choice)
choice = int( input() )
print (enter first number)
num1=int( input () )
print (enter second number)
num2=int( input () )
if choice ==1:
print (num1,+,num2,=,add (num1,num2) )
else:
print (invalid number)
output:
Main menu
1.add
Enter your choice
1
Enter first number
10
Enter second number
20
10+20=30
Local and global scope:
The global variable are those variable that are declared and defined outside the function and
can be used inside the function.
The local variable are those variable that are declared and defined inside the function.
Example:
>>>deffn( ) >>>a=100
>>>a = 100>>>deffn( )
>>>print (a) >>>print(a)
(local scope) (global scope)

Function composition:
It is a way of combining function such that the result of each function is passed as the
argument of next function.
Example:
fand g is defined f (g(x) )
Example program:
def add (a,b):
returna+b
defmul (c,num):
return c*num
defmainfun (x,y):
z=add(x,y)
result =mul(z,10)
return result
n1=int(input (enter one) )
n2=int(input (enter two) )
print (function composition,mainfun (n1,n2) )
output:
enter one 10
enter two 20
function composition 300
Recursion:
Recursion is a property in which one function calls itself repeatedly in which the values of
function parameter get changed on each cell.
Property:
1. Must have base class.
2. Must change its state and move towards the base case.
3. Must call itself recursion.
Example program:
def fact (n):
if n==0:
return1
else:
result=n*fact(n-1)
return result
print (fact (5) )
output:
120

ILLUSTRATIVE PROBLEMS

Binarysearch:
Program :
defbinary_search(item_list,item)
first=0
last=len(item_list)-1
while (first<=last):
mid=(first+last)/2
ifitem_list[mid]==item:
found=true
elif item<item_list[mid]:
last=mid-1
else:
first=mid+1
return found
print(binary_search([1,2,3,5,8],6))
print(binary_search([1,2,3,5,8],5))
output:
False
True
Linear Search:
Program:
defseq_search(list,item):
pos=0
found=false
whilepos<len(list) and not found
if ( int (list [pos])==item)
found=True
pos=pos+1
return(found,pos)
Ls=[ ]
n=int(input(enter no of elements in a list))
fori in range (0,n):
v=int(input(enter no:))
Ls.append(v)
Print(Ls)
key = int (input(enter key to be searched))
print(seq_search(Ls,key))
output:
enter no of elements in the list 3
enter no:1
enter no:2
enter no:3
[1,2,3]
enter key to be searched 2
(True,2)

Power exponential:
Program:
n=2
e=3
r=n
fori in range (1,e)
r=n*r
result = r
print (result:,result)

output:
result:8
Square root by newton method:

Program:
defnt(i):
app=0.5*i
for j in range (1,10):
res = 0.5*(app+i/app)
app=res
return res
num=5
print(num)
fori in range (1,num+1):
print( square root of,i ,is:, nt(i) )

output:
5
Square root of 1 is : 1.0
Square root of 2 is : 1.414213562373095
Square root of 3 is : 1.7320508075688772
Square root of 4 is : 2
Square root of 5 is : 2.23606797749979

GCD Numbers:
Program:
X=54
Y=24
if x>y:
smaller = y
else:
smaller=x
fori in range (1,smaller+1)
if ((x%i == 0) and (y%i == 0)):
gcd = i
print (gcd)

output:
6

You might also like