# Write "if" statements

To express conditional logic in Python, you use `if` statements. When you're writing an `if` statement, you're relying on another concept we cover in this module, *mathematical operators*.  Python supports the common logic operators from math: equals, not equals, less than, less than or equal to, greater than, and greater than or equal to. You're probably used to seeing these operators displayed using symbols, which is how they're represented in Python, too.

* Equals: `a == b`
* Not Equals: `a != b`
* Less than: `a < b`
* Less than or equal to: `a <= b`
* Greater than: `a > b`
* Greater than or equal to: `a >= b`

## Test expression

You need to use an `if` statement to run code only `if` a certain condition is satisfied. The first thing you do when you write an `if` statement is to check the condition by using a *test expression.* You then determine whether the statement evaluates to `True` or `False`. If it's `True`, the next indented code block is run:

```python
a = 97
b = 55
#test expression
if a < b
    #statement to be run
    print(b)
```

In this example, `a < b` is the test expression. The program evaluates the test expression and then runs the code within the `if` statement only if the test expression is `True`. If you evaluate the expression, you know that it's `False`, so any code you write in the `if` statement won't be run.&#x20;

{% hint style="info" %}
In Python, `None` and `0` are also interpreted as `False`
{% endhint %}

## Write `if` statement

You use an `if` statement if you want to run code only if a certain condition is satisfied. The syntax of an `if` statement is always:

```python
if test_expression:
    # statement(s) to be run
```

For example:

<pre class="language-python"><code class="lang-python">a = 39
b = 45
<strong>if a >= b:
</strong>    print(a)
</code></pre>

In Python, the body of an if statement must be indented. Any code following a test expression that isn't indented will always be run:

```python
a = 39
b = 45
if a >= b:
    print(a)
print(b)
```

In this example, the output is 45 because the test expression is `False` and the `print(b)` statement isn't indented at the same level as the `if` statement.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.pqtkha.com/programming/technology/python/microsoft-training-python-for-beginners/module-2-use-boolean-logic-in-python/write-if-statements.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
