Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Syntax of if-else Statement | Python if-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

book
Syntax of if-else Statement

Let's briefly discuss the syntax of the if-else construct.

  • The syntax of the if statement remains unchanged;
  • Write the else keyword. Note that it is case sensitive, so it is not the same as Else so if you use Else you will get an error because it is not correct;
  • Furthermore, you don't need to specify a condition when using else since it inherently implies the opposite condition of the if part;
  • Simply use a colon;
  • Then, proceed with code indentation for the instructions executed within the else-block. These instructions are executed if our condition in the if statement evaluates to False.

Practice Time!

Task
test

Swipe to show code editor

You have to find the greatest number of three numbers using an if-else statement.

Fill in the blanks.

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 2. Chapter 2
toggle bottom row

book
Syntax of if-else Statement

Let's briefly discuss the syntax of the if-else construct.

  • The syntax of the if statement remains unchanged;
  • Write the else keyword. Note that it is case sensitive, so it is not the same as Else so if you use Else you will get an error because it is not correct;
  • Furthermore, you don't need to specify a condition when using else since it inherently implies the opposite condition of the if part;
  • Simply use a colon;
  • Then, proceed with code indentation for the instructions executed within the else-block. These instructions are executed if our condition in the if statement evaluates to False.

Practice Time!

Task
test

Swipe to show code editor

You have to find the greatest number of three numbers using an if-else statement.

Fill in the blanks.

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 2. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt