The if statement is used to check whether a particular condition is true or not, then it executes the block of the specific code known as if-block.
The condition of the if statement can be any logical expression that is valid and can be evaluated as true or false.
if < expression>:
< statement>
NOTE- In Python, indentation is used to declare a block of code inside a statement.
If two statements are at the same indentation level, they are part of the same code block. Four spaces are given to indent the statements, which are the most commonly used indentation in the python programming language.
Python makes use of the ‘if’ statement for implementing decision control. Decision-making plays a crucial role in almost all programming languages, and as its name implies, it allows running a particular block for a given decision. Decision-making here is based on the validity of a given condition, and condition checking acts as a backbone for decision making.
After the ‘if’ keyword, you must place a Boolean expression that evaluates true or False. Then, you must use the ‘:’ symbol and press ‘Enter’ for starting a block with an increased indent. All the statements written at the same indentation level will execute if the expression evaluates to True. Further, to end the block, you must decrease the indentation to the previous one. The following sets of statements are executed after the statements in the ‘if’ block.
Nested if statements allow using If-else inside an outer ‘if’ statement.
Knowledge2life
|