THE IF STATEMENT OF PYTHON
The if statement is a conditional statement in python and these implement selection constructs ( decision constructs )
An if statement tests a particular condition ; evaluates to true , a course of action is followed i.e., the – course – of action is ignored .
Before we proceed further it is important to know about the relational and logical operators you can find the relational and logical operator in our previous blog named operators , relational and logical operators will help you to learn about the form of conditionals in this topic .
In all forms of if statement , we are using true to refer the boolean value true as well as truth value collectively ; similarly , false here will refer to boolean value false and truth value collectively .
THE if STATEMENT
The simplest form of if statement tests a condition and if the condition evaluates to true , it carries out some instructions and does nothing in case condition evaluates to false .The if statement is a compound statement and its syntax ( general form ) is as shown below :
If < conditional expression > :
Statement } > the statement inside an if are indented at same level
Where a statement may consist of single statement , a compound statement , or just the pass statement ( in case of empty statement ) . for instance , we
consider the following code –
If ch == ‘ ’ :
In this –
Single quotes ( ‘ ’ ) are conditional expression
Colon ( : ) are the header of if statement ; please notice the colon ( : ) at the end
Spaces + = 1
Chars + = 1
In these 2 lines are the body of if . please notice that all the statement in the if body are indented at same level .
NOTE – INDENTATION IN PYTHON IS VERY IMPORTANT
In an if statement , if the conditional expression evaluates to true , the statements in the body – of – if are executed , otherwise ignored .
The above code will check whether the character variable ch stores a space or not ; if it does then the condition ch == ‘ ’ evaluates to true .
IN THE NEXT BLOG WE LEARN SOME PROGRAMS ON IF STATEMENT AND ANOTHER IMPORTANT STATEMENT IF – ELSE STATEMENT
THANK YOU FOR VISITING
KEEP VISITING
KEEP LEARNING
STAY TUNED FOR NEXT BLOG
VIEW OUR PREVIOUS BLOG FOR THE BASIC CONCEPTS OF PYTHON AND OTHER BASIC CONCEPTS OF COMPUTER AND ALSO FOR THE IMPORTANT DEFINITIONS OF THE SQL
THE IF STATEMENT OF PYTHON
Reviewed by Learn Python
on
November 17, 2019
Rating:
No comments: