Conditional Statements

In Python, we can check the conditions by comparing variables or data values. We can compare two variables or data values by using comparison operators:

  • a == b
  • a != b
  • a > b
  • a >= b
  • a < b
  • a <= b

In Python, we have three types of conditional statements:

  1. If statement
  2. If-else statement
  3. Elif statement

If Statement

If statement is a conditional statement in python which is used to check condition using logical operators. If the condition is true, the code is executed.

If-else Statement

If-else statement is a conditional statement in python which is used to check condition using logical operators. If the condition is true, the code int he if block is executed. But if the condition is false, the condition in the else block is executed.

Elif Statement

Elif is basically else-if. It is used to check condition more than one time. In if statement, we were checking condition only one time. On the other hand, in else statement, we were not checking any condition. In elif, we can check multiple conditions.

Using Logical Operators

We can also use logical operators along with conditional statements.

There are three logical operators. They are available not only in Python but also in other languages.

  1. and
  2. or
  3. not

Logical AND Operator

Logical AND is used to check two conditions on the same variable. If the two conditions are true, only then it returns true otherwise false and the code block is not executed.

Logical OR Operator

Logical OR is also used to check two conditions on the same variable. If only one condition is true, it returns true. If both conditions are false, the code block is not executed.

Logical NOT Operator

We use not operator to reverse the result of condition. If the result is true, it returns false. If the result is false, it returns true.

Nested if Statements

Nested if statements are if statements within if statement. If the condition is true, control moves inside the if statement. There another if statement is present and check condition and so on. In this way we can nest as many if statements as we want.

Pass Statement

The pass statement is a placeholder that does nothing. It is used when you need a statement for syntactical reasons, but you don't want to execute any code.

Ternary Operators

A ternary operator also known as a conditional expression is a concise way to write a simple conditional statement that results in a value.

Dangling Else Grammar

The "dangling else" problem occurs when an else statement is associated with the wrong if statement due to improper indentation. However, in Python, the "dangling else" problem is not an issue because of Python's strict indentation rules.

In this example, the else statement is paired with the inner if statement because of the proper indentation. If condition1 is true but condition2 is false, only code block B will be executed.

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

What is the output when x = 5 in: if(x > 5) print A; else print B;

10 XP~3 min
Exercise 2 of 2Easy

The keyword used for alternative condition is ___.

10 XP~2 min