Comparison Operators | Python if Statement
Conditional Statements in Python

Course Content

Conditional Statements in Python

Comparison Operators

Now, let's get into the details of what you can actually include within those conditions.

Comparison operators are useful for assessing the values of variables. Their result is always a boolean value, which can be either `True` or `False`.

• `==` equal;

Note

There are two equal signs here because a single equal sign (=) has a completely different meaning. It is used for assignment and cannot (and does not make sense) to be used in if blocks.

• `!=` not equal;
• `>` greater than;
• `<` less than;
• `>=` greater than or equal;
• `<=` less than or equal.

You can compare complex mathematical expressions, elements of different data structures, strings, and even boolean values. Look at the next examples:

Example 3:

We have a variable month which can be a number from 1 to 12. You have tо determine what time of the year this month falls on (winter, spring, summer or autumn). In this task, you have to implement 4 if-statement. Note that we divide the quarters as follows:

• spring 3 <= month < 6, in such case print such text: `It is spring.`;
• summer 6 <= month < 9, in such case print such text: `It is summer.`;
• autumn 9 <= month < 12, in such case print such text: `It is autumn.`;
• winter month = 1 or month = 2 or month = 12, in such case print such text: `It is winter.`.

Note

To include multiple conditions within a single `if` statement, you can use logical operators. You will learn more about these in the upcoming two chapters. For now, just use the `or` operator to combine two or more conditions together.

Fill in the blanks.

Everything was clear?

Section 1. Chapter 4

Comparison Operators

Now, let's get into the details of what you can actually include within those conditions.

Comparison operators are useful for assessing the values of variables. Their result is always a boolean value, which can be either `True` or `False`.

• `==` equal;

Note

There are two equal signs here because a single equal sign (=) has a completely different meaning. It is used for assignment and cannot (and does not make sense) to be used in if blocks.

• `!=` not equal;
• `>` greater than;
• `<` less than;
• `>=` greater than or equal;
• `<=` less than or equal.

You can compare complex mathematical expressions, elements of different data structures, strings, and even boolean values. Look at the next examples:

Example 3:

We have a variable month which can be a number from 1 to 12. You have tо determine what time of the year this month falls on (winter, spring, summer or autumn). In this task, you have to implement 4 if-statement. Note that we divide the quarters as follows:

• spring 3 <= month < 6, in such case print such text: `It is spring.`;
• summer 6 <= month < 9, in such case print such text: `It is summer.`;
• autumn 9 <= month < 12, in such case print such text: `It is autumn.`;
• winter month = 1 or month = 2 or month = 12, in such case print such text: `It is winter.`.

Note

To include multiple conditions within a single `if` statement, you can use logical operators. You will learn more about these in the upcoming two chapters. For now, just use the `or` operator to combine two or more conditions together.

Fill in the blanks.

Everything was clear?

Section 1. Chapter 4
We're sorry to hear that something went wrong. What happened?