# if

## Objective

After working through this lesson, you’ll be able to

* Write Python code using if statements.

{% embed url="<https://docs.google.com/presentation/d/1LVjHcUGZEipeqSxhIaeU_VREnpGd_uXT_RGQnqqr9nU/edit?usp=sharing>" %}

## Introductory Problem

Write Python code that asks:

* How many aunts do you have?
  * Assign the input to the variable aunts
* How many uncles do you have?
  * Assign the input to the variable uncles

{% code title="intro.py" %}

```python
aunts = int(input("How many aunts do you have? "))
uncles = int(input("How many uncles do you have? "))
print("I have",aunts,"aunts and",uncles,"uncles")
```

{% endcode %}

## if

The if conditional statement can be True or False.  When the if statement executes, the condition is tested and if it is true, the indented block of code is executed.  Otherwise, it is skipped.

![](/files/-Ld0pLRjMku32D6REBdp)

{% hint style="info" %}
Indentation is Python's method for grouping statements.
{% endhint %}

## Examples

### Example 1

Ask the user for a number.  Write if statements to determine.

* if the number is positive.
  * Print positive
* if the number negative
  * Print negative
* if the number is 0
  * Print is zero.

{% code title="" %}

```python
num = int(input("Give me a number: "))
if num > 0:
    print(num, "is positive")
if num < 0:
    print(num, "is negative")
if num == 0:
    print(num, "is zero")
```

{% endcode %}

### Example 2

Ask the user if it is hot today. &#x20;

* If the user enters "Yes", print "The summer is always hot!"
* If the user enters "No", print "It's time to move!".

{% code title="" %}

```python
hot = input("Is it hot today? ")
if hot == "Yes":
    print("It's always hot!")
if hot == "No":
    print("It's always cold!")
```

{% endcode %}

### Example 3

Ask the user how much money they have.

* If the user enters 99.99 or more, print "You're rich!"
* If the user enters less than 99.99, print "You're not rich!"

```python
money = float(input("How much money do you have? "))
if money >= 99.99:
    print("You're rich!")
if money < 99.99:
    print("You're not rich!")
```

### Example 4

Using the code from the Introductory Problem

* If the user has more aunts than uncles
  * Print "You have more aunts than uncles"
* If the user has more uncles than aunts
  * Print "You have more uncles than aunts"
* If the user has the same number of aunts and uncles
  * Print "You have the same number of aunts and uncles
* If the user does not have the same number of aunts and uncles
  * Print "You do not have the same number of aunts and uncles

```python
uncles = int(input("How many uncles do you have? "))
aunts = int(input("How many aunts do you have? "))
if aunts > uncles:
    print("You have more aunts than uncles")
if aunts < uncles:
    print("You have more uncles than aunts")
if aunts == uncles:
    print("You have the same number of aunts and uncles")
if aunts != uncles:
    print("You do not have the same number of aunts and uncles")
```


---

# 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://www.pythonclassroom.com/decisions-if-elif-else/if.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.
