Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

How to Use Conditional Statements in Python?

Last Updated on November 30, 2022 by Prepbytes

Conditional statements in python are used to execute a block of code based on the evolution of a specific constraint is true or false. If the statement is used to perform conditional statements in python. In this article, we will see how to use conditional statements in python.

If conditional statements in Python:

If statement in python is used to make true or false decisions. It executes particular code based on a result of the logical condition. Let’s see what types of logical conditions we can use with the If statement.

  • Equals ( A == B )
  • Not Equals ( A != B )
  • Less than ( A < B )
  • Greater Than ( A > B )
  • Less than or equal to ( A <= B )
  • Greater than or equal to ( A >= B )
  • Logical and ( A and B )
  • Logical or ( A or B )
  • Logical not ( not A )
  • In ( A in B)
  • Not in ( A not in B )

Based on the output of the above python operators true or false, if statement will execute a block of code. Let’s see how the flow chart of the if statement will work.
How to write if conditional statements in python. Syntax of the if statement is given below.

if condition:
    # statement
    # statement
…
    # statement
# following statment

In the above syntax, first, if the condition will be checked if it is true then statements in the block will be run and if the condition is false then statements in the block will be not run. After that, it will directly go to the following statement. Let’s understand this with an example.

A=10
B=5

if A>B:
    print("The condition is true")
print("Following statement 1")

if A

In the above program, we checked the first condition ( A>B ) which is true so if block code will be executed. After that following statement will be executed. The second condition ( A< B ) is false so if the block code will not be executed and direct the following statement will be executed.

If else conditional statements in Python:

We already learned how to use the if statement in python. We already know that if the condition is true then if block will be executed else it will not be executed. Now we add one more block which is the else block. Now, if the if condition will be true then it will execute only the if block, and if the condition is not true then it will execute the else block. The syntax of the if-else conditional statement in python is given below.

if condition:
    #statement
    #statement
    …
    #statement
else:
    #statement
    #statement
    …
    #statement

In the above syntax, first, the if condition will be checked. Now, if the condition is true then the if block will be executed, and if the condition is false then the else block will be executed. Let’s understand the if-else statement with help of an example.

A=10
B=5
# check A is greater than B (True)
if A>B:
    print("If block is executed because the condition is true")
else:
    print("Else block is executed because the condition is false")
print()
# check A is less than B (False)
if A

In the above program, we created two variables A and B. After that first, we checked if A > B, it is true thus the if block will be executed after that we checked A< B which is false so in this case else block will be executed.

If elif conditional statements in Python:-

We already know the use of the if statement. Now, let’s understand how to use elif in python. Elif is used to check another condition and if that condition is true then the elif block will be executed. Thus, first, if condition is checked and if it is false then the elif condition will be checked. Syntax of the if elif statement is given below.

if condition:
    #statement
    #statement
    …
    #statement
elif condition:
    #statement
    #statement
    …
    #statement

In the above syntax, first, the if condition will be checked if it is true then the if block will be executed otherwise elif condition will be checked and if it is true then the elif block will be executed. Let’s understand this with help of an example.

A=10
B=5
if A>B:
    print("A is greater than B")
elif AB:
    print("A is greater than B")
elif A

In the above program, we created two variables A and B. First, we checked if A is greater than B and it is true so in the first case, the if block will be executed. Secondly, we changed the values of A and B, and then we again check if A is greater than B and it is false so now the elif condition will be checked and it is true so in the second case, the elif block will be executed.

If elif else conditional statements in Python:

We already know how to use if, elif, and else all three but now let’s try to use all three together. First of all, if the condition will be checked and if it is true then if block will be executed otherwise elif condition will be checked and if it is true then elif block will be checked otherwise else block will be executed. Syntax of the if elif else statement is given below.

if condition:
    # statement
    # statement
    …
    # statement
elif condition:
    # statement
    # statement
    …
    # statement
else:
    # statement
    # statement
    …
    # statement

Let’s understand how to use the above syntax to implement if elif else conditional statements in python.

A=10
B=5
if A>B:
    print("A is greater than B")
elif AB:
    print("A is greater than B")
elif AB:
    print("A is greater than B")
elif A

In the above example, we created two variable A and B. In the first case, A is greater than B thus the if block will be executed. In the second case, A is less than B thus the elif block will be executed. In the third case, A is equal to B thus else block will be executed.

Conditional Expression in Python:

Conditional Expression in Python is similar to ternary operators in other languages like C++, and Java. The syntax of the conditional expression is given below.

[expression 1] if [condition] else [expression 2]

In the above syntax, if the condition is true then our answer will be expression 1 else our answer will be expression 2. Let’s understand this in more detail with help of an example.

Vote_A=10
Vote_B=17

winner= "Winner A" if Vote_A>Vote_B else "Winner B"
print(winner)

Output:
Winner B

In the above program, we have created two variables Vote_A and Vote_B. If Vote_A is greater than Vote_B then our winner will be A otherwise B.

Leave a Reply

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