Python Program

Conditional Statements in Python

Conditional Statements in Python
What are Conditional Statements?
Conditional Statements are statements in Python that provide a choice for the control flow based on a condition. It means that the control flow of the Python program will be decided based on the outcome of the condition.
Types of Conditional Statements in Python.
1. If Conditional Statement in Python
If the simple code of block is to be performed if the condition holds then the if statement is used. Here the condition mentioned holds then the code of the block runs otherwise not.
Syntax of If Statement
if condition:
 # Statements to execute if
 # condition is true

 

# if statement example 

if 10 > 5:

print(“10 greater than 5”)

print(“Program ended”)

 

—-Output—-

10 greater than 5

Program ended

 

2. If else Conditional Statements in Python

In a conditional if Statement the additional block of code is merged as an else statement which is performed when if condition is false.

 

Syntax of Python If-Else: 

if (condition):

# Executes this block if

# condition is true

else:

# Executes this block if

# condition is false

 

# if..else statement example 

x = 3

if x == 4:

print(“Yes”)

else:

print(“No”)

 

–Output—-

No

 

3. Nested if..else Conditional Statements in Python

Nested if..else means an if-else statement inside another if statement. Or in simple words first, there is an outer if statement, and inside it another if – else statement is present and such type of statement is known as nested if statement. We can use one if or else if statement inside another if or else if statements.

 

# if..else chain statement 

letter = “A”

if letter == “B”:

print(“letter is B”)

else:

if letter == “C”:

print(“letter is C”)

else:

if letter == “A”:

print(“letter is A”)

else:

print(“letter isn’t A, B and C”)

 

-Output–

letter is A

 

4. If-elif-else Conditional Statements in Python

The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final “else” statement will be executed.

 

# if-elif statement example 

letter = “A”

if letter == “B”:

print(“letter is B”)

elif letter == “C”:

print(“letter is C”)

elif letter == “A”:

print(“letter is A”)

else:

print(“letter isn’t A, B or C”)

 

–Output–

letter is A

 

5. Ternary Expression Conditional Statements in Python

The Python ternary Expression determines if a condition is true or false and then returns the appropriate value in accordance with the result. The ternary Expression is useful in cases where we need to assign a value to a variable based on a simple condition, and we want to keep our code more concise — all in just one line of code.

 

—-Syntax of Ternary Expression—-

Syntax: [on_true] if [expression] else [on_false]

expression: conditional_expression | lambda_expr

 

# Python program to demonstrate nested ternary operator 

a, b = 10, 20

print (“Both a and b are equal” if a == b else “a is greater than b”

if a > b else “b is greater than a”)

 

—-Output—-

b is greater than a

 

Social Media Links of Tech DCode

👉🏻 YouTube

https://www.youtube.com/channel/UCjJnEdeugftBwQ3yMuD4B_A

👉🏻Instagram

https://www.instagram.com/thetechdcode/

👉🏻Facebook Page

https://www.facebook.com/thetechdcode

👉🏻Twitter

👉🏻Telegram Channel

https://t.me/thetechdcode

👉🏻Tech D Code Linktree

https://linktr.ee/thetechdcode

 

Leave a Reply

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