Tutorial on if, else and else-if (elif) conditions
Based on the sources and our conversation, the if
statement is a fundamental and powerful tool in Python programming used for decision making or selecting from alternative actions based on test results. It is considered the primary selection tool in Python and represents much of the logic a Python program possesses. The if
statement evaluates expressions that produce TRUE
or FALSE
outcomes, and based on this outcome, the program determines which statements to execute.
The concept of if
is part of a core triangle of powerful programming entities, along with for
and while
, which are crucial for understanding programming completely. The if
statement is used in almost every piece of code and is found in 99 percent of code written.
Here’s a breakdown of the if
statement in Python as described in the sources:
Basic
if
Statement- The simplest form consists of the word
if
, followed by an expression (the condition) that is interpreted as a true or false result. - The header line ends with a colon (
:
). - A block or suite of code follows the header. This block contains the statement(s) to run if the test is true.
- All statements within this block must be indented the same amount. Indentation is how Python groups statements into blocks; there are no braces or “begin/end” delimiters like in C-like languages.
- If the boolean expression evaluates to
TRUE
, the block of statements is executed. If it’sFALSE
, nothing associated with thatif
block is executed, and the program continues after theif
statement’s block. - There must be at least one statement in the body, or you can use the
pass
statement as a placeholder.pass
is a no-operation placeholder used when syntax requires a statement but you have nothing to do. - Example syntax:
if expression: statement(s) # indented block
- The simplest form consists of the word
Alternative Execution (
else
)- The
if
statement can be followed by an optionalelse
statement. - The
else
statement contains a block of code that executes if the conditional expression in theif
statement resolves toFALSE
. - The
else
part also has an associated block of nested statements, indented under theelse:
header. - Exactly one of the alternatives (
if
block orelse
block) will run. - There can be at most one
else
statement following anif
. - Example syntax:
if expression: statement(s) # Executed if expression is TRUE else: statement(s) # Executed if expression is FALSE
- An example given is checking if a number is even or odd or calculating a discount based on an amount.
- The
Chained Conditionals (
elif
)- When there are more than two possibilities, you can use one or more optional
elif
(“else if”) tests. - The
elif
statement allows you to check another condition if the previousif
orelif
conditions were false. - Each
elif
also has an associated indented block of statements. - The conditions (
if
, thenelif
s) are checked in order from top to bottom. Python executes the block of code associated with the first test that evaluates to true. - If one condition is true, its corresponding block runs, and the entire
if
/elif
/else
statement ends. - If all
if
andelif
tests are false, the optionalelse
block (if present) is executed. - There is no limit on the number of
elif
statements. - This structure is the most straightforward way to code a multiway branch. Python does not have a
switch
orcase
statement like some other languages; multiway branching is typically done withif
/elif
/else
or by using dictionaries. - Using
elif
can make code cleaner compared to nestedif
statements, especially when checking ranges of values (like for grading based on marks). - Example syntax:
if <test1>: <statements1> # if test, associated block elif <test2>: <statements2> # Optional elif, associated block # ... more elifs ... else: <statements3> # Optional else, associated block
- An example shows checking a variable
x
against different values (‘roger’, ‘bugs’) usingif
andelif
.
- When there are more than two possibilities, you can use one or more optional
Truth Tests and Boolean Values
if
statements rely on truth tests or Boolean expressions.- Python interprets any non-zero and non-null values as
TRUE
, and any zero or null values asFALSE
. For example, a non-empty string or list isTrue
, while an empty string (""
), empty list ([]
), empty dictionary ({}
), or the valueNone
isFalse
. A non-zero number (like1
) isTrue
, and0.0
isFalse
. - This means you can test an object directly (
if X:
) instead of comparing it to an empty value (if X != '':
). - The built-in values
True
andFalse
are essentially predefined to have the same meanings as integer 1 and 0, respectively. - Comparison operators (
>
,>=
,<
,<=
,==
,!=
) are used to create boolean expressions. They returnTrue
orFalse
. - Logical operators (
and
,or
,not
) are used to combine boolean expressions. In Python, these are typed as words.and
andor
perform short-circuit evaluation. Foror
, Python returns the first true operand it finds; forand
, it returns the first false operand. Thenot
operator negates a boolean expression.
Nested Conditionals
- You can write an
if
statement within anotherif
statement, or inside anotherif...elif...else
construct. - When code is conditional or repeated, you simply indent it further to the right.
- An example shows an
if
statement nested in theelse
clause of anotherif
statement. While possible, deeply nested conditionals can become difficult to read.
- You can write an
Single Statement Suites
- If the block of an
if
clause is only a single line, it can sometimes go on the same line as the header statement after the colon. Example:if var == 100 : print ("Value of expression is 100")
. This saves an extra line.
- If the block of an
Conditional Expression (Ternary Operator)
- For simple cases, Python (since 2.5) offers a concise way to express a simple
if/else
that returns or assigns a value. - The syntax is
Y if X else Z
. It evaluates toY
ifX
is true, andZ
otherwise. - It has the same effect as a four-line
if
statement (if X: A = Y else: A = Z
) but is simpler to code for basic scenarios. - Like
and
andor
, it short-circuits, evaluating only the necessary expression (Y
orZ
). - It can be used to simplify code, such as in recursive functions or handling default values.
- The sources mention an older
and/or
combination that achieved a similar effect but was trickier and less preferred. TheY if X else Z
form is recommended.
- For simple cases, Python (since 2.5) offers a concise way to express a simple
In summary, the if
statement and its variations (else
and elif
) are fundamental control-flow statements in Python that allow programs to execute different code paths based on conditions evaluated as true or false. Indentation is critical for defining the blocks of code associated with each part of the statement.