Ola pythonista's, so this is a follow
up of the previous post which was on the introduction of python. Here, I’m
going to talk about Conditional execution and iterations in python. Hope you
would find it interesting to browse.
There are many conditional constructs
in python, and the first one we are going to see is the unidirectional
branching statement, the prodigal construct: “if”.
An if statement precedes with a
condition that is checked and is recouped to a boolean expression and if it’s
results to true then a set of statement are executed otherwise they’re skipped
and the instructions after the construct body is executed.
Boolean expressions in python use
the following relational operators:
Indentation is an important
prospect of python programming language as the language doesn’t use the usual
flower braces to represent a construct body as in other high-level languages as
C++ or Java and hence an increase indent after conditional or looping statement
indicate the body of the construct and the indent is maintained to indicate the
scope of the block. To get back to level of the conditional or iteration
statement we reduce the indent to indicate the end of the block.
Two-way decisions are possible in
python by using if-else construct wherein if the condition evaluates to true a
set of statement are executed otherwise the body in the else construct is
executed and finally executed the rest of the instructions after the
conditional construct. Below is a snippet that depict two-way decision.
//flowchart and snippet
Mutli-branching decision is
possible by using an if-elif-else construct, where in if the condition
evaluates to true executes a set of statements and skips execution of the rest
of the conditional construct otherwise evaluate the next condition that
precedes the elif statement and if that evaluates the condition to true then a
set of instructions are executed otherwise executes the statements in the else
body.
This is the end of conditional
construct in python and before we can get to python iterations let’s discuss
about how to handle dangerous sections of code that raises an error that can be
maskable i.e. for which we can write a piece of code that acts when a set of
statements causes a traceback and tries to solve or bring the program out of
the troubleshoot problem else the program can terminate if not handled. This is
possible in python by use of the try/except structure. You surround a dangerous
section of code with try and except. If the code in the try works - the except
is skipped. If the code in the try fails - it jumps to the except section.
Now let’s understand python iterations
which is last topic of this post. What are iterations?? They’re repetition of a set of statements. They can
be categorized into two kinds – definite and indefinite.
While
loops are called “indefinite loops” because they keep going until a logical
condition becomes false. Sometimes it is a little harder to be sure if a loop
of such a kind will terminate or not. A while loop is written in a following
manner, its identified by the reserved word while and follows with a condition
that evaluates to a Boolean value and only if it’s true that the control flow
enter the loop body and hence the while loop is also called entry controlled
loop.
For
loops are called “definite loops” because they execute the loop body for exact
number of times. We can write a loop to run the loop once for each of the items
in a set using the Python for construct. Definite loops (for loops) have
explicit iteration variables that change each time through a loop. These iteration variables move through the
sequence or set. The iteration variable “iterates” through the sequence
(ordered set). The block (body) of code is executed once for each value in the
sequence. The iteration variable moves through all of the values in the
sequence.
The
“is” and “is not” operators in python that are used in logical expression. The
“is” operator implies is the same as but have more weight than the logical
operator “==”. The “is not” operator logically is opposite of the “is”
operator.
So here
we are, at the end of the post, let’s summarise the topics we have seen today.
(……..) This concludes the post, stay tuned for the next post on the topic of functions
and string operations, until then guys enjoy your time browsing through other
posts in the blog!!
I like your blog, I read this blog please update more content on hacking, further check it once at python online course
ReplyDeleteHi, thanks for your blog, if you want to learn about programming languages like java, php, android app, embedded system etc. I think this training institute is the best one.
ReplyDeletebest python training in coimbatore
Android training in coimbatore
Networking training in coimbatore