Python if...else statement syntax

In this section, we are going to learn about the if..else statement. The else block will get executed only when the if condition is false. Refer to the following syntax:

if test expression:
if block
else:
else block

The if..else statement evaluates the test expression and will execute the body of if only when the test condition is true. If the condition is false, the body of else is executed. Indentation is used to separate the blocks. Refer to the following example:

a = 10
if a > 0:
print("Positive number")
else:
print("Negative number")

Output:
Positive number
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset