Course Content
Python Tutorial for Beginners
Python Tutorial for Beginners
if-else and if-else-elif statements
The expression if, which we used before, worked like this: if the condition is true, then something will happen, if the condition turns out to be wrong, nothing will happen.
But suppose you want something to happen regardless of whether the condition is false or true. Let's look at an example.
number = 157 if number % 2 == 0: print("It is an even number") if number % 2 != 0: print("It is an odd number")
In this example, we have two if statements, one to check if the number is even and the other if the number is odd. Thus, all cases are covered, depending on the value of the variable number.
The code works, and it covers the cases we need, but it's more wordy than it needs to be, and it's a little crazy. Therefore, it can be written more concisely and not crazy. The following example shows this:
number = 157 if number % 2 == 0: print("It is an even number") else: print("It is an odd number")
If the test passes - that is, if the number is even - the first message is displayed. If the check fails and the number is not even, a second message is displayed.
Be attentive:
- The else keyword is written on a separate line and a colon at the end;
- Statements that execute in the else case are indented as in the if case.
It is worth recalling one more statement - elif. It's not hard to guess what this is an abbreviation for else if. If no test succeeds yet, elif tries something else.
donut_price = 100 if donut_price > 100: print('These donuts are expensive') elif donut_price < 100: print('These donuts are cheap') else: print('These donuts are at the standard price')
The code assigns the variable buy_score an initial value of 100. It then performs three tests to check the value of the price, and depending on it draws conclusions, whether expensive donuts or vice versa.
Let's move on to practice to assimilate knowledge.
Task
You have to implement such a program: If a equals b, display "ok". If not, then if c equals d, display "ok". If both tests fail, display "failed".
Thanks for your feedback!
if-else and if-else-elif statements
The expression if, which we used before, worked like this: if the condition is true, then something will happen, if the condition turns out to be wrong, nothing will happen.
But suppose you want something to happen regardless of whether the condition is false or true. Let's look at an example.
number = 157 if number % 2 == 0: print("It is an even number") if number % 2 != 0: print("It is an odd number")
In this example, we have two if statements, one to check if the number is even and the other if the number is odd. Thus, all cases are covered, depending on the value of the variable number.
The code works, and it covers the cases we need, but it's more wordy than it needs to be, and it's a little crazy. Therefore, it can be written more concisely and not crazy. The following example shows this:
number = 157 if number % 2 == 0: print("It is an even number") else: print("It is an odd number")
If the test passes - that is, if the number is even - the first message is displayed. If the check fails and the number is not even, a second message is displayed.
Be attentive:
- The else keyword is written on a separate line and a colon at the end;
- Statements that execute in the else case are indented as in the if case.
It is worth recalling one more statement - elif. It's not hard to guess what this is an abbreviation for else if. If no test succeeds yet, elif tries something else.
donut_price = 100 if donut_price > 100: print('These donuts are expensive') elif donut_price < 100: print('These donuts are cheap') else: print('These donuts are at the standard price')
The code assigns the variable buy_score an initial value of 100. It then performs three tests to check the value of the price, and depending on it draws conclusions, whether expensive donuts or vice versa.
Let's move on to practice to assimilate knowledge.
Task
You have to implement such a program: If a equals b, display "ok". If not, then if c equals d, display "ok". If both tests fail, display "failed".
Thanks for your feedback!
if-else and if-else-elif statements
The expression if, which we used before, worked like this: if the condition is true, then something will happen, if the condition turns out to be wrong, nothing will happen.
But suppose you want something to happen regardless of whether the condition is false or true. Let's look at an example.
number = 157 if number % 2 == 0: print("It is an even number") if number % 2 != 0: print("It is an odd number")
In this example, we have two if statements, one to check if the number is even and the other if the number is odd. Thus, all cases are covered, depending on the value of the variable number.
The code works, and it covers the cases we need, but it's more wordy than it needs to be, and it's a little crazy. Therefore, it can be written more concisely and not crazy. The following example shows this:
number = 157 if number % 2 == 0: print("It is an even number") else: print("It is an odd number")
If the test passes - that is, if the number is even - the first message is displayed. If the check fails and the number is not even, a second message is displayed.
Be attentive:
- The else keyword is written on a separate line and a colon at the end;
- Statements that execute in the else case are indented as in the if case.
It is worth recalling one more statement - elif. It's not hard to guess what this is an abbreviation for else if. If no test succeeds yet, elif tries something else.
donut_price = 100 if donut_price > 100: print('These donuts are expensive') elif donut_price < 100: print('These donuts are cheap') else: print('These donuts are at the standard price')
The code assigns the variable buy_score an initial value of 100. It then performs three tests to check the value of the price, and depending on it draws conclusions, whether expensive donuts or vice versa.
Let's move on to practice to assimilate knowledge.
Task
You have to implement such a program: If a equals b, display "ok". If not, then if c equals d, display "ok". If both tests fail, display "failed".
Thanks for your feedback!
The expression if, which we used before, worked like this: if the condition is true, then something will happen, if the condition turns out to be wrong, nothing will happen.
But suppose you want something to happen regardless of whether the condition is false or true. Let's look at an example.
number = 157 if number % 2 == 0: print("It is an even number") if number % 2 != 0: print("It is an odd number")
In this example, we have two if statements, one to check if the number is even and the other if the number is odd. Thus, all cases are covered, depending on the value of the variable number.
The code works, and it covers the cases we need, but it's more wordy than it needs to be, and it's a little crazy. Therefore, it can be written more concisely and not crazy. The following example shows this:
number = 157 if number % 2 == 0: print("It is an even number") else: print("It is an odd number")
If the test passes - that is, if the number is even - the first message is displayed. If the check fails and the number is not even, a second message is displayed.
Be attentive:
- The else keyword is written on a separate line and a colon at the end;
- Statements that execute in the else case are indented as in the if case.
It is worth recalling one more statement - elif. It's not hard to guess what this is an abbreviation for else if. If no test succeeds yet, elif tries something else.
donut_price = 100 if donut_price > 100: print('These donuts are expensive') elif donut_price < 100: print('These donuts are cheap') else: print('These donuts are at the standard price')
The code assigns the variable buy_score an initial value of 100. It then performs three tests to check the value of the price, and depending on it draws conclusions, whether expensive donuts or vice versa.
Let's move on to practice to assimilate knowledge.
Task
You have to implement such a program: If a equals b, display "ok". If not, then if c equals d, display "ok". If both tests fail, display "failed".