Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Simple if/else Expressions | Conditional Statements
Introduction to Python

Simple if/else Expressions

Now that we're acquainted with conditional operators and know how to use them, we can branch our code based on certain conditions. In Python, this is achieved with the following structure:

In the example above, the condition can be either a conditional statement or a boolean value. For example, it might look like a != 4 or b > 100. The commands beneath if and else are instructions to be executed depending on whether the condition is met. These could be print() functions, variable assignments, arithmetic operations, string manipulations, and more.

Let's walk through an example: Suppose you have a string and you want to determine if it's long. We'll define a string as 'long' if it contains more than 20 characters.

Note

Remember, the code blocks under the if and else statements should have consistent indentation (e.g., 2 spaces, 4 spaces, etc.).

Given the provided if/else block, what will be the output if a = 95, and what if a = 56?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 3. Capítulo 6
course content

Contenido del Curso

Introduction to Python

Simple if/else Expressions

Now that we're acquainted with conditional operators and know how to use them, we can branch our code based on certain conditions. In Python, this is achieved with the following structure:

In the example above, the condition can be either a conditional statement or a boolean value. For example, it might look like a != 4 or b > 100. The commands beneath if and else are instructions to be executed depending on whether the condition is met. These could be print() functions, variable assignments, arithmetic operations, string manipulations, and more.

Let's walk through an example: Suppose you have a string and you want to determine if it's long. We'll define a string as 'long' if it contains more than 20 characters.

Note

Remember, the code blocks under the if and else statements should have consistent indentation (e.g., 2 spaces, 4 spaces, etc.).

Given the provided if/else block, what will be the output if a = 95, and what if a = 56?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 3. Capítulo 6
some-alt