Updated June 26, 2023
Introduction to If Statement in Python
‘If statement in Python is an eminent conditional loop statement that can be described as an entry-level conditional loop, where the condition is defined initially before executing the code. Python does not contain an incremental factor in the syntax. It is used for printing or performing a particular operation when the condition in the statement is met. It is used only as the keyword incorporated directly from the statement syntax.
“if” statement works basically on the Boolean conditions “True” & “False.” A given block of code passes when a given “if” condition is True and does not pass or is executed when a given condition is false.
“if” condition can also be used on simple mathematical conditions such as Equal (=), Not Equal (! =), Less than (<), Less than or equal to (<=), Greater than (>) Greater than or equal to (>=).
How If Statement Works?
The “if” statement is primarily used to control our program’s direction. It is used to skip the execution of specific results we don’t intend to execute.
The basic structure of an “if” statement in python is typing the word “if” (lower case) followed by the condition with a colon at the end of the “if” statement and then a print statement regarding printing our desired output.
Python is case sensitive, too, so “if” should be lower case.
Syntax:
if <condition>:
print <statement>
Python is sensitive to indentation; after the “if” condition, the next line of code is spaced four spaces apart from the statement’s start. Any instructions or conditions belonging to the same block of code should be indented. Indentation is unique to the python programming language.
Comparing Python If Statement to Other Languages
In C and Java programming, curly braces are used to identify the “if” statement Block, and any statement or condition outside the braces does not belong to the “if” Block. The statement or operation inside the “if” block is ended with a semi-colon.
if (condition)
{
print statement or operation;
}
Unlike other languages, Python is fairly simple, and indentation makes the code neat and easily understandable.
After a given condition, we can use multiple statements in python. If the condition is true, the following statement or operation is executed, or if there are alternate statements or operations mentioned to execute. If the condition is false, that statement is executed. If no alternate statement or condition is provided to execute, the program will jump to execute the next block of code outside the “if” statement.
Example #1
Code:
if 'cat' in ['dog', 'cat', 'horse', 'penguin']:
print('Cat exists')
print('Cat is my favorite pet')
Output:
In example 1, the “if” condition is true since the cat is present inside the list; hence both the print statement is executed and printed. The whole of example 1 is a single block of code.
Example #2
Code:
if 'horse' in ('dog', 'cat', 'horse', 'penguin'):
print('horse exists')
print('horse is a strong animal')
print('Cat is my favorite pet')
Output:
In example 2, the given condition is true; hence, both print statements were executed. The “if” condition is terminated as soon as indenting back, and hence all the three print statements are executed.
We can also use multiple “if” conditions inside the same block, provided the statements follow indentation.
Example #3
Code:
if 'horse' in ('dog', 'cat', 'horse', 'penguin'):
print('horse exists')
if 'cat' in ('dog', 'cat', 'sheep'):
print('cat exist')
if 'sheep' not in ('dog', 'cat', 'horse', 'penguin'):
print('sheep does not exist')
Output:
Python also has logical “AND”, “OR,” “NOT” operators,
Code:
a = 4
b = 7
if a > 0 and b > 0:
print('Both are Positive numbers')
if a%2 or b%2:
print('Either of one is even')
if a > 0 and not b < 0:
print("Both are positive")
Output:
Flow Chart of If Statement
Examples of If Statements
Here are some of the examples for the “if” statement implemented on different conditional statements.
Example #1
Example using mathematical conditions.
Code:
x = 10
y = 17
if (x > 0):
print("X is positive")
if (x % 2 ==0):
print("X is even")
if (y!=x):
print("Both are unique")
if (y % 2 != 0):
print("y is odd")
if (x>=11):
print("condition is True")
if (y<=19):
print("True")
Output:
The condition ‘x’ greater than or equal to 11 is false; hence respective print statement is not executed.
Popular Course in this category
Python Training Program (40 Courses, 13+ Projects)
Related Courses
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)Angular JS Training Program (9 Courses, 7 Projects)
Example #2
Example using mathematical operators.
Code:
a = 5
b = 10
c = 115
if a + b <= 99:
print('a & b are two digit numbers')
if a + c <= 99:
print('a & c are two digit numbers')
if a > 0:
print(c/a)
if b > 0:
print(c/b)
if c%b == 0:
print("The numbers are divisible")
if c%a == 0:
print("a is divisible by c")
if a < b < c:
print("The sum is", a + b + c)
Output:
For c % b, the remainder is not equal to zero, the condition is false, and hence next line is executed.
Conclusion – If Statement in Python
In general, the “if ” statement in python is used when there is a need to decide which statement or operation needs to be executed and which statements or operations need to be skipped before execution. The execution works on a true or false logic. All mathematical and logical operators can be used in python “if” statements.
Recommended Articles
We hope that this EDUCBA information on “If Statement in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.