LEARN PYTHON NOW! Book: The Pillars of Python. 14 — Execution Flow (Loops and Conditionals)



“Action is the fundamental key to all success.” Pablo Picasso.

Before we start talking about the loops or loops “for” and “while” and the condominiums (if), we must introduce the execution flow.

I mean, how does the python interpreter read our lines of code?

The interpreter starts reading the first line from the top left to right and continues down to the bottom.

I mean, it would have a shape like this:

However, we can modify it with the flow control tools (for, while, if and functions).

The if, or conditional, allows you to add a branch in the code flow by adding an if something happens then something happens. To write an if statement, simply type the following:

age=16
if age <18: 
print ("You are under 18" )
else: 
print ("You are over 18")

When the interpreter arrives at if, a fork in the flow occurs. If the next line of code is True then what is below the if is executed. Note that in python, unlike other programming languages like C++ or javascript, you simply write the line of the conditional and if it is True then run the following one, which are tabbed.

Graphically, it could be represented as follows.

To end with conditionals, if we want to keep throwing conditionals, we can do it with elif.

age=14
if age <18: 
print ("You are under 18" )
elif 18< age <20: 
print("The age is between 18 and 20")

elif 20< age<25:
print ("The age is between 20 and 25")

else:
print ("You are over 25)

There is another way to manage flow control and that is to have the code repeated several times. This is what is known within programming languages as a loop. Loops can be written in python as for or while.

For we will use it when we know the number of times we are going to repeat the code, for example:

for i in range(10): 
print ("hello")

In a graphic way we would see it that way:

In case we don’t know the number of times we are going to iterate over certain parts of the code we can simply use while:

i=0
while i<10:
print ("hello")
i +=1

When we use while, we don’t have to forget to initialize the variable at the beginning and increase it later within the same while.
The third way to manipulate the flow control is with the functions.
The functions are used to encapsulate lines of code in just one word in order to reuse it and clean the code. The most important thing to understand is that when the interpreter reads the term “def”, which is the one used to create the functions or as it is said in programming (“define a function”) he automatically understands that what follows is a function and remembers that in that position there was a function called X. For example.

def saludo(): 
print ("Hello")
saludo()

graphically it would be like this:

The most important of the functions are 2 things:
1- The ability to write parameters within parentheses.

An example of the use of parameters:

def suma(a,b): 
print ("Sum is:", a+b)

suma(10,2)# 'Sum is:', 12

As we can see by declaring the variable we have also written within the parentheses two parameters, which is total sum is a+b =12 in this case. In the next chapter we will talk about something very important as the scope and that has a lot to do with the use of functions.

2. The term return. Which is used to make the function return something when you call it. For example, using the example we just gave:

def suma(a,b): 
return (a+b)

print (suma(10,2))#'Sum is:', 12

Although it may seem the same, the biggest difference is that if we use the term return we can save the result in a variable and we can get more than one result. For example:

def operation(word): 
a=10
b=4
 if word==1: 
return a+b
 elif word==2: 
return a-b
 elif word==3: 
return a*b
 elif word==4: 
return a/b
 else: 
return "Please type any of the options: 1,2,3,4"

a=input("select one of the following options:1,2,3,4 ===> ")
operation(a)

In this last example, what the interpreter does is read the function first and then ask for an input (which means that the user writes with his tenclado”) and saves it in the variable a. It then calls the operation function and passes the variable a to it as the parameter.

So much for the three ways of manipulating the execution flow in programming. In the next lesson we will look at the scope.

— — — — — — — — — — — — — — The End — — — — — — — — — — — — —

If you like this small and free magazine you can help us by simply sharing it or subscribing to the publication. My name is Rubén Ruiz and I work in Artificial Intelligence in the financial industry and as a personal project I run this little magazine where we experiment with Artificial Intelligence… until the computer explodes :) You can follow me on:

Instagram (Personal life, it’s fun) => @rubenruiz_t

Youtube (Channel about AI, try to make it fun )=> Rubén Ruiz A.I.

Github (Where I upload my code, this is not so much fun anymore) => RubenRuizT

Source: Deep Learning on Medium