In Python, beginners and expert coders can aware of executing an If statement thoroughly. If you are a newbie to this python programming then check out this tutorial on How to use Python’s If Statement and completely understand the python basic conditional statement.
In this, if statement you will see the expression then statement that should execute when the condition is true. If false then the statement will be skipped or not executed. Make sure you learn what is If statement, syntax, flow chart, and examples thoroughly from this Python If Statement Tutorial.
The python if statement tutorial covers the following modules:
- What is If Statement in Python?
- Python if Statement Syntax
- If Statement flow diagram
- Python – Simple If Statement Example
- Example 2: Program to print the largest of the three numbers.
Python if statements are control flow statement that assists developers to execute a particular code only when a performed code is satisfied. It is utilized for decision-making operations. If the condition is true then it displays the body of the code. If the condition is false, the body of the code is skipped over and not executed.
The basic syntax for an If statement is as follows:
if(condition) : [code to execute]
The syntax for a simple if statement is fairly straightforward. Make sure that the condition is something that can either be true or false, like a variable is equal to, not equal to, less than or greater than something (and remember that the “equal to” symbol in this situation looks like this: ==)
The below representation make you understand how if statement in python works:
To see it used in context, take a look at the example below.
var=40 if(var == 40) : print "Happy Birthday!" print "end of if statement"
The output for the above code, as you might be able to guess, would be as follows:
Happy Birthday! end of if statement
Happy Birthday! is printed because that was the code executed based on the conditions that var was equal to 40, which it was. End of if statement is used in this example to signify that the if statement is over. If var had equaled something other than 40, only “end of if statement” would have been printed, and the code belonging to the if statement wouldn’t have been executed, since it’s only allowed to execute when the if conditions are true.
a = int(input("Enter a? ")); b = int(input("Enter b? ")); c = int(input("Enter c? ")); if a>b and a>c: print("a is largest"); if b>a and b>c: print("b is largest"); if c>a and c>b: print("c is largest");
Enter a? 100 Enter b? 120 Enter c? 130 c is largest