S-1
18.1.2. Understanding If construct
02:40
The general syntax of if statement in Python is,
If test expression: statement(s)
- The if-construct is a selective construct. The statements within the if block are executed only once when the condition evaluates to True. Otherwise, the control goes to the first statement after the if-construct.
- In Python, the body (block of statements) of the If statement is indicated by indentation and the first unindented line marks the end.
- Python interprets non-zero values as
True . None and 0 are interpreted asFalse .
Here is a simple program to illustrate the simple if-statement:
if (12 % 2 == 0):
print("12 is divisible by 2") # Notice the Indentation
print("Execution done")
Write a program to check whether the given integer is divisible by 3 or not, and print the result to the console as shown in the examples.
Sample Input and Output 1:
num: 66 divisible by 3 End of programSample Input and Output 2:
num: 52 End of program
EXAMPLE PROGRAM
num = int(input("num: "))if(num%3==0):print("divisible by 3")print("End of program")else:print("End of program")
Comments
Post a Comment