Published on

Elif

Authors
  • avatar
    Name
    Jason Deramo
    Twitter

Some decisions in life aren’t simple Yes/No answers (i.e., boolean). Often, we need additional pieces of information to help us navigate the way beyond and through. When we logically think of coding problems, similar scenarios are presented.

elif

AI-Generated (ChatGPT©)

In Python, the elif keyword is used in conditional statements, which allow your programs to execute certain code based on the evaluation of conditions. The elif stands for "else if," and it provides a way to check multiple expressions for truth and executes a code block once the conditions are met.

Basic Structure of if-elif-else

if condition1:
    # code block 1

elif condition2:
    # code block 2

else: 
    # code block 3
  • if condition1 - This checks if condition1 is True. If it is, the program executes code block 1.

  • elif condition2 - If condition1 is not True, the program checks condition2. If condition2 is True, it executes code block 2.

  • else (optional) - If neither condition1 nor condition2 is True, the program defaults to executing code block 3.


When considering the many possible decisions and outcomes you face, know there are often multiple conditions at play. Perhaps you’re struggling with an issue, or trying to understand your purpose in life. Just make the next best choice and leave the rest to God. Give yourself an if (or elif) and make sure to check your syntax.

Source: