Legacy - IF Statements
- 01:58
Learn how to build "IF" statements in Python.
Downloads
No associated resources to download.
Glossary
conditioning indentation. variable definingTranscript
Let us now look at how to write an IF statement in Python. Before writing a IF statement, we need to understand Python supports the usual logical conditions from mathematics, such as equal to, not equal to, greater than, less than, greater than or equal to and less than or equal to. These logical conditions are used to write the IF statement in Python. Initially, before writing the statement, we have to define the variables that shall be used. The statement starts with a IF and that the condition which is that followed by a colon, we did mention an action that needs to be performed once the conditions are met. But what happened if the conditions are not met, the statement would not output any kind of a result. In that case, we should also have an action that needs to be performed if the conditions are not met. One thing to note is that the editions are really important. One suggestion is to press enter once you reach the colon, rather than manually setting the notations, this is the same case when you reach the end of a line, press Enter rather than manually setting the indentation. Let Python the job for you. In the example of the screen, we have initially defined the variables as x and y, which are equal to five and two respectively. In this example, we have three scenarios when x is greater than y, when x is less than y, or when x is equal to y. In an IF statement we have if followed by LIF followed by else for three different conditions. In this scenario, it is not necessary to say the condition for the else scenario as the only condition that is left to be back this x being less than y, and the scenario is assumed by Python by default.