In this post, we will study Python If statement, Python if else statement. first of all, lets first understand what is if statement? And why we use that. Python if-else, python elif is part of python basics.
If we want to execute some line of codes on any particular condition, in that case generally we use if statement.
Flow chart of If Statement:
If we see the flowchart of if condition, it will be like
A conclusion from flowchart of if condition:
If we observed clearly the flowchart, we will see If some condition is true in the program, then it will execute the statements inside if block, else it will execute the rest of the program.
For example: if we want to print the capital of the country, we can use if statement like this
print (“New Delhi”)
print (“Washington, D.C.”)
in this example we saw, if country name is India then “New Delhi” will print and if countryName is USA, then “Washington, D.C.” will print.
Now we understand what is if condition and why we use it
Python If statement:
Now we will study Python If statement and how we will write if statement in Python.
Python If statement syntax: Here we will see the syntax of Python if Statement, below is the syntax
This is the general syntax of python if statement.
Key points to understand:
- There will be a keyword if in if condition
- condition is some expression to validate
- There will be colon ( : ) after condition
- Statements are the line of code which will execute inside if block
Example of python if statement
If we take the same example as we discuss above, then we will write that code in python as below
countryName="India" if countryName=="India" : print("New Delhi") if countryName=="USA" : print("Washington, D.C")
The output will be New Delhi in this case
Now we have read python if, now we will read python if else statement
Python If else Statement
If we generally say if-else is interpreted as either this or that. Let’s understand it by taking an example.
Example: if there is a number, now the requirement is that we need to check whether the number is 0 or not then we will write python if-else program like this
Python if-else example
number=1 if number &gt; 0 : print ("number is greater than 0") else: print ("number is 0")
The output will be
number is greater than 0 .
This is if-else python program like if we will put a colon ( : ) after else keyword, after that the statements which we want to execute.
Python elif Statement:
Python elif is another if condition, which is used with if condition in python. elif states that when if the condition is not true then python will check this condition, it’s kind of another if condition which will be used with if condition in Python.
Example: let’s understand by taking one example.
Requirement: Suppose we have to check whether the number is 5 or greater than 5 or less than 5.
Then we can use elif like this
number=6 if number==5 : print ("number is equal to 5") elif number &gt; 5: print ("number is greater than 5") else: print("number is less than 5")
The output is
number is greater than 5
In this example, the code will check the value in if condition, here the value is 6, so if condition code will not execute, then it will come to elif, it will check, now the number is 6 and it’s greater than 5, so this condition will be true. Hence we will able to see the result.
The number is greater than 5
So we have study about python if statement, Python if-else statement and Python elif statement.