What are conditional statements?
Conditional Statements are used to terminate or changes the flow of program.
If the condition is True then all the statements written in the condition will get executed and after completing the statements it proceeds with the program.
And if the condition is False then program continues without executing the statements (statements are ignored/skipped).
Python conditional statements
Conditional Statements with Python are, 'if' conditional statement, alternative execution, chained or ladder conditional execution, nested conditionals.
Conditional execution - 'if' statement
'if' is the simplest form of conditional statement. Which has the following syntax.
Syntax
if condition:
Program Example
Explanation:- First Of All the 'if' keyword is used to initiate the conditional execution, then the required condition is checked using Boolean operation which returns 'True' or 'False'.
Followed by a colon (:), which will tell the compiler that the condition checking is over, now it's time to write statements.
Then each statement is written in new line with an indentation(Tab spacing), if your 'Tab' key is broken then you can give 4 spaces, which works same as Tab.
Indentation tells the compiler that a particular set of instruction belongs to a particular statement.
If the condition is True then the statements are executed otherwise it is skipped in False condition.
If you forget indentation then Python will give you an error sayin "SyntaxError: expected an indented block".
That's why indents are very very important in Python.
Use '#' symbol to write comments, all the things written after '#' will be ignored.
Program
If you don't know how to create a Python file (Without installing any IDE), click here.
Alternative execution
In alternative execution, there are 2 possibilities 'if' and 'else'. In which one of the statement gets executed for sure. Depending on the condition.
When 'if' statement is 'True' then the content in the 'if' statement gets executed and the 'else' statement is ignored/skipped.
And if the 'if' statement is 'False' then the content in the 'if' statement is ignored and the content in the 'else' statement is executed.
Program
Input
5 is greater than 0
Output
-5 is less than 0
Python Chained conditions
In the chained conditions there are more then one possibilities of execution.
To make more possibilities, we make use of 'elif' conditional statement in between 'if' and 'else' statements.
The format of 'elif' conditional statement is same as that of 'if' conditional statement, only the keyword's are different.
Program
Input
Output
5 is greater than 0
0 is equal to 0
5 is odd number
Nested conditions in Python
Conditional Statements within another conditional statement are called as nested conditions. Which is also possible with python.
Following is an example for nested conditions in Python.
Program
Input
Thank you for reading this article...
Conclusion
Putting conditions in program will terminate or will change the flow of program.
Topics converted-
- if conditional execution.
- Alternate if and else conditional execution. if False then else is executed.
- Chained condition in which we can put more conditions using elif conditions.
- Nested condition in which we put condition inside another condition.
Post a Comment
If you have any query, feel free to ask.