Conditional Statement in Python
Tags: beginners, python, codenewbie, programming
Published: 2021-09-03 12:40:03

So after looking into the Datatypes,we got is
 Conditional statement.
What are Conditional Statements in Python?and why do we need them?
The reasons are:
Conditional Statement in Python perform different computations or actions depending on whether a specific Boolean constraint evaluates to true or false. Conditional statements are handled by IF statements in Python.
Decision making is required when we want to execute a code only if a certain condition is satisfied.
The if…elif…else statement is used in Python for decision making.
so we have different decision making statement they are
1.if
2.if-else
3.elif
4.Nested 
#1.if
###if Statement Syntax
```python
if condition:
   code()
```
If the given condition is true,the code will be executed
if it is false the process will end.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h05u5bw1fmtiv98tfkhh.jpg)
```python
age=12
if age<21:
  print("you are under age")
```
This is how we use the if statement
#2.if else
This is the extention for the if statement.So that even if the
the 'if' condition becomes false we need to perform another code in that cases we use this if-else statements.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oknbvpfydz1n26ezghws.png)
```python
if condition:
  code()
else:
  code()
```
here if the condition in the 'if' becomes true,then it executes code(1)
if the 'if' condition is false then the code(2) is executed
here Example:
```python
Age =11
if age<21:
  print("you are under age")
else:
  print("you are an youngster")
```
so next in the table we have
#3.elif
elif is a shortcut of else if condition statements. In Python one or more conditions are used in the elif statement.the flow :
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gn85pd425h63w87537rq.png)
the syntax for the elif is
```python
if condition0:
    code()
elif condition1:
    code()
elif condition2:
    code(2)
else:
    code()
```
this is the syntax of the elif
as we see if the condition in the 'if' : false
the next condition is checked which is in elif block, if the conditions true the code will be executed,And so.
so lets see an Ex:
```python
num=1,num=-23,num=0
if num > 0:
    print("Positive number")
elif num == 0:
    print("Zero")
else:
    print("Negative number")
```
next
#4.Nested
The nested is to do the condition checking simultaneously.
we have 
1.Nested if
2.Nested if else
###1.Nested if
A nested if is an if statement that is the target of another if statement. Nested if statements mean an if statement inside another if statement.Yes, Python allows us to nest if statements within if statements. we can place an if statement inside another if statement.
the syntax will be 
```python
if (condition1):
   code()
   if (condition2): 
      code()
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6oqx91jecdh3cyf05ie.jpg)
an Ex 
```python
a= 1001  
if a> 100:  
  print("Above 100")  
  if a > 1000:  
    print("and also above 1000") 
```
so the next is 
###2.Nested if else
using one “if else” statement inside other  if else statements it is called nested if else statement.
the syntax for this is
```python
if(condition):  
  if code  
     if(condition):  
       
       if code  
       
     else:  
       
        else code  
       
  
else:  
  
 else code  
  
```
lets see an Ex:
```python
a=1456  
if a> 100:  
  print("Above 100")  
  if a > 1000:  
    print("and also above 1000")  
  else:  
    print("and also below 1000")  
else:  
    print("below 100")  
```
so these are the conditional statements in the pyhton
so once again why do we use these conditional statements?
Conditional statements are also called _decision-making statements_. We use those statements while we want to execute a block of code when the given condition is true or false.
 well see you in the next post.
print("thank you!").