Course Content
Conditional Statements in Python
Conditional Statements in Python
Introduction to if-else Statement
Let's look at some examples first.
Example 1:
Checking a number for negativity.
number = 15 if number >= 0: print('this number is positive:', number) if number < 0: print('this number is negative', number)
Example 2:
Checking whether a chocolate bar is expensive or has an average price.
price_of_chocolate = 5 if price_of_chocolate <= 3: print('This a chocolate bar has an average price.') if price_of_chocolate > 3: print('This a chocolate bar is expensive.')
We can observe that to address various situations, it's necessary to write if
-statements multiple times.
However, to adhere to the DRY ("Don't Repeat Yourself", one of the principles of good code) principle and maintain conciseness, we can make use of the else keyword.
Let's rewrite the example above and simplify them.
Example 3:
number = 15 if number >= 0: print(f'Number {number} is positive.') else: print(f'Number {number} is negative.')
Example 4:
price_of_chocolate = 5 if price_of_chocolate <= 3: print('This chocolate bar has an average price.') else: print('This chocolate bar is expensive.')
For a clearer understanding of the if-else
expression's functionality, refer to the diagram below.
Time to consolidate your knowledge.
Task
Let's write a program that takes 3 numbers as input, these will be the sides of a triangle, and our program will check and say whether it is really possible to build a triangle from these three segments, or not.
Recall the conditions that the segments must satisfy in order to be able to construct a triangle :either side of an arbitrary triangle is less than the sum of its other two sides. x + y > z
and x + z > y
and y + z > x
.
Thanks for your feedback!
Introduction to if-else Statement
Let's look at some examples first.
Example 1:
Checking a number for negativity.
number = 15 if number >= 0: print('this number is positive:', number) if number < 0: print('this number is negative', number)
Example 2:
Checking whether a chocolate bar is expensive or has an average price.
price_of_chocolate = 5 if price_of_chocolate <= 3: print('This a chocolate bar has an average price.') if price_of_chocolate > 3: print('This a chocolate bar is expensive.')
We can observe that to address various situations, it's necessary to write if
-statements multiple times.
However, to adhere to the DRY ("Don't Repeat Yourself", one of the principles of good code) principle and maintain conciseness, we can make use of the else keyword.
Let's rewrite the example above and simplify them.
Example 3:
number = 15 if number >= 0: print(f'Number {number} is positive.') else: print(f'Number {number} is negative.')
Example 4:
price_of_chocolate = 5 if price_of_chocolate <= 3: print('This chocolate bar has an average price.') else: print('This chocolate bar is expensive.')
For a clearer understanding of the if-else
expression's functionality, refer to the diagram below.
Time to consolidate your knowledge.
Task
Let's write a program that takes 3 numbers as input, these will be the sides of a triangle, and our program will check and say whether it is really possible to build a triangle from these three segments, or not.
Recall the conditions that the segments must satisfy in order to be able to construct a triangle :either side of an arbitrary triangle is less than the sum of its other two sides. x + y > z
and x + z > y
and y + z > x
.
Thanks for your feedback!
Introduction to if-else Statement
Let's look at some examples first.
Example 1:
Checking a number for negativity.
number = 15 if number >= 0: print('this number is positive:', number) if number < 0: print('this number is negative', number)
Example 2:
Checking whether a chocolate bar is expensive or has an average price.
price_of_chocolate = 5 if price_of_chocolate <= 3: print('This a chocolate bar has an average price.') if price_of_chocolate > 3: print('This a chocolate bar is expensive.')
We can observe that to address various situations, it's necessary to write if
-statements multiple times.
However, to adhere to the DRY ("Don't Repeat Yourself", one of the principles of good code) principle and maintain conciseness, we can make use of the else keyword.
Let's rewrite the example above and simplify them.
Example 3:
number = 15 if number >= 0: print(f'Number {number} is positive.') else: print(f'Number {number} is negative.')
Example 4:
price_of_chocolate = 5 if price_of_chocolate <= 3: print('This chocolate bar has an average price.') else: print('This chocolate bar is expensive.')
For a clearer understanding of the if-else
expression's functionality, refer to the diagram below.
Time to consolidate your knowledge.
Task
Let's write a program that takes 3 numbers as input, these will be the sides of a triangle, and our program will check and say whether it is really possible to build a triangle from these three segments, or not.
Recall the conditions that the segments must satisfy in order to be able to construct a triangle :either side of an arbitrary triangle is less than the sum of its other two sides. x + y > z
and x + z > y
and y + z > x
.
Thanks for your feedback!
Let's look at some examples first.
Example 1:
Checking a number for negativity.
number = 15 if number >= 0: print('this number is positive:', number) if number < 0: print('this number is negative', number)
Example 2:
Checking whether a chocolate bar is expensive or has an average price.
price_of_chocolate = 5 if price_of_chocolate <= 3: print('This a chocolate bar has an average price.') if price_of_chocolate > 3: print('This a chocolate bar is expensive.')
We can observe that to address various situations, it's necessary to write if
-statements multiple times.
However, to adhere to the DRY ("Don't Repeat Yourself", one of the principles of good code) principle and maintain conciseness, we can make use of the else keyword.
Let's rewrite the example above and simplify them.
Example 3:
number = 15 if number >= 0: print(f'Number {number} is positive.') else: print(f'Number {number} is negative.')
Example 4:
price_of_chocolate = 5 if price_of_chocolate <= 3: print('This chocolate bar has an average price.') else: print('This chocolate bar is expensive.')
For a clearer understanding of the if-else
expression's functionality, refer to the diagram below.
Time to consolidate your knowledge.
Task
Let's write a program that takes 3 numbers as input, these will be the sides of a triangle, and our program will check and say whether it is really possible to build a triangle from these three segments, or not.
Recall the conditions that the segments must satisfy in order to be able to construct a triangle :either side of an arbitrary triangle is less than the sum of its other two sides. x + y > z
and x + z > y
and y + z > x
.