Python if statement syntax

The following is the syntax for the if statement:

if test_expression:
statement(s)

Here, the program evaluates the test expression and will execute statement(s) only if the text expression is true. If the text expression is false, statement(s) isn't executed.

In Python, the body of the if statement is indicated by the indentation. The body starts with an indentation and the first unindented line marks the end. Let's look at an example:

a = 10
if a > 0:
print(a, "is a positive number.")
print("This statement is always printed.")

a = -10
if a > 0:
print(a, "is a positive number.")

Output:
10 is a positive number.
This statement is always printed.
..................Content has been hidden....................

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