All the IPython Notebooks in Python Flow Control Statements lecture series by Dr. Milaan Parmar are available @ GitHub
In Python programming, decision-making statements are those that help in deciding the flow of the program.
For example, at times, you might want to execute a block of code only if a particular condition is satisfied. Well, in this case, decision-making statement will be of great help. Let us consider an example to understand this better.
Decision making is required when we want to execute a code only if a certain condition is satisfied.
The key thing to note about Python's control flow statements and program structure is that it uses _indentation_
to mark blocks. Hence the amount of white space (space or tab characters) at the start of a line is very important. This generally helps to make code more readable but can catch out new users of python.
The first thought that would pop up your mind is this - "If the number is divisible by 2, then it is an even number, else it is an odd number". That's absolutely the right logic. But when this has to be coded, you will need the help of decision-making statements. To understand this, let's now look at how they function.
The flow control statements are divided into three categories:
In Python, condition statements act depending on whether a given condition is true or false. You can execute different blocks of codes depending on the outcome of a condition. Condition statements always evaluate to either True
or False
.
There are four types of conditional statements:
In Python, iterative statements allow us to execute a block of code repeatedly as long as the condition is True
. We also call it a loop statements.
Python provides us the following two loop statement to perform some actions repeatedly
In Python, transfer statements are used to alter the program’s way of execution in a certain manner. For this purpose, we use three types of transfer statements.