Page 327 - AI Ver 1.0 Class 9
P. 327
• Multiline Statements: We can make a statement that extends over multiple lines by using line continuation
character (\) as shown below:
a = 4 + 5 +\
6 + 7 +\
8 + 9
The main advantage of using multiline is when we need to do long calculations and cannot fit these statements into
one line.
5. Explain if…else statement with example.
Ans. When the condition is True then the statements indented just below the if statement will be executed and if the
condition is False then the statements indented just below the else statement will be executed. Syntax is:
if (condition):
Statements for True
else:
Statements for False
Example:
age=input("enter your age ")
if (age>=18):
print("eligible for driving")
else:
print("Not eligible")
6. Explain "for" loop with an example.
Ans. It is used to repeat a set of instructions for a fixed number of times. It means when the number of iterations are
known/definite before we start with the execution of a loop. It is therefore also known as definite loop. Indentation of
statements is must to specify the block of statements to be repeated using for loop.
There are two ways to use for loop:
• Using sequence
• Using range() function
Example:
for i in range(10, 0, -1):
print(i)
7. Explain while loop with example.
Ans. It is used to repeat a set of instructions as long as the condition is true. It means when the number of iterations are
not fixed/indefinite before we start with the execution of a loop. It is therefore known as indefinite loop. Indentation of
statements is must to specify the block of statements to be repeated using while loop.
This loop is also called an entry-controlled loop as it checks for the condition in the beginning only. If the condition is
true then the body of the loop will be executed. If the condition is false then it will not be allowed to enter within the
loop and it stops. Syntax of the while loop is:
while <condition>
Statements
Where,
while is a keyword.
condition is a criterion to repeat the instructions. The instructions repeat till the condition is True. As soon as the
Introduction to Python 325

