Anirudha
← Work
15 Days of CodingDay 3September 8, 2025~3 min read

If Else, Modulo, Elif

Pay attention to indentation and use :

Things got interesting today with if else logic in Python. If-statements are used in Python to route decisions. Essentially we write 2 statements in the code. One is the if-statement and the second is else statement. If the if-condition we have given is true, only then is the code executed which is stored inside the if-statement. If the condition we have given is false, the else code gets executed which is stored inside the else statement.

The reason Python can differentiate which code is inside which statement is through indentations. This is why, careful attentions needs to be given to checking the indentation of the code. Otherwise, this can lead to unintended results and execution of the wrong piece of code.

We have talked only about if and else statements and the code inside each but what if we have more conditions? This is where the elif-statements come into play. Elif is basically else if. The way we use them is we start writing the code with our if statement, then we can use elif to give as many more conditions as we need. This leads to Python being able to execute the code that we want it to based on the conditions we have set. Another interesting fact about if-statement is that they can be nested inside another if-statement. This leads to even more precise execution.

The next logical question is what are the conditions we can give Python and how we denote them? The answer to this is Comparator Operators and Logical Operators. Comparator operators are used to compare two different values. What this means is we can compare if the two values are equal to each other, if one value is greater than another, and so on. You get it. Logical operators are used to combine two different conditions together. Some examples of logical operators are “and”, “or”, “not”, etc.

Modulo is another Python function (%) which gives us the remainder of a division. This can be used in pretty creative ways with integers. One example where I used it is in deciding if an integer is even or odd. Another way I used it is in creating my coin toss game where a heads or a tails is randomly generated.

For the project today, I designed a Treasure Island game. The goal of the game is to reach the treasure and the only way to do it is by choosing different options that you get throughout the game. My first draft of the game included a lot of if, elif- statements. Easy but long and inefficient. I then tried to use my conditions in a more systematic and logical way which reduced my code from 16 lines to 12 lines. Not a lot of improvement but a win is a win. Take it!