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

Contenido del Curso

Python Tutorial for Beginners

Python Tutorial for Beginners

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

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.

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.

Tarea

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

Tarea

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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 4. Capítulo 3
toggle bottom row

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.

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.

Tarea

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

Tarea

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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 4. Capítulo 3
toggle bottom row

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.

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.

Tarea

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

Tarea

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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

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.

Tarea

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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 4. Capítulo 3
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt