Introduction to If Condition in Python
Programming as we know it is not very different from the laws of life. We always abide by certain rules, and conditions are just one of them. When programming started to get more complex new features came up. Let us suppose we have two numbers. Now initially, when we did programming, we did arithmetic operations on the two numbers to get the addition, subtraction, division, 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 If Condition in Python.
Now, what if we want to ascertain which number is greater than the other? Then we certainly need to incorporate some sort of decision-making capability to 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 important 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 basically 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 Relational Operator
Code:
num1 = 4
num2 = 8
if(num1>num2):
print("Inside if condition")
Nothing will be print 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 hence 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, hence the value of the expression becomes false, and hence the print statement is not executed.
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; hence the print statement is executed.
8. When the condition is False when Not Finding an Object in Array
Code:
array1 = [1.5,"hi",3,True]
if(5 in array1):
print("Inside if condition")
Nothing is printed as the condition becomes false. The expression (5 in array1) becomes False as element 5 is not present in array1; hence the print statement is not executed.
9. When the condition is True when Finding Substring in 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; hence the print statement is executed.
Previously we have mentioned how the colon after the condition and the indentation were very important.
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")
Compile-time error is shown, and when the program is still executed, the following error is shown.
Output:
b. Without Indentation
Code:
if("py" in "python"):
print("Inside if condition")
Output:
Recommended Articles
This is a guide to If Condition in Python. Here we discuss the introduction and working of If Condition in Python along with code implementation. You can also go through our suggested articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses