# More on conditionals in Python

#   


Previously, you explored conditional statements and how they’re useful in automating tasks in Python. So far, you’ve focused on the <var>if</var> and <var>else</var> keywords. In this reading, you’ll review these and learn another keyword, <var>elif</var>. You’ll also learn how you can apply the <var>and</var>, <var>or</var>, and <var>not</var> operators to your conditions.

## How conditional statements work

A **conditional statement** is a statement that evaluates code to determine whether it meets a specific set of conditions. When a condition is met, it evaluates to a Boolean value of <var>True</var> and performs specified actions. When the condition isn’t met, it evaluates a Boolean value of <var>False</var> and doesn’t perform the specified actions.

In conditional statements, the condition is often based on a comparison of two values. This table summarizes common comparison operators used to compare numerical values.

<div class="rc-CML" dir="auto" id="bkmrk-operator-use-%3E-great"><div><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml" data-track-page="item_layout" role="presentation"><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml_link" data-track-page="item_layout"><div class="css-1kgqbsw" data-testid="cml-viewer"><div class="css-1yr0py9"><table><thead><tr><th scope="col">**operator**

</th><th scope="col">**use**

</th></tr></thead><tbody><tr><td><var>&gt;</var>

</td><td>greater than

</td></tr><tr><td><var>&lt;</var>

</td><td>less than

</td></tr><tr><td><var>&gt;=</var>

</td><td>greater than or equal to

</td></tr><tr><td><var>&lt;=</var>

</td><td>less than or equal to

</td></tr><tr><td><var>==</var>

</td><td>equal to

</td></tr><tr><td><var>!=</var>

</td><td>not equal to

</td></tr></tbody></table>

</div></div></div></div></div></div>**Note:** The equal to (<var>==</var>) and not equal to (<var>!=</var>) operators are also commonly used to compare string data.

## if statements

The keyword <var>if</var> starts a conditional statement. It’s a necessary component of any conditional statement. In the following example, <var>if</var> begins a statement that tells Python to print an <var>"OK"</var> message when the HTTP response status code equals <var>200</var>:

<var>if status == 200:</var>

<var> print("OK")</var>

This code consists of a header and a body.

### **The header of an if statement**

The first line of this code is the header. In the header of an <var>if</var> statement, the keyword <var>if</var> is followed by the condition. Here, the condition is that the <var>status</var> variable is equal to a value of <var>200</var>. The condition can be placed in parentheses:

<var>if (status == 200):</var>

<var> print("OK")</var>

In cases like this one, placing parentheses around conditions in Python is optional. You might want to include them if it helps you with code readability. However, this condition will be processed the same way if written without parentheses.

In other situations, because Python evaluates the conditions in parentheses first, parentheses can affect how Python processes conditions. You will read more about one of these in the section of this reading on <var>not</var>.

**Note:** You must always place a colon (<var>:</var>) at the end of the header. Without this syntax, the code will produce an error.

**The body of an if statement**

After the header of an <var>if</var> statement comes the body of the <var>if</var> statement. This tells Python what action or actions to perform when the condition evaluates to <var>True</var>. In this example, there is just one action, printing <var>"OK"</var> to the screen. In other cases, there might be more lines of code with additional actions.

**Note:** For the body of the <var>if</var> statement to execute as intended, it must be indented further than the header. Additionally, if there are multiple lines of code within the body, they must all be indented consistently.

## Continuing conditionals with else and elif

In the previous example, if the HTTP status response code was not equal to <var>200</var>, the condition would evaluate to <var>False</var> and Python would continue with the rest of the program. However, it’s also possible to specify alternative actions with <var>else</var> and <var>elif</var>.

### **else statements**

The keyword <var>else</var> precedes a code section that only evaluates when all conditions that precede it within the conditional statement evaluate to <var>False</var>.

In the following example, when the HTTP response status code is not equal to <var>200</var>, it prints an alternative message of <var>"check other status"</var>:

<var>if status == 200:</var>

<var> print("OK")</var>

<var>else:</var>

<var> print("check other status")</var>

**Note:** Like with <var>if</var>, a colon (<var>:</var>) is required after <var>else</var>, and the body that follows the <var>else</var> header is indented.

### **elif statements**

In some cases, you might have multiple alternative actions that depend on new conditions. In that case, you can use <var>elif</var>. The <var>elif</var> keyword precedes a condition that is only evaluated when previous conditions evaluate to <var>False</var>. Unlike with <var>else</var>, there can be multiple <var>elif</var> statements following <var>if</var>.

For example, you might want to print one message if the HTTP response status code is <var>200</var>, one message if it is <var>400</var>, and one if it is <var>500</var>. The following code demonstrates how you can use <var>elif</var> for this:

<var>if status == 200:</var>

<var> print("OK")</var>

<var>elif status == 400:</var>

<var> print("Bad Request")</var>

<var>elif status == 500:</var>

<var> print("Internal Server Error") </var>

Python will first check if the value of <var>status</var> is <var>200</var>, and if this evaluates to <var>False</var>, it will go onto the first <var>elif</var> statement. There, it will check whether the value of <var>status</var> is <var>400</var>. If that evaluates to <var>True</var>, it will print <var>"Bad Request"</var>, but if it evaluates to <var>False</var>, it will go on to the next <var>elif</var> statement.

If you want the code to print another message when all conditions evaluate to <var>False</var>, then you can incorporate <var>else</var> after the last <var>elif</var>. In this example, if it reaches the <var>else</var> statement, it prints a message to check the status:

<var>if status == 200:</var>

<var> print("OK")</var>

<var>elif status == 400:</var>

<var> print("Bad Request")</var>

<var>elif status == 500:</var>

<var> print("Internal Server Error")</var>

<var>else:</var>

<var> print("check other status")</var>

Just like with <var>if</var> and <var>else</var>, it’s important to place a colon (<var>:</var>) after the <var>elif</var> header and indent the code that follows this header.

**Note:** Python processes multiple <var>elif</var> statements differently than multiple <var>if</var> statements. When it reaches an <var>elif</var> statement that evaluates to <var>True</var>, it won’t check the following <var>elif</var> statements. On the other hand, Python will run all <var>if</var> statements.

## Logical operators for multiple conditions

In some cases, you might want Python to perform an action based on a more complex condition. You might require two conditions to evaluate to <var>True</var>. Or, you might require only one of two conditions to evaluate to <var>True</var>. Or, you might want Python to perform an action when a condition evaluates to <var>False</var>. The operators <var>and</var>, <var>or</var>, and <var>not</var> can be used in these cases.

### **and**

The <var>and</var> operator requires both conditions on either side of the operator to evaluate to <var>True</var>. For example, all HTTP status response codes between <var>200</var> and <var>226</var> relate to successful responses. You can use <var>and</var> to join a condition of being greater than or equal to <var>200</var> with another condition of being less than or equal to <var>226</var>:

<var>if status &gt;= 200 and status &lt;= 226:</var>

<var> print("successful response")</var>

When both conditions are <var>True</var>, then the <var>"successful response"</var> message will print.

### **or**

The <var>or</var> operator requires only one of the conditions on either side of the operator to evaluate to <var>True</var>. For example, both a status code of <var>100</var> and a status code of <var>102</var> are informational responses. Using <var>or</var>, you could ask Python to print an <var>"informational response"</var> message when the code is either <var>100</var> or <var>102</var>:

<var>if status == 100 or status == 102:</var>

<var> print("informational response")</var>

Only one of these conditions needs to be met for Python to print the message.

### **not**

The <var>not</var> operator negates a given condition so that it evaluates to <var>False</var> if the condition is <var>True</var> and to <var>True</var> if it is <var>False</var>. For example, if you want to indicate that Python should check the status code when it’s something outside of the successful range, you can use <var>not</var>:

<var>if not(status &gt;= 200 and status &lt;= 226):</var>

<var> print("check status")</var>

Python first checks whether the value of status is greater than or equal to <var>200</var> and less than or equal to <var>226</var>, and then because of the operator <var>not</var>, it inverts this. This means it will print the message if <var>status</var> is less than <var>200</var> or greater than <var>226</var>.

**Note:** In this case, the parentheses are necessary for the code to apply <var>not</var> to both conditions. Python will evaluate the conditions within the parentheses first. This means it will first evaluate the conditions on either side of the <var>and</var> operator and then apply <var>not</var> to both of them.

## Key takeaways

It’s important for security analysts to be familiar with conditional statements. Conditional statements require the <var>if</var> keyword. You can also use <var>else</var> and <var>elif</var> when working with conditionals to specify additional actions to take. The logical operators <var>and</var>, <var>or</var>, and <var>not</var> are also useful when writing conditionals.