Python-3 5
Python-3 5
if number > 0:
print('Number is positive.’)
if number > 0:
print('Number is positive.’)
OUTPUT:
the value of number is less than 0. Hence, the condition
evaluates to False. And, the body of if block is skipped.
2. Python if...else Statement
if condition:
# block of code if condition is True
else:
# block of code if condition is False
The if...else statement evaluates
the given condition:
If the condition evaluates to True,
the code inside if is executed
the code inside else is skipped
if number > 0:
print('Positive number')
else:
print('Negative number')
Example 2. Python if...else
Statement
Since the value of number is 10, the
test condition evaluates to True. Hence
code inside the body of if is executed.
if number > 0:
print('Positive number')
else:
print('Negative number')
Example 2. Python if...else
Statement
The test condition evaluates to
False. Hence code inside the body
of else is executed.
3. Python if...elif...else Statement
if condition1:
# code block 1
elif condition2:
# code block 2
else:
# code block 3
■If condition1 evaluates to true, code block 1 is
executed.
■If condition1 evaluates to false, then condition2
is evaluated.
■If condition2 is true, code block 2 is executed.
■If condition2 is false, code block 3 is executed.
Python if...elif...else Statement
Example:
number = 0
if number > 0:
print("Positive number")
elif number == 0:
print('Zero')
else:
print('Negative number')
Example: