conditional-statement-in-python
  • Save
conditional-statement-in-python

Conditional Statements in Python (With Examples)

In this blog, I will tell about conditional statements in Python in a very deep manner. If you are looking for the best blog related to conditional statements in Python, Don’t worry, you came to the right place. So let’s get started with the blog.

Table of Contents

Introduction to Conditional Statements

Conditional statements in Python perform different actions based on whether a specific Boolean constraint evaluates true or false.

Types of Conditional Statements

Conditional Statements are handled by if statements in Python.

A) If statement in Python:

It is used for the decision-making process in the program. It is used when we have some condition in our program.

For example, A person can vote if he/she have age above 18 years, here there is a condition of 18 years which will decide whether the person has to right to vote or not.

Syntax:

## If statement

if condition:
  statement
 
# Conditional statement " if statement"

alex_age = 19

sam_age = 16


1) if alex_age > 18:
    print("Alex has right to vote")

# Output:
Alex has right to vote


2) if sam_age > 18:
    print("Sam has to right to vote")


The above example 1 shows that the condition of the if statement evaluates to be true, and the statement inside the if statement will be executed.

The above example 2 shows that the condition of the if statement evaluates to be false, and the statement inside the if statement will not be executed.

Then how will be false condition will be executed? So here comes the role of the if-else statement in Python

B) If-else statement in Python:

The if-else statement in Python is also used for the decision-making process. In the if statement we can only handle true statements while the if-else statement is also used for handling true or false statements.

If one condition goes wrong, then there should be another condition that should justify the statement or logic.

Syntax:

# if-else condition

if condition:
  statement
else:
  statement

Example:

alex_age = 17

if alex_age > 18:
    print("Alex have the right to vote")
else:
    print("Alex do not have right to vote")


# Output:
Alex do not have right to vote

The above example shows that if the “if statement” does not satisfy the condition then it will skip all the statements inside the if statement and the program move to the else part and execute all the statements inside it.

Sometimes, the else statement does not work in the specific condition. Let me give the example

# Problem with if-else condition

num1 = 32

num2 = 32

if num1 > num2:

    print("num1 is greater than the num2")

else:

    print("num2 is greater than the num1")

# Output:num2 is greater than the num1

The above example returns the wrong output. Because if the “if statement” does not satisfy the condition, it will directly move to the else part without seeing the condition. So, to solve this issue, a condition statement in Python has an elif statement.

C) Elif conditional statement:

To handle the if-else statement condition, we can use the elif statement. By using the “elif” statement, you are telling the program to print out the third condition or possibility when the other condition is not satisfied.

# Problem with if-else condition
num1 = 32
num2 = 32

if num1 > num2:
    print("num1 is greater than the num2")
elif num1 == num2:
    print("num1 and num2 are equal")
else:
    print("num2 is greater than the num1")

# Output:
num1 and num2 are equal

The above example shows how we can solve the problem when we have checked more than one condition.

The elif statement is used when there is more than one condition that needs to be checked.

D) Nested if-else conditional Statement.

Suppose, our first condition is satisfied, and it goes inside the if statement, but in the if statement again we want to check the condition.

So all above the conditional statement in Python does not satisfy these conditions so here come the roles of nested if-else conditions in Python.

# Nested if-else block statement:

print("Enter the Alex marks :")
alex_marks = int(input())

if alex_marks > 80:
    print("Your grade is A")

if alex_marks > 70 and alex_marks <= 80:
    if alex_marks > 70 and alex_marks <= 75:
        print("Your Grade is B+")
    else:
        print("Your Grade is B")

if alex_marks > 60 and alex_marks <= 70:
    if alex_marks > 60 and alex_marks <= 65:
        print("Your Grade is C+")
    else:
        print("Your Grade is C")

if alex_marks > 50 and alex_marks <= 60:
    if alex_marks > 50 and alex_marks <= 55:
        print("Your Grade is D+")
    else:
        print("Your Grade is D")

else:
    print("You are failed")
    
    
# Output-1:
Enter the Alex marks :
55
Your Grade is D+

# Output-2:
Enter the Alex marks :
30
You are failed

Let me explain the above code and tell you what the program says.

In this example, we are taking input as Alex marks. on the basis of that teacher give him grades.

Explaining Output 1:

The program will execute the first statement which means printing the statement.

The next statement will take input from the user, so my input will be 55 marks.

Now the program will check each if statement in the program.

So, the program satisfies the condition of alex_marks > 50 and alex_marks <=60. Now it will go inside that if statement. Now again it will check the if statement inside the if statement which means nested if statement, it satisfies the condition of alex_marks > 50 and alex_marks <= 55 and prints the statement, your Grade is D+.

Explaining Output 2:

The program will execute the first statement which means printing the statement.

The next statement will take input from the user, so my input will be 30 marks.

Now the program will check each if statement in the program.

In these, not even one if statement satisfies the condition so it will directly move to the else statement and print, You are failed.

E) Ternary Operator in Python:

A ternary Operator known as a conditional expression operator evaluates something based on a condition being true or false.

It simply allows testing conditions in a single line instead of writing multiple if-else statements in the program.

# Ternary Operator 

[true_statement] if expression else [false_statement]

Explanation: If the expression evaluates to be true, then the before the if statement will execute, if it is false then the after the else statement will be executed.

# Simple Ternary Operator

a, b = 30, 40

max = b if b > a else b

print(max)

# Output:
40

Ternary Operator in Tuples:

Syntax:

# Ternary Operator in Tuples

(false_statement,true_statement) [condition]

# Ternary operator in tuples

a, b = 30, 40

print((b, a)[a > b])

# Output:

40

It’s a bit confusing that simplify these, so if the condition returns false that means it will return 0 which means it will execute the 0th index. If it is true it will return which means it will return 1 so the index 1 will return.

Above example code, I had placed b at the 0th index and placed an at the 1st index, here the condition will return false because a is less than b. So it will return the 0th index that is b. So the output will be 40.

Ternary Operator in Dictionary:

# Ternary Operator in Tuples

({False: value,True:value}) [condition]
# Ternary operator in dictionary

a, b = 30, 40

print(({False: b, True: a})[a > b])

# Output:
40

It is the same as the ternary operator in tuples. Here if the condition does not satisfy then it will return the False and the False key will be executed, otherwise, it will return the True, and the True key will be executed.

Ternary Operator using Lambda:

# Ternary Operator in Tuples

({lambda: false_value,lambda:true_value}) [condition]()
# Ternary operator using lambda

a, b = 30, 40

print((lambda: b, lambda: a)[a > b]())

# Output:
40

In the above example, there is one key in brackets, which is lambda. If the condition is false, the first key of lambda will be executed; if it is true, it will execute the lambda key of true value.

FAQ

What are the conditional statements in Python?

Conditional statements in Python perform different actions based on whether a specific Boolean constraint evaluates true or false.

What are 3 conditional statements in Python?

1) If Statement
2) If… else statement
3) elif statement

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *