4.Conditional Statements

Conditional Statement:

Conditional Statement allows you to execute a block of code
only when a specific condition is True.

if True:
print("If Block")
print("Inside If")
# Output is:
If Block
Inside If

If – Else Statement: When the If – Else conditional statement is used, the Else
block of code executes if the condition is False.

a = int(input()) # -1
if a > 0:
print("Positive")
else:
print("Not Positive")

# Output is:
Not Positive

Nested Conditions: The conditional block inside another if/else conditional block is called as a nested conditional block.

if Condition A:
if Condition B:
block of code
else:
block of code
if Condition A:
block of code
else:
if Condition B:
block of code

Elif Statement: Use the elif statement to have multiple conditional statements
between if and else. The elif statement is optional.

if Condition A:
block of code
elif Condition B:
block of code
else:
block of code

Identation:

  1. Space(s) in front of the conditional block is called indentation.
  2. Indentation(spacing) is used to identify the Conditional Blocks.
  3. Standard practice is to use four spaces for indentation.

Flow Chat


Leave a comment

Blog at WordPress.com.

Design a site like this with WordPress.com
Get started