Keywords: if statement, conditional statement, else statement, elif statement
The if statements in Python is a condition that must be fulfilled in order for a desired command to be executed. All programming is based on conditions. It is the if-statements that control the dependent conditions, which gives the foundation for all logic in programming
The if statement in Python tests if the logical expression (if-condition in the image) is fulfilled. The logical expression is a condition that is either true or false. For example, if you press a lamp button, the lamp should either be turned on or off. Not both at the same time. With an if statement, you can decide whether to turn the power on or off (depending on whether the lamp is off or on).
In other words, we use a logical expression to decide if our programs should execute a certain code or not. Meaning, by using the if statement, you can specify what should happen if a condition is met or not. This creates conditional programming, which is the basis of all logic in a program.
What the figure above illustrates is that:
Remember: It is possible to use one or more conditions that indicate whether the program should perform the operation or not.
if (logical expression): Operation / Operations …
Operation / Operations ..
Below is an example of how an if statement works.
run_operation = True if(run_operation): print("If statement has started") print("All operations that are indented will be performed") print("This command is not included in the if statement because the code is not indented")
The result in this case will be:
If statement has started All operations that are indented will be performed This command is not included in the if statement because the code is not indented
However, if we instead set the variable run_operation to False, then the code inside the if statement will not be performed
run_operation = False if(run_operation): print("If statement has started") print("All operations that are indented will be performed") print("This command is not included in the if statement because the code is not indented")
The result will be:
This command is not included in the if statement because the code is not indented
If we want to check if an integer variable is within an interval, for example, between 0 and 200, we easily set up the if statement with the logical expression.
num = 50 if(num > 0 and num < 200): print("Number:", num)
And in this case we get the result
Number: 50
The if statement in Python is a conditional statement that determines whether the program should perform an operation, or several operations, depending on whether one or more specified conditions are met or not. The condition is a logical expression that is either true or false.
if (logical expression): Operation / Operations …
Please leave feedback and help us continue to make our site better.
How useful was this article?
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?