Python Conditional Statements

What are python conditional statements ?

Python Conditional statements or any conditional statements in programming helps in decision making in the code.

We use the keywords IF, ELIF and ELSE.

Basic Structure

Python conditional statements

From the basic structure, you can see that we evaluate a python expression first, and if the expression yields true, then the IF block gets executed. If the expression yields false, then the ELIF expression is tested. If ELIF fails, then the ELSE block gets executed.

what are ELIF blocks?

ELIF blocks are checked once the IF block expression yields false. If the ELIF condition is true, then the ELIF block code executes if the expression is false, it falls to the else block.

				
					a = 23

if a > 25:
    print("Values is greater than 25")
elif a> 20 and a < 25:
    print("Value is between 20 and 25")
else:
    print("Value is less than 20")
    
				
			

Output:

				
					Value is between 20 and 25

				
			

Nested IF statements

As shown in the example below, we can write IF statements inside IF statements(nesting): based on the expression evaluated to Truth or False in the outer IF expression, the code control is changed. If true, then code control moves into the inner IF condition.

				
					a = 30

if a > 10:
    if a % 2 ==0:
        print("A is even number greater than 10")
    else:
        print("A is an odd number greater than 30")
else:
    print("A is less than 10")
				
			

Output:

				
					A is even number greater than 10

				
			

How to write a single line If else statement?

We can write the single line IF statement in two ways. Using only IF and using IF-ELSE.

Single Line If Statments

From the above example, it is clear.

For a single line IF statement, we have the IF keyword followed by the expression to test and then a full colon, and if the expression yields True, the next statement is executed, which is present alongside.

				
					a = 24

if a%2==0: print("a is even number")
				
			

Output:

				
					a is even number
				
			

The primary statement is written first for a single line IF-ELSE statement, then the IF keyword with the expression, followed by the ELSE keyword with the statement.

				
					a = "APPLE"

# Expression will check if the variable is empty or not
b = True if a else False

print(b)
				
			

Output:

				
					True
				
			

Using OR operator for decision making

Interestingly we can use OR statement to make a quick decision making a hack. The general structure is variable followed by OR keyword followed by another variable.

				
					variable1 or variable2
				
			

If there is any value in the first variable, the OR statement always returns the first variable. The second variable value returns if the first variable is empty.

				
					a =24
b =40

a or b
				
			

Output:

				
					24
				
			
				
					# lets test the second condition
a = ""
b =40

a or b
				
			

Output:

				
					40
				
			

I hope you understood the conditional statements and how to write them in python. Go ahead and look through other articles on the basics of python. You can directly contact me if you need any help with any of the concepts. Thank you!

Share

Facebook
Twitter
Pinterest
LinkedIn