Course Content
Conditional Statements in Python
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 1:
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')
Example 2:
string_1 = 'Netherlands' string_2 = 'Switzerland' if len(string_1) == len(string_2): print('These lines are the same length.')
Example 3:
if True != False: print('These boolean values are not equal.')
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 theor
operator to combine two or more conditions together.
Fill in the blanks.
Thanks for your feedback!
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 1:
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')
Example 2:
string_1 = 'Netherlands' string_2 = 'Switzerland' if len(string_1) == len(string_2): print('These lines are the same length.')
Example 3:
if True != False: print('These boolean values are not equal.')
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 theor
operator to combine two or more conditions together.
Fill in the blanks.
Thanks for your feedback!
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 1:
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')
Example 2:
string_1 = 'Netherlands' string_2 = 'Switzerland' if len(string_1) == len(string_2): print('These lines are the same length.')
Example 3:
if True != False: print('These boolean values are not equal.')
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 theor
operator to combine two or more conditions together.
Fill in the blanks.
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:
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')
Example 2:
string_1 = 'Netherlands' string_2 = 'Switzerland' if len(string_1) == len(string_2): print('These lines are the same length.')
Example 3:
if True != False: print('These boolean values are not equal.')
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 theor
operator to combine two or more conditions together.
Fill in the blanks.