So after looking into the Datatypes,we got is
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
if condition: code()
age=12 if age<21: print("you are under age")
This is how we use the if statement
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
Age =11 if age<21: print("you are under age") else: print("you are an youngster")
so next in the table we have
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:
num=1,num=-23,num=0 if num > 0: print("Positive number") elif num == 0: print("Zero") else: print("Negative number")
The nested is to do the condition checking simultaneously.
2.Nested if else
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()
a= 1001 if a> 100: print("Above 100") if a > 1000: print("and also above 1000")
so the next is
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
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")
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.