In Python, an If statement contains code that is executed based on whether or not certain conditions are true. For example, if you want to print something, but you want the execution of that printed text to be contingent upon other conditions, you're going to need to use an If statement.
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: ==)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 that was 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.