If-Else Statements In Python

Hi there. This page will go over if-else statements in Python. It is split into three sections:

  1. Relational Operators
  2. The If Statement
  3. The If-Else Statement

Reference: Starting Out With Python – Second Edition By Tony Gaddis


Relational Operators

This section will show how relational operators work in Python with an example. The relational operators being mentioned are:

  • greater than: >
  • less than: <
  • greater than or equal to: ≥
  • less than or equal to: ≤
  • equal to: ==
  • not equal to: !=



The If Statement

The if statement is a decision structure which runs a certain section of code when a condition is met.




In this example, the user is asked to guess my favourite number? If the answer is 8, the user receives a message saying that their answer is correct.


If-Else Statements

In the if statement, nothing really happens when the condition in the if part is met. With if-else statements, code can be written when the if condition is not met from the if-else statement.

The if-else statement format in Python is as follows:





Leave a Reply