Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
if-else and if-else-elif statements | If...Else Statement
Python Tutorial for Beginners
course content

Conteúdo do Curso

Python Tutorial for Beginners

Python Tutorial for Beginners

1. Introduction to Programming
2. Variables in Python
3. Comments
4. If...Else Statement

bookif-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.

12345
number = 157 if number % 2 == 0: print("It is an even number") if number % 2 != 0: print("It is an odd number")
copy

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:

12345
number = 157 if number % 2 == 0: print("It is an even number") else: print("It is an odd number")
copy

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.

1234567
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')
copy

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.

Tarefa

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".

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 3
toggle bottom row

bookif-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.

12345
number = 157 if number % 2 == 0: print("It is an even number") if number % 2 != 0: print("It is an odd number")
copy

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:

12345
number = 157 if number % 2 == 0: print("It is an even number") else: print("It is an odd number")
copy

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.

1234567
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')
copy

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.

Tarefa

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".

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 3
toggle bottom row

bookif-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.

12345
number = 157 if number % 2 == 0: print("It is an even number") if number % 2 != 0: print("It is an odd number")
copy

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:

12345
number = 157 if number % 2 == 0: print("It is an even number") else: print("It is an odd number")
copy

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.

1234567
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')
copy

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.

Tarefa

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".

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu 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.

12345
number = 157 if number % 2 == 0: print("It is an even number") if number % 2 != 0: print("It is an odd number")
copy

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:

12345
number = 157 if number % 2 == 0: print("It is an even number") else: print("It is an odd number")
copy

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.

1234567
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')
copy

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.

Tarefa

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".

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 4. Capítulo 3
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
some-alt