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 portion of the code. Unlike the ‘if’ statements in other object-oriented programming languages, 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 ‘if’ statement is met, which is used only ‘if’ 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 in controlling the direction of our program. It is used in skipping the execution of certain results that 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 in 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 set of instructions or conditions that belongs to the same block of code should be indented. Indentation is unique to the python programming language. Python strictly adheres to indentation; it is developed that way to make the lines of code neat and easily readable.
4.8 (14,153 ratings)
View Course
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;
}
When compared to other languages, Python is fairly simple, and indentation makes the code neat and understandable easily.
After a given “if” condition, we can use multiple “if” statements and else statements in python. The condition is true the following statement or operation is executed, or if there is alternate statements or operations mention to execute if the condition is false, then that statement inside the “if” block is executed or if there is no alternate statement or condition provided to execute when the condition is false then the program will simply 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, and hence both the 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:
If Statement Flow Chart
Syntax of If Statements
Here some of the syntaxes for the “if” statement that is 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.
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 needed to skip 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
This is a guide to If Statement in Python. Here we discuss how the statement works, syntax, flowchart, comparison between python if statement and other languages, and different examples and code implementation. You may also look at the following articles to learn more-