blog

Home / DeveloperSection / Blogs / What are conditional statement in python and how to use them?

What are conditional statement in python and how to use them?

What are conditional statement in python and how to use them?

Anonymous User 97 15-Mar-2024

There are multiple programming languages and Python is one of the most famous useful languages in the programming world. Conditional statements are the main building block of any programming language where the flow of your code can be controlled based on input conditions. In Python, these conditional statements consist of different forms of `if`, `else`, `elif`, and nested versions, enabling complex use of the paths within your programs. They enable developers to design programs that are responsive and dynamic by controlling the execution routes in accordance with various input conditions and scenarios.

 

What are Conditional Statements?

 

Conditional statements in Python allow you to run a particular set of code if the given conditions are true-false. These conditions are typically described through the use of logical expressions that provide a True-or-False answer.

 

The `if` Statement

 

In Python, the "if" statement is the most used conditional command. It enables you to run a given code fragment when a condition, which has been specified earlier, is determined to be `True`. The syntax for the `if` statement is as follows:

 

if condition:
#Condition met: perform code block

When the condition evaluates to be ``True``, the code block indented below the ```if``` statement is executed. Otherwise, it is skipped.

Let’s see one example using an “if” statement:

num = 5
if num > 0:
    print("The number is positive.")

Here, the message "The number is positive" is printed on the computer screen if the variable `num` is greater than zero.

 

The `else` Statement

 

The `else` statement runs in an `if` statement, then it lets you take an alternative path if the `if` condition is `False`.

if condition:
# Code block to execute if the condition is True
else:
# Code block to execute if condition is False

 

The statement `if` the condition evaluates to `True`, and the code block indented below the `if` statement is performed. Or, if the statement is passed `else` then the block of code indented below it will be executed.

Let’s see one example using the “else” statement:

num = -5
if num > 0:
   print("The number is positive.")
else:
   print("The number is negative.")

In the case of the condition being `num > 0`, the output will be "The number is positive". Otherwise, the message "The number is negative." will be living the statement.

 

The “elif” statement

 

The `elif` (short for "else if") conditional statement comes after the initial `if` statement to check multiple conditions in numerical or alphabetical orders depending on the situation. It is useful when you have to compare different conditions in order to decide which one is true so that the following code block can be executed. The syntax for the `elif` statement is as follows:

if condition1:
# Conditional code block which executes if condition 1 is True
elif condition2:
# Code executable if condition1 were not True and condition2 were True
elif condition3:
# If condition1, and condition2 are False, and condition3 is True,  the following code should be executed.
else:
#conditional statement if all conditions are True

The `elif` statement can be used more than once for the evaluation of extra criteria.

 

Example of using the `elif` Statement

score = 85
if score >= 90:
   grade = "A"
elif score >= 80:
   grade = "B"
elif score >= 70:
   grade = "C"
elif score >= 60:
   grade = "D"
else:
   grade = "F"
print("Your grade is:", grade)

In the  example, the presence of the `elif` statement makes it possible to test multiple conditions one after the other, and the numerical score is translated into a letter grade.

 

Nested Conditional Statements

 

The ‘If’ statement is a parent of the’ else ‘ statement. This gives more opportunities for intriguing tasks when decisional processes must be surveyed and attention is paid to multiple conditions. Nested conditionals can contain `if`, `else`, and `elif` statements within these statements, respectively.

Example: Conditionals statements using nesting

num = 10
if num > 5:
   print("Bigger than 5")
if num <= 15:
   print("Between 5 and 15")

Here, in this line of code, the first `if` statement is located inside another `if` statement. The second `if` statement (greater than) is evaluated only if the precondition of the first `if` statement (equal to) is satisfied.

 

Conclusion

 

Conditional statements in Python allow responsible use of flow control of your code through the use of prescribed instances. They enable you to develop agile programs that can be responsive relying on provisions. Through commands on conditional capabilities, your Python programs can do complicated and various tasks which is really important to achieve efficiency and effectiveness in programming. Regardless of the purpose for which you construct programs from simple scripts to complex applications your ability to properly and effectively use of conditional statements is the key to developing clean, readable, and maintainable code.


I am a content writter !

Leave Comment

Comments

Liked By