> For the complete documentation index, see [llms.txt](https://www.pythonclassroom.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pythonclassroom.com/decisions-if-elif-else/if-elif-else.md).

# if elif else

## Objective

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

* Write Python code using if else statements.

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

## Introductory Problem

Write Python code that asks for a number and assigns the input to the variable num.

Write if statements that print the messages:&#x20;

* num is positive.&#x20;
* num is negative.&#x20;
* num is 0.

## if elif else

Sometimes a problem will have multiple conditional cases.

![](/files/-Ld0oLTI0l1pgSkguO9I)

## Difference between if and if elif else

* Python will evaluate all three if statements to determine if they are true.&#x20;
* Once a condition in the if elif else statement is true, Python stop evaluating the other conditions.&#x20;
* Because of this, if elif else is faster than three if statements.

{% hint style="info" %}
The else is not required.
{% endhint %}

### Example 1

Write Python code that asks what type of gas do you want. The average car holds about 17 gallons of gas. Print the total price based on the type of gas and 15 gallons of gas.

| Type of Gas | Price per galloon |
| ----------- | ----------------- |
| regular     | $3.89             |
| plus        | $4.09             |
| premium     | $4.19             |
| diesel      | $4.59             |

* If the user enters “regular”, print the total price
* If the user enters “plus”, print the total price&#x20;
* If the user enters “premium”, print the total price&#x20;
* If the user enters “diesel”, print the total price&#x20;
* Else if the user enters something else, print invalid gas type

```python
gas = input("What type of gas do you want? ")
if gas == "regular":
    print("Regular Gas:",3.89*17)
elif gas == "plus":
    print("Plus Gas:",4.09*17)
elif gas == "premium":
    print("Premium Gas:",4.19*17)
elif gas == "diesel":
    print("Diesel Gas:",4.59*17)
else:
    print("Invalid Gas Type")
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://www.pythonclassroom.com/decisions-if-elif-else/if-elif-else.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
