Page 106 - PortGPT_V2.1_C7_Flipbook
P. 106
THE IF STATEMENT
The if statement is the simplest conditional statement. In case of
Start
the if statement, a statement or a collection of statements within
the if block is executed only if a certain condition or expression
evaluates to True. If the condition evaluates to False, then the False
if condition
control of execution is passed to the next statement after the if
block. The syntax of the if statement is given below: True
Syntax: Statements in if
block execute
if (Test Expression):
Indented statement block Stop
# if block ends here
Tech Fact
In Python, the non-zero value is interpreted as True. None and 0 are interpreted as False.
Program 1: To check whether a given number is positive.
Program1.py
File Edit Format Run Options Window Help
#Program to check if a number is positive
n = int (input ('Enter a number:'))
if(n > 0):
print('Entered number is positive')
#Statement outside the if statement
print('All numbers greater than 0 are positive')
On running the above program, we get the following output:
Output
Enter a number:6
Entered number is positive
All numbers greater than 0 are positive
Enter a number:-9
All numbers greater than 0 are positive
104 Premium Edition-VII

