INTRODUCTION:
In the programs we have seen till now, there has always been a series of statements faithfully executed by Python in exact top-down order. What if you wanted to change the flow of how it works? For example, you want the program to take some decisions and do different things depending on different situations, such as printing ‘Good Morning’ or ‘Good Evening’ depending on the time of the day?
Similarly while writing program(s), we almost always need the ability to check the condition and then change the course of program.
Types of Statements in Python:
Statement is the instructions given to the computer to perform any kind of action.
Three type of Statement:
a) Empty Statement: As the name suggested, this statement does not contain anything. In python an empty statement is pass statement.
b) Simple Statement (Single Statement): Simple statements are comprised within a single logical line. Several simple statements may occur on a single line separated by semicolons. For example,
A=10
B=int(input(“Enter Number”))
c) Compound Statement: Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way.
For example:
a=int(input(“Enter Number: ”)
if a>0:
print(“Number is positive”)
else
print(“Number is negative”)
Flow charts:
A flowchart is the graphical or pictorial representation of an algorithm with the help of different symbols, shapes and arrows in order to demonstrate a process or a program. With algorithms, we can easily understand a program. The main purpose of a flowchart is to analyze different processes. Several standard graphics are applied in a flowchart:
A flowchart depicts pictorially the sequence in which instructions are carried out in an algorithm.
A flowchart is a schematic representation of a algorithm or a stepwise process, showing the steps of boxes of various kinds and their order by connecting these with arrows.
Flowcharts are used not only as aids in developing algorithms but also to document algorithms.
Flowcharts are used in designing or documenting a process or program.
Flowcharts Symbols:
For Example: Write Algorithm to check whether entered number is even or odd and also draw the flowchart for the same.
Algorithm:
Flowchart :
Step 1 : Start
Step 2 : Input Number
Step 3 : rem=number mod 2
Step 4 : if rem=0 then
Display “Even Number”
else
Display “Odd Number”
endif
Step 5 : Stop
Pseudo code is a term which is often used in programming and algorithm based fields. It is a methodology that allows the programmer to represent the implementation of an algorithm. Simply, we can say that it’s the cooked up representation of an algorithm. Often at times, algorithms are represented with the help of pseudo codes as they can be interpreted by programmers no matter what their programming background or knowledge is. Pseudo code, as the name suggests, is a false code or a representation of code which can be understood by even a layman with some school level programming knowledge.
Algorithm: It’s an organized logical sequence of the actions or the approach towards a particular problem. A programmer implements an algorithm to solve a problem. Algorithms are expressed using natural verbal but somewhat technical annotations.
Pseudo code: It’s simply an implementation of an algorithm in the form of annotations and informative text written in plain English. It has no syntax like any of the programming language and thus can’t be compiled or interpreted by the computer.
if "1"
print response
"I am case 1"
if "2"
print response
"I am case 2"
Introduction to Decision Trees :
A decision tree is a decision support tool that uses a tree-like graph or model of decisions and their possible consequences, including chance event outcomes, resource costs, and utility. It is one way to display an algorithm that only contains conditional control statements.
A decision tree is a flowchart-like structure in which each internal node represents a “test” on an attribute (e.g. whether a coin flip comes up heads or tails), each branch represents the outcome of the test, and each leaf node represents a class label (decision taken after computing all attributes). The paths from root to leaf represent classification rules.
Tree based learning algorithms are considered to be one of the best and mostly used supervised learning methods. Tree based methods empower predictive models with high accuracy, stability and ease of interpretation. Unlike linear models, they map non-linear relationships quite well. They are adaptable at solving any kind of problem at hand (classification or regression). Decision Tree algorithms are referred to as CART (Classification and Regression Trees).
“The possible solutions to a given problem emerge as the leaves of a tree, each node representing a point of deliberation and decision.”
- Niklaus Wirth (1934 — ), Programming language designer
Programme control flow:The control flow of a programme in any programming language can be broadly classified into three categories:
Sequential staements in the program are executed in a sequential order or in a linear fashion ,one after another without any jump in the program. In the figure, statement 1 will be executed first, followed by the statement 2 and then by the statement 3.
When the program uses a condition to decide which set of statements to be executed between two alternative sets of statements. In conditional statements 'if' statement is used. In figure 'body of if statement' will be executed if test condition evaluates to true.
When a certain set of statements need to be executed multiple times in a loop. In figure, body of loop will be executed until test expresssion evaluates to true.
Decision making is anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions.
Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise.
Following is the general form of a typical decision making structure found in most of the programming languages
Python programming language assumes any non-zero and non-null values as TRUE, and if it is either zero or null, then it is assumed as FALSE value.
Python programming language provides following types of decision making statements. Click the following links to check their detail.
Sr.No.Statement & Description
An if statement consists of a boolean expression followed by one or more statements.
An if statement can be followed by an optional else statement, which executes when the boolean expression is FALSE.
You can use one if or else if statement inside another if or else if statement(s).
Let us go through each decision making briefly −
If the suite of an if clause consists only of a single line, it may go on the same line as the header statement.
Here is an example of a one-line if clause −
a = 100
print "Good bye!"
When the above code is executed, it produces the following result −
Value of expression is 100
Good bye!
An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value.
The else statement is an optional statement and there could be at most only one else statement following if.
The syntax of the if...else statement is −
if expression:
statement(s)
else:
statement(s)
a = 100
if a:
print "1 - Got a true expression value"
print a
else:
print "1 - Got a false expression value"
print a
b = 0
if b:
print "2 - Got a true expression value"
print b
else:
print "2 - Got a false expression value"
print b
print "Good bye!"
When the above code is executed, it produces the following result −
1 - Got a true expression value
100
2 - Got a false expression value
0
Good bye!
The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.
Similar to the else, the elif statement is optional. However, unlike else, for which there can be at most one statement, there can be an arbitrary number of elif statements following an if.
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Core Python does not provide switch or case statements as in other languages, but we can use if..elif...statements to simulate switch case as follows −
a= 100
if a == 200:
print "1 - Got a true expression value"
print a
elif a == 150:
print "2 - Got a true expression value"
print a
elif a == 100:
print "3 - Got a true expression value"
print a
else:
print "4 - Got a false expression value"
print a
print "Good bye!"
When the above code is executed, it produces the following result −
3 - Got a true expression value
100
Good bye!