Conditional Statements
In this post, you will learn about Python's conditional statements. We'll also look at their use cases along with some examples that you can test on this page!
If Statement
In Python, the If statement is used for decision making. The If statement checks for a given condition, if the condition is true, then the set of code present inside the if block will be executed. The If condition evaluates a Boolean expression and executes the block of code only when the Boolean expression becomes TRUE. In its simplest form, the syntax looks like this:
In the form shown above:
- <expr> is an expression evaluated in Boolean context (true or false)
- <statement> is a valid Python statement, which must be indented
If <expr> is true, then <statement> is executed. If <expr> is false, then <statement> is skipped over and not executed. Note that the colon (:) following <expr> is required. Some programming languages require <expr> to be enclosed in parentheses, but Python does not.
Let's look at an example:
Else Clause
The Else clause is usually used when you have to judge one statement on the basis of other. If one condition goes wrong, then there should be another condition that should justify the statement or logic. The syntax looks like this:
If <expr> is true, the first code is executed, and the second is skipped. If <expr> is false, the first code is skipped and the second is executed. Either way, execution then resumes after the second code. Both codes are defined by indentation, as described above.
Let's look at a few examples:
Elif Clause
In python, we have one more conditional statement called elif statements. Elif statement is used to check multiple conditions only if the given if condition false. It's similar to an if-else statement, the only difference being that in an if-else we will not check the condition but in elif we will do check the condition. Elif statements are similar to if-else statements but elif statements evaluate multiple conditions.
Syntax:
Python evaluates each in turn and executes the code corresponding to the first that is true. If none of the expressions are true, and an else clause is specified, then its code is executed:
Lets look at an example:
Your turn!
If you want to try any of the code for yourself, all you have to do is copy it and paste it into the compiler below, and then hit run! Remember, if you declare a variable, you should add a print statement for the console to display it.
I noticed in one of your if statements you have parenthesis around the expression and in some of the others you don't? Are parenthesis optional when declaring a statement? If they are is it more of a norm in Phyton to still use parenthesis or leave them off?
ReplyDeleteGreat question! Parenthesis aren't required for expressions, including expressions with multiple operators. This is because operators have a defined precedence, making parenthesis optional in most cases. However, although they aren't strictly necessary, it is good practice to include parenthesis for a number of reasons. They make code easier to understand and make the developer's intent clear. Extra parentheses, like indentation, help visually organize the code in a logical way. Additionally, it is easy to make mistakes without them. If you just have a single, simple expression, it is fine to leave them off. But for clarity purposes, it is wise to include them.
Delete