Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
If/else expressions with multiple conditions (1/2) | Conditional statements
Learn Python from Scratch
course content

Contenido del Curso

Learn Python from Scratch

Learn Python from Scratch

1. The basics
2. Arithmetic operations
3. Common data types
4. Conditional statements
5. Other data types
6. Loops
7. Functions

bookIf/else expressions with multiple conditions (1/2)

In the last example, you might notice that we didn't consider the case when revenue is exactly 2000. In that case, we suffer no losses, but at the same time have no profit. Surely there should be a way to modify our code to predict such things.

If you want to check one more condition after you've done another one, use elif. It looks like follows:

123456
if condition1: do something elif condition2: do another else: do nothing
copy

A piece of code after the last else will be executed only when all the conditions above don't hold.

For example, remember our last example with string length. Let's modify it a bit and now let's call string large if it has more than 20 symbols, medium - if more than 10 (but surely less than 20), and small otherwise.

123456789101112131415161718192021
# assign some medium string test = "medium string" # conditional statements if len(test) > 20: print("String: '", test, "' is large") elif len(test) > 10: print("String: '", test, "' is medium") else: print("String: '", test, "' is small") # one more checking test = "small" # conditional statement if len(test) > 20: print("String: '", test, "' is large") elif len(test) > 10: print("String: '", test, "' is medium") else: print("String: '", test, "' is small")
copy

Tarea

Modify your code (from exercise about the small store) so if revenue will be exactly 2000 you will receive the message "We are in balance".

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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

bookIf/else expressions with multiple conditions (1/2)

In the last example, you might notice that we didn't consider the case when revenue is exactly 2000. In that case, we suffer no losses, but at the same time have no profit. Surely there should be a way to modify our code to predict such things.

If you want to check one more condition after you've done another one, use elif. It looks like follows:

123456
if condition1: do something elif condition2: do another else: do nothing
copy

A piece of code after the last else will be executed only when all the conditions above don't hold.

For example, remember our last example with string length. Let's modify it a bit and now let's call string large if it has more than 20 symbols, medium - if more than 10 (but surely less than 20), and small otherwise.

123456789101112131415161718192021
# assign some medium string test = "medium string" # conditional statements if len(test) > 20: print("String: '", test, "' is large") elif len(test) > 10: print("String: '", test, "' is medium") else: print("String: '", test, "' is small") # one more checking test = "small" # conditional statement if len(test) > 20: print("String: '", test, "' is large") elif len(test) > 10: print("String: '", test, "' is medium") else: print("String: '", test, "' is small")
copy

Tarea

Modify your code (from exercise about the small store) so if revenue will be exactly 2000 you will receive the message "We are in balance".

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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

bookIf/else expressions with multiple conditions (1/2)

In the last example, you might notice that we didn't consider the case when revenue is exactly 2000. In that case, we suffer no losses, but at the same time have no profit. Surely there should be a way to modify our code to predict such things.

If you want to check one more condition after you've done another one, use elif. It looks like follows:

123456
if condition1: do something elif condition2: do another else: do nothing
copy

A piece of code after the last else will be executed only when all the conditions above don't hold.

For example, remember our last example with string length. Let's modify it a bit and now let's call string large if it has more than 20 symbols, medium - if more than 10 (but surely less than 20), and small otherwise.

123456789101112131415161718192021
# assign some medium string test = "medium string" # conditional statements if len(test) > 20: print("String: '", test, "' is large") elif len(test) > 10: print("String: '", test, "' is medium") else: print("String: '", test, "' is small") # one more checking test = "small" # conditional statement if len(test) > 20: print("String: '", test, "' is large") elif len(test) > 10: print("String: '", test, "' is medium") else: print("String: '", test, "' is small")
copy

Tarea

Modify your code (from exercise about the small store) so if revenue will be exactly 2000 you will receive the message "We are in balance".

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

In the last example, you might notice that we didn't consider the case when revenue is exactly 2000. In that case, we suffer no losses, but at the same time have no profit. Surely there should be a way to modify our code to predict such things.

If you want to check one more condition after you've done another one, use elif. It looks like follows:

123456
if condition1: do something elif condition2: do another else: do nothing
copy

A piece of code after the last else will be executed only when all the conditions above don't hold.

For example, remember our last example with string length. Let's modify it a bit and now let's call string large if it has more than 20 symbols, medium - if more than 10 (but surely less than 20), and small otherwise.

123456789101112131415161718192021
# assign some medium string test = "medium string" # conditional statements if len(test) > 20: print("String: '", test, "' is large") elif len(test) > 10: print("String: '", test, "' is medium") else: print("String: '", test, "' is small") # one more checking test = "small" # conditional statement if len(test) > 20: print("String: '", test, "' is large") elif len(test) > 10: print("String: '", test, "' is medium") else: print("String: '", test, "' is small")
copy

Tarea

Modify your code (from exercise about the small store) so if revenue will be exactly 2000 you will receive the message "We are in balance".

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 4. Capítulo 5
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt