Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In programming, we must make judgments and then execute the following code block depending on those decisions. In programming languages, decision-making statements determine the course of program execution.
If, else, elif if used for decision-making in Python.
If Statement
if condition is the most basic decision-making statement. It is used to determine whether a particular statement or block of statements will execute or not, i.e., if a condition is true, a block of statements will be performed, otherwise not.
Syntax
if condition:
# Statements to execute if
# condition is true
You can also try this code with Online Python Compiler
After evaluation, the condition will be either true or false. If the value is true, the if statement will execute the block of statements below it; otherwise, it will not.
Flowchart of if statement
Example:
Here's an example of a basic if statement
x = 70
y = 90
if (x < y):
print('We’re inside the if condition')
z = 130
print(z)
You can also try this code with Online Python Compiler
Because the condition present in the if statement is true. That's why the block below the if statement is executed and the statements just after the if block.
If-else Statement
If a condition is true, then if statement will execute a block of statements; if the condition is false, the if statement will not. But what if the condition is false, and we want to do something else? This is when the else statement comes in. When the condition is false, we may combine the else statement with the if statement to execute a code block.
Syntax
if (condition):
# Statements to execute if
# condition is true
else:
# Statements to execute if
# condition is false
You can also try this code with Online Python Compiler
After evaluation, the condition will be either true or false. If the value is true, the if statement will execute the block of statements below it; otherwise, else code block will execute.
Flowchart of if-else statement
Example:
Here's an example of a basic if-else block
x = 70
y = 90
if (x < y):
print('x is smaller than y')
print('in if statement')
else:
print('y is less than or equals to x')
print('in else statement')
z = 130
print(z)
You can also try this code with Online Python Compiler
Because the condition present in the if statement is true. So, the code block inside the if statement is executed and the statements just after the if-else block and it’ll not go inside the else block.
Nested if-else statements mean that an if-else block is inside of another if-else block.
Syntax
if (condition1):
# Statements to execute if condition1 is true
if (condition2):
# Statements to execute if condition2 is true
else:
# Statements to execute if condition2 is false
else:
# Statements to execute if condition1 is false
You can also try this code with Online Python Compiler
x = 70
y = 90
z = 100
if (x < y):
print('x is smaller than y')
if (x < z):
print('x is smaller than z too')
else:
print('y is smaller than or equals to x')
if (x < z):
print('x is smaller than z')
You can also try this code with Online Python Compiler
The if-elif statements are executed from the top down. As soon as one of the conditions controlling the if-elif is true, the statement associated with that if-elif is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
What do you mean by control structure in python? Control structures decide which statements in a program are executed and in what order, allowing statements to be skipped or repeated. if, if/else, and if/elif/else statements are all examples of control structures that allow which statements to be skipped or execute conditionally
What part of an if statement should be indented? All the statements that lie within the if block should be indented and at the same level
What is the most popular use case of shorthand syntax? It is most popularly used when we just need to return or print something and there’s only one statement in the conditional block.
Key Takeaways
Cheers if you reached here! In this blog, we learned about Conditional Statements in Python.
We have covered the syntax, flowchart, and uses of basic if and if-else nested if-else code blocks.
We have also seen different examples and use cases of these conditional blocks.
Further, we learned about shorthand syntax in python.
On the other hand, learning never ceases, and there is always more to learn. So, keep learning and keep growing, ninjas!