Python Concepts/If Statement

Objective

 * Learn about Python's indentation syntax.
 * Learn about the conditional statement.
 * Learn about the use of semi-colons and backslashes in Python.
 * Learn about two dependent statements,  and.

Indentations
Unlike many other computer languages, like C++ and java, Python uses indentation to determine blocks of code. The following pseudocode below will help demonstrate. This is some code. This is some more code. This code is part of a code block. And so is this! And this!!! This code isn't part of the code block. =( But this code is part of a new code block!  And this code is, as well!

As we can see, there can be multiple blocks of code. You can also contain blocks of code within another block of code, so long as it indents. There isn't a limit on the indent, but it must be kept constant or you'll get an error. This is some code. This is a block of code indented very far away. This is completely legal in Python, although not encouraged. The preferred indentation is in multiples of four. This isn't a block of code; it follows no indentation style. This would cause an error!

Although you're allowed to use white spaces and tabs, it's preferred that you use white spaces. Also, you cannot mix both white spaces and tabs together or you'll get an error.

You should know that since we'll be using the Python Interpreter to compute the example code,  is exactly the same as , except that it means you're in a code block. The below code will demonstrate.

Now that we got this out of the way, we can continue on with the main part of the lesson.

The If Statement
In most, if not all programming languages, there are special ways to control the flow of code. Such control allows certain parts of code to execute if the conditions are right. This flow control is done by using statements and code blocks. The most common and widely used statement is the  statement. It will compute if a statement is true or false. If it's true, it will execute the code. If it's false, it won't execute the code.

Remember that  and   are Booleans in Python. This means that  and other conditional statements will use Boolean math to compute their Boolean state. You should also note the need for a colon at the end of the   statement. This is needed at the end of a control flow statement.

You can also mix several conditions together by using the previously taught Boolean operators;,  ,.

As stated at the beginning of the lesson, you can nest statements within a code block to begin a new code block, so long as they follow their respective indentations.

Although you can nest statements within each other, it can become hard to manage. It would be more Pythonic to keep statements flat. This means instead of nesting four  statements together to do something, it would be better to use one   statement with several   operators.

Semi-colons, Backslashes, and Parentheses
You're probably going to want to keep several lines of code on just one line. This can be accomplished by using the semicolon. The semicolon acts like a newline, though the programmer won't see it like that. Take the following code as an example.

This same code can be condensed into one line, like the example below.

The great thing about using semicolons is the need not to indent. Although their use seems great, semicolons should be used sparingly. As such, you shouldn't use them with large or long multi-line blocks of code. This can make it difficult and confusing to manage. Take a look at two bad examples.

Now think about another dilemma. Imagine that you need to compare six numbers in an  statement. Such an  statement could become long and you don't want to make it worse by nesting the separate parts in six separate   statements. A backslash can help solve this problem. A backslash allows you to break up one long piece of code into several parts.

Like the semicolon, you don't need to worry about indentation following the. This allows you to keep code level with the first part of your  statement. Watch out though, since any character after the backslash will cause an error. This even means excess whitespaces will cause an error. You'll need to be extra careful when working with backslashes.

A more workable solution is to use parentheses to enclose all of the code. Use of parentheses works like the backslash, but it allows for extra characters at the end of an incomplete logical line, including whitespaces.

The Else Statement
There will be many times when you want to execute code in the event that the  statement isn't true. This would require the use of the  statement. This statement will execute when the  statement is false. It needs to be on the same indentation level as the  statement.

Although the  and   need to be aligned with each other, the indentation of their code blocks can be different as the example below demonstrates.

Although there isn't a requirement for it, it's recommended that you indent every four spaces.

The  statement can be used to make code more readable. If you want the code to act when a condition is not true, you can negate the condition:

or you can leave the condition as is and act on the  statement:

The Elif Statement
Like a similar problem before, you might need to do something that might seem tedious. Say you wanted four  statements together, but you only wanted one to execute. You could try to write those for statements, but it would be easier writing one big one using the  statement. stands for "else if", which acts like an  and. This means that if the initial  isn't true, it will go to the   and see if it's true. This can come in handy later on. The  statement is one of the ways by which Python implements a   or   statement. The following simple example illustrates the equivalent of a  statement:

Assignments

 * Play around with the Python Interpreter. Mix and match different  statements to get a general feel on how they work.
 * PEP 8 is an official document adopted by the Python Software Foundation to maintain readability within parts of Python. Skim through the document and get a general understanding on how Python code can be readable and maintainable at the same time.


 * Previous Lesson: Dictionaries
 * Next Lesson: Match-case Statement
 * Course Home Page