Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Introduction to if-elif-else Statement | Python if-elif-else Statement
Conditional Statements in Python
course content

Course Content

Conditional Statements in Python

Conditional Statements in Python

1. Python if Statement
2. Python if-else Statement
3. Python if-elif-else Statement
4. Python Ternary Operator

bookIntroduction to if-elif-else Statement

Let's begin by examining an example that will aid in a better understanding of the if-elif-else statement.

Here is an example where we check the age for several conditions.

Example 1:

123456789101112
age = 10 if age < 0: print('Age cannot be negative') if age >= 18: print('Adult') if age < 18 and age >= 10: print('Teenager') if age < 10 and age >= 1: print('Child') if age < 1 and age > 0: print('Baby')
copy

If you need to execute one command from several options based on a condition, you can utilize the elif construct.

You can also include an else statement at the end, which will be executed if none of the preceding conditions are satisfied.

Let's consider the following illustration:

So let's rewrite the example above with the new elif statement.

Example 2:

123456789101112
age = 10 if age < 0: print('Age cannot be negative') elif age >= 18: print('Adult') elif age >= 10: print('Teenager') elif age >= 1: print('Child') else: print('Baby')
copy

Clearly, the new operator works smoothly as intended.

Task

Now you can write the program which can determine the shape of the figure on the number of angles. Use the if-elif-else construction to solve this task.

We have the following classification:

  • 0 angles -> Circle or ellipse;
  • 3 angles -> Triangle;
  • 4 angles -> Square, rectangle, or rhombus;
  • more than 4 angles -> Polygon;
  • in other cases -> It is not a geometric figure.

Please, fill in the gaps.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 1
toggle bottom row

bookIntroduction to if-elif-else Statement

Let's begin by examining an example that will aid in a better understanding of the if-elif-else statement.

Here is an example where we check the age for several conditions.

Example 1:

123456789101112
age = 10 if age < 0: print('Age cannot be negative') if age >= 18: print('Adult') if age < 18 and age >= 10: print('Teenager') if age < 10 and age >= 1: print('Child') if age < 1 and age > 0: print('Baby')
copy

If you need to execute one command from several options based on a condition, you can utilize the elif construct.

You can also include an else statement at the end, which will be executed if none of the preceding conditions are satisfied.

Let's consider the following illustration:

So let's rewrite the example above with the new elif statement.

Example 2:

123456789101112
age = 10 if age < 0: print('Age cannot be negative') elif age >= 18: print('Adult') elif age >= 10: print('Teenager') elif age >= 1: print('Child') else: print('Baby')
copy

Clearly, the new operator works smoothly as intended.

Task

Now you can write the program which can determine the shape of the figure on the number of angles. Use the if-elif-else construction to solve this task.

We have the following classification:

  • 0 angles -> Circle or ellipse;
  • 3 angles -> Triangle;
  • 4 angles -> Square, rectangle, or rhombus;
  • more than 4 angles -> Polygon;
  • in other cases -> It is not a geometric figure.

Please, fill in the gaps.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 1
toggle bottom row

bookIntroduction to if-elif-else Statement

Let's begin by examining an example that will aid in a better understanding of the if-elif-else statement.

Here is an example where we check the age for several conditions.

Example 1:

123456789101112
age = 10 if age < 0: print('Age cannot be negative') if age >= 18: print('Adult') if age < 18 and age >= 10: print('Teenager') if age < 10 and age >= 1: print('Child') if age < 1 and age > 0: print('Baby')
copy

If you need to execute one command from several options based on a condition, you can utilize the elif construct.

You can also include an else statement at the end, which will be executed if none of the preceding conditions are satisfied.

Let's consider the following illustration:

So let's rewrite the example above with the new elif statement.

Example 2:

123456789101112
age = 10 if age < 0: print('Age cannot be negative') elif age >= 18: print('Adult') elif age >= 10: print('Teenager') elif age >= 1: print('Child') else: print('Baby')
copy

Clearly, the new operator works smoothly as intended.

Task

Now you can write the program which can determine the shape of the figure on the number of angles. Use the if-elif-else construction to solve this task.

We have the following classification:

  • 0 angles -> Circle or ellipse;
  • 3 angles -> Triangle;
  • 4 angles -> Square, rectangle, or rhombus;
  • more than 4 angles -> Polygon;
  • in other cases -> It is not a geometric figure.

Please, fill in the gaps.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Let's begin by examining an example that will aid in a better understanding of the if-elif-else statement.

Here is an example where we check the age for several conditions.

Example 1:

123456789101112
age = 10 if age < 0: print('Age cannot be negative') if age >= 18: print('Adult') if age < 18 and age >= 10: print('Teenager') if age < 10 and age >= 1: print('Child') if age < 1 and age > 0: print('Baby')
copy

If you need to execute one command from several options based on a condition, you can utilize the elif construct.

You can also include an else statement at the end, which will be executed if none of the preceding conditions are satisfied.

Let's consider the following illustration:

So let's rewrite the example above with the new elif statement.

Example 2:

123456789101112
age = 10 if age < 0: print('Age cannot be negative') elif age >= 18: print('Adult') elif age >= 10: print('Teenager') elif age >= 1: print('Child') else: print('Baby')
copy

Clearly, the new operator works smoothly as intended.

Task

Now you can write the program which can determine the shape of the figure on the number of angles. Use the if-elif-else construction to solve this task.

We have the following classification:

  • 0 angles -> Circle or ellipse;
  • 3 angles -> Triangle;
  • 4 angles -> Square, rectangle, or rhombus;
  • more than 4 angles -> Polygon;
  • in other cases -> It is not a geometric figure.

Please, fill in the gaps.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt