Day - 24 of DevOps

Day - 24 of DevOps

Python Conditional statements

Hello Everyone,

Welcome to Day 24 of DevOps Learning, Today we explore Python Conditional statements along with examples to help illustrate their usage.

Decision-making is the most important aspect of almost all programming languages. As the name implies, decision-making allows us to run a particular block of code for a particular decision. Here, the decisions are made on the validity of the particular conditions. Condition checking is the backbone of decision-making.

conditional statements in Python allow us to check for certain conditions and perform actions based on the outcome of those checks. There are several types of conditional statements in Python, including:

  • if statement

  • if else statement

  • if elif else statement

  • nested if else statement

Let’s take a look at each of these in more detail.

  1. if statement

The if statement is used to check if a certain condition is true, and if so, execute a specific block of code.

The condition of an 'if' statement can be any valid logical expression that can be either evaluated as true or false.

Here’s an example:

In this example, the if statement checks if the value of the variable age is greater than or equal to 18. If it is, the code inside the if statement is executed, which in this case is simply printing a message to the console.

2. if else statement

The if-else statement provides an else block combined with the if statement which is executed in the false case of the condition.

If the condition is true, then the if-block is executed. Otherwise, the else-block is executed.

Here’s an example:

In this example, the first if statement checks if the value of the variable mark is greater than "35" and the message "Pass" is printed. If it's not, the message "Fail " is printed instead.

3. if elif else statement

The if elif else statement is used to check multiple conditions, and execute a specific block of code based on which condition is true. Here’s an example:

In this example, the first if statement checks if the value of age is greater than or equal to 18. If it is, the message "You are old enough to vote." is printed. If not, the elif statement checks if age is greater than or equal to 16. If it is, the message "You can drive but cannot vote." is printed. If neither of these conditions is true, the else statement executes, and the message "You cannot drive or vote yet." is printed.

4. nested if else statement

if statement can also be checked inside other if statement. This conditional statement is called a nested if statement. This means that the inner if condition will be checked only if the outer if condition is true and by this, we can see multiple conditions to be satisfied.

Here’s an example:

In this example, the first if statement checks if age is greater than or equal to 18. If it is, the nested if statement checks if the value of gender is "male". If it is, the message "You are a male and old enough to vote." is printed. If not, the message "You are a female and old enough.

Example programs:

Answer

Congrats🎉, On Learning the Python conditional statements.

Happy Learning :)