DEV Community

221910301060
221910301060

Posted on

Python if else

There comes situations in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations arises in programming also where we need to make some decisions and based on these decisions we will execute the next block of code.

Decision making statements in programming languages decides the direction of flow of program execution. Decision making statements available in python are:

if statement
if..else statements
nested if statements
if-elif ladder
Short Hand if statement
Short Hand if-else statement
Enter fullscreen mode Exit fullscreen mode

if statement

if statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.

Syntax:
if condition:

# Statements to execute if
# condition is true

Here, condition after evaluation will be either true or false. if statement accepts boolean values – if the value is true then it will execute the block of statements below it otherwise not. We can use condition with bracket ‘(‘ ‘)’ also.
Flowchart:-
if-statement

python program to illustrate If statement

i = 10
if (i > 15):
print ("10 is less than 15")
print ("I am Not in if")

Output:

I am Not in if

if- else

if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
Flow Chart:-

if-else

Top comments (0)