Updated June 17, 2023
Introduction to If Condition in Python
Programming as we know it is not very different from the laws of life. We always abide by specific rules, and conditions are just one of them. As programming became more complex, new features emerged. Let us suppose we have two numbers. Initially, when we did programming, we did arithmetic operations on the two numbers to get the addition, subtraction, division, and multiplication with the help of Arithmetic Operators in the desired choice of your programming language. We will start by looking at the basic structure of the If Condition in Python.
What if we want to ascertain which number is greater than the other? Then we certainly need to incorporate some decision-making capability into the program in the form of conditional statements and with the help of Relational Operators.
Syntax:
if <expr>:
<statement>
Let us look into the parameters of the syntax, as shown below.
- The most critical component is the “if” keyword, which helps us identify an expression to be a conditional statement.
- expr: This signifies the condition, the fulfillment of which would execute the below statement. The expr is a Python statement that results in a boolean value. (True or False). The statement for the particular expr will get executed only if the Boolean value for the expr is True.
- statement: This is the final part of the if-statement, which is the path along which the program must flow if the expr is True.
Working of If Condition in Python with Examples
Let us show some examples of writing if-statement in Python with an explanation of their working:
1. When the condition is True using the Relational Operator
Code:
num1 = 4
num2 = 8
if(num1<num2):
print("Inside if condition")
Output:
The condition num1(4) < num2(8) being true, the value of the expression becomes True, and hence the print statement is executed.
2. When the condition is False, using the Relational Operator
Code:
num1 = 4
num2 = 8
if(num1>num2):
print("Inside if condition")
Nothing will be printed as the condition becomes false. The condition num1(4) > num2(8) being false, the value of the expression becomes false, and hence the print statement is not executed.
3. When the condition is True, Passing Non-Zero Value in Condition
Code:
num1 = 1
num2 = 0
if(num1):
print("Inside if condition")
Output:
num1 is non-zero, i.e., 1; hence, the expression’s value becomes True, and the print statement is executed.
4. When the condition is False, Passing Zero or None Value in Condition
Code:
num1 = 1
num2 = 0
if(num2):
print("Inside if condition")
Nothing is printed as the condition becomes false. Num2 is zero, i.e., 0; the print statement does not get executed because the expression is false.
5. When a condition is True using OR Logical Operator
Code:
num1 = 1
num2 = 0
if(num1 or num2):
print("Inside if condition")
Output:
The expression (num1 or num2) becomes True; hence the print statement is executed.
6. When the condition is False, using AND Logical Operator
Code:
num1 = 1
num2 = 0
if(num1 and num2):
print("Inside if condition")
Nothing is printed as the condition becomes false. The expression (num1 and num2) becomes False; hence the print statement is not executed.
7. When the condition is True when Finding an Object in an Array
Code:
array1 = [1.5,"hi",3,True]
if("hi" in array1):
print("Inside if condition")
Output:
The expression (“hi” in array1) becomes True as the element “hi” is present in array1; therefore, the program executes the print statement.
8. When the condition is False when Not Finding an Object in the Array
Code:
array1 = [1.5,"hi",3,True]
if(5 in array1):
print("Inside if condition")
When the condition is false, the program does not print anything. The expression (5 in array1) becomes False as element 5 is not present in array1; as a result, the print statement does not get executed.
9. When the condition is True when Finding a Substring in a String
Code:
if("py" in "python"):
print("Inside if condition")
Output:
The expression (“py” in “python”) becomes True as the element “hi” is present in array1; thus, the program executes the print statement.
Previously, we mentioned how the Colon and Indentation were essential after the condition.
Let us see an example of what happens when we do away with them.
a. Without Colon
Code:
if("py" in "python")
print("Inside if condition")
The compiler displays a compile-time error, and the program exhibits the following error during execution.
Output:
b. Without Indentation
Code:
if("py" in "python"):
print("Inside if condition")
Output:
Recommended Articles
This is a guide to the If Condition in Python. Here we discuss the introduction and working of If Condition in Python and code implementation. You can also go through our suggested articles to learn more –