Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Comparison Operators | Python if Statement
Conditional Statements in Python
course content

Course Content

Conditional Statements in Python

Conditional Statements in Python

1. Python if Statement
2. Python if-else Statement
3. Python if-elif-else Statement
4. Python Ternary Operator

bookComparison 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 1:

12345678
import math #BMI = Weight(in kg) / Height^2 (in meters) weight = 65 height = 1.70 if weight / math.pow(height, 2) <= 24.9: print('BMI: Healthy Weight')
copy

Example 2:

1234
string_1 = 'Netherlands' string_2 = 'Switzerland' if len(string_1) == len(string_2): print('These lines are the same length.')
copy

Example 3:

12
if True != False: print('These boolean values are not equal.')
copy
Task
test

Swipe to show code editor

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 4
toggle bottom row

bookComparison 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 1:

12345678
import math #BMI = Weight(in kg) / Height^2 (in meters) weight = 65 height = 1.70 if weight / math.pow(height, 2) <= 24.9: print('BMI: Healthy Weight')
copy

Example 2:

1234
string_1 = 'Netherlands' string_2 = 'Switzerland' if len(string_1) == len(string_2): print('These lines are the same length.')
copy

Example 3:

12
if True != False: print('These boolean values are not equal.')
copy
Task
test

Swipe to show code editor

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 4
toggle bottom row

bookComparison 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 1:

12345678
import math #BMI = Weight(in kg) / Height^2 (in meters) weight = 65 height = 1.70 if weight / math.pow(height, 2) <= 24.9: print('BMI: Healthy Weight')
copy

Example 2:

1234
string_1 = 'Netherlands' string_2 = 'Switzerland' if len(string_1) == len(string_2): print('These lines are the same length.')
copy

Example 3:

12
if True != False: print('These boolean values are not equal.')
copy
Task
test

Swipe to show code editor

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

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 1:

12345678
import math #BMI = Weight(in kg) / Height^2 (in meters) weight = 65 height = 1.70 if weight / math.pow(height, 2) <= 24.9: print('BMI: Healthy Weight')
copy

Example 2:

1234
string_1 = 'Netherlands' string_2 = 'Switzerland' if len(string_1) == len(string_2): print('These lines are the same length.')
copy

Example 3:

12
if True != False: print('These boolean values are not equal.')
copy
Task
test

Swipe to show code editor

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt