DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Conditional Statement in Python
SaranJoel
SaranJoel

Posted on

Conditional Statement in Python

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

if condition:
   code()
Enter fullscreen mode Exit fullscreen mode

If the given condition is true,the code will be executed
if it is false the process will end.
Alt Text

age=12
if age<21:
  print("you are under age")
Enter fullscreen mode Exit fullscreen mode

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

if condition:
  code()
else:
  code()
Enter fullscreen mode Exit fullscreen mode

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:

Age =11
if age<21:
  print("you are under age")
else:
  print("you are an youngster")
Enter fullscreen mode Exit fullscreen mode

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
the syntax for the elif is

if condition0:
    code()
elif condition1:
    code()
elif condition2:
    code(2)
else:
    code()
Enter fullscreen mode Exit fullscreen mode

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:

num=1,num=-23,num=0
if num > 0:
    print("Positive number")
elif num == 0:
    print("Zero")
else:
    print("Negative number")
Enter fullscreen mode Exit fullscreen mode

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

if (condition1):
   code()
   if (condition2): 
      code()
Enter fullscreen mode Exit fullscreen mode

Alt Text
an Ex

a= 1001  
if a> 100:  
  print("Above 100")  
  if a > 1000:  
    print("and also above 1000") 
Enter fullscreen mode Exit fullscreen mode

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

if(condition):  
  if code  
     if(condition):  

       if code  

     else:  

        else code  


else:  

 else code  

Enter fullscreen mode Exit fullscreen mode

lets see an Ex:

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")  
Enter fullscreen mode Exit fullscreen mode

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!").

Top comments (0)

About Real-time

Join DEV and MongoDB to build a front-end application using MongoDB Atlas. Change streams to display live updates as your database changes for your entry in the DEV x MongoDB Atlas Hackathon 2022.

β†’ Join the Hackathon