python tip

Replacing nested ifs with if/continue

Consider the following example: we want to loop through each element in a 3 by 3 matrix, which is done with two "for loops". If the element satisfies a certain condition, we’ll perform some action with or on the numbers. Although this example is way too basic, writing an if statement in this manner makes your code a lot less readable. This is especially the case if there are already many indents before and after so it’s safe to say that it is almost always better to reduce the amount of necessary indentation in our code. If there is only a need to include an if statement but not an elif or else component, we can write the code another way. If the condition is not true, we call the continue keyword, which will skip anything left in the iteration and then continues to the next. This method is very helpful in making complex code more readable!!!