Control flow in Python

A python program have crowd of function judgements accompanied by cluster of statements. Control flow is the sequence where program’s code accomplish. Python interpreter reads everything from top to bottom whatever statements we give it. Python breaks down the function instead of achieving the function. If this is not a definition, python will try to execute it.

We will further elaborate this concept using an example:-

Presume, I’m getting ready for a holiday picnic. While I’m arranging everything, I had a question about whether to carry a lunch box or not. It can also depend on the time I’m going to spend and the place I’m visiting. If I carried the lunch box, my bag will be too heavy but if I didn’t carry it and I’ll depend on the condition that you will have some food where you are visiting, might be some cafe or anything. But there’s a chance of risk.

This kind of execution which differs the order in which statements are completed is called as Control Flow.

There are 2 Fundamental parts here- 1. Conditional execution 2. Repeated execution

Conditional Execution

Conditions which governs the statements are known as conditional statements.

In python, this statements are written in ‘if’ and ‘else’ statements. It returns a value in ‘true’ and ‘false’, i.e. Boolean form. Rest of the conditions are checked depending on the first condition.

>>> if condition:                                            ...   statement_1 #Execute conditionally                      ...   statement_2 #Execute conditionally                       ... statement_3 #Execute unconditionally

   File "<stdin>", line 4                                 statement_3 #Execute unconditionally     //output//                                                                                                        SyntaxError: invalid syntanx

Indentation delimits body of if statements. If you have any block of code, its your choice of giving any number of spaces but that should should be equal everywhere. We don’t use curly or normal braces in if and else statements,  just use ‘:’ colon.

We take if condition when the condition is True and else condition if condition is False.

From the above example, we can say that:- If the tiffin is in the bag use ‘if’ statement, if the tiffin isn’t there then use ‘else’ statement.

>>> if m%n != 0:                                              ...   (m, n) = (n, m%n)                                         ... else:                                                     ...   gcd = n

Python also allows some different types of things like lists and numbers. In general, any numerical expression returning value ‘0’ is False and ‘1’ for True.

Python has a shortcut, sometimes we have to check multiple conditions then we can combine this else and if statement into second check ‘elif’, which just avoid long verification.

Repeated execution or loops

When we want to execute something repeated number of time, or when we want to keep our program running we use Looping which executes repeated actions. We take a new name, pass through sequence of values and executes the given expression.

We have 2 statements to perform this type of executions, namely – -‘for’ -‘while’. This both have same rules as we have for ‘if’ and ‘else’ statements. Both statements looks similar but have various use cases.

  • ‘while’ loop is used when you have ‘if’ condition and your statements comes true but still you want to search until it becomes false, but you have to ensure that the sequence of statements to execute must make things false, otherwise the loop will never terminate.
>>> x = 5                                                     While x <= 10          print(“hi”)                                                      x += 1

# x +=1 is used because when the value comes false i.e.,  value becomes 11 then while loop will terminate. Python doesn’t support x++, so instead we use x += .

>>> x = 5                                                       While x <= 10     print(“hi”)                                                      x += 1 else:                                                                print(“bye”)
  • ‘for’ loop is used more like a iterator. It can be used over a sequences like lists, tuple, set, string.
List = [‘tina’, 1111, qwer, ‘eric’, 2222, tyui]                for i in list:                          
print(i)                                                      tina                                                                    1111                                                            qwer                                                           eric                                                                2222                              //output//                                                                                                                                               

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s