Using nesting in Python

In my previous blog, I explained a lot about using if, elif and else in Python. In this blog I am going further on that subject and we are going to use nesting in these conditions.
Nesting is actually using conditions in conditions. We are going to use the code that we wrote in my previous blog and we are going to nest in that code, so bringing conditions in the already made conditions. You can find that full code here.

So we are going to add some conditions in the first condition (if). First we are going to erase the print(smaller than 5). Now we are going to add some conditions, we are going to split the conditions in positive, zero and negative. So first we are going to add an if conditions, because you always start with if and we say if the input is bigger than 0 (positive). After that, we are going to make an elif condition and we state if the input is equal to zero. The last one is again the else condition, here we don’t state anything anymore. Because, it is always negative if it doesn’t satisfy the other two conditions. So finally we get:

As you can see is only the first statement divided. The other ones are still on themselves. Sometimes it is easier to use nested loops if you want to make a division in one of the subcategories that you have. If you are writing a very long and complex code, it becomes also possible that it is less time consuming to calculate. Your code will namely not always take the inner loop into account if the outer loop condition, that triggers the inner loop, is not satisfied.

Hopefully you enjoyed reading this blog and see you with my next blog again!

Plaats een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit /  Bijwerken )

Google photo

Je reageert onder je Google account. Log uit /  Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit /  Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit /  Bijwerken )

Verbinden met %s

Maak je website op WordPress.com
Aan de slag
%d bloggers liken dit: