Python if statement
The Python if statement is a statement which is used to test specified condition. We can use if statement to perform conditional operations in our Python application. The if statement executes only when specified condition is true. We can pass any valid expression into the if parentheses.
There are various types of if statements in Python.
- if statement
- if-else statement
- Else if statement
- nested if statement
If Statement Syntax
if statement, which will evaluate whether a statement is true or false, and run code only in the case that the statement is true.
if(condition):
statements
Example of if statement :
grade = 70
if grade >= 65:
print("Passing grade")
With this code, we have the variable grade and are giving it the integer value of 70. We are then using the if statement to evaluate whether or not the variable grade is greater than or equal ( >= ) to 65. If it does meet this condition, we are telling the program to print out the string Passing grade.
output of above example :
Passing grade
Again, if we change the grade to 50 or a < 65 number, we will receive no output.
advertisement