Question and Answer
2 Marks Question
Q1. What is a Control Statement in Python?
Answer:
A control statement is a statement that controls the flow of execution of a Python program. It determines the order in which statements are executed based on conditions or repetitions.
The main types of control statements in Python are:
-
Selection (Decision-Making) Statements:
if,if-else, andif-elif-else -
Iteration (Looping) Statements:
forandwhile -
Jump Statements:
break,continue, andpass
Conclusion:
Control statements help make Python programs more efficient by allowing decisions, repetition of tasks, and transfer of control during program execution.
Q2. What is the purpose of the if statement?
Answer:
The if statement is a
decision-making statement in Python. It is used to
execute a block of code only when a specified condition is
True. If the condition is
False, the block of code is skipped.
Example:
age = 18
if age >= 18:
print("Eligible to Vote")
Output:
Eligible to Vote
Conclusion:
The if statement helps a program make
decisions by executing code only when a specified condition is true.
Q3. Differentiate between if and if-else statements.
Answer:
Both if and if-else
are decision-making statements in Python. However, they differ in the way
they execute code based on a condition.
if Statement
|
if-else Statement
|
|---|---|
| Executes a block of code only if the specified condition is True. | Executes one block of code if the condition is True and another block if the condition is False. |
| If the condition is False, no statement is executed. | One of the two blocks is always executed. |
| Used when only one condition needs to be checked. | Used when two alternative actions are required based on a condition. |
Example of if Statement:
age = 18
if age >= 18:
print("Eligible to Vote")
Example of if-else Statement:
age = 16
if age >= 18:
print("Eligible to Vote")
else:
print("Not Eligible to Vote")
Q4. What is the use of the elif statement?
Answer:
The elif (else if) statement is used to check
multiple conditions in a Python program. It is executed only if the
previous if or
elif condition is
False. It helps in selecting one block of code from
several alternatives.
Example:
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
else:
print("Grade C")
Output:
Grade B
Conclusion:
The elif statement is useful when a program
needs to test multiple conditions one after another and execute the
appropriate block of code.
Q5. What is a Loop?
Answer:
A loop is a control statement in Python that is used to execute a block of code repeatedly until a specified condition becomes false or for a fixed number of iterations.
Python provides two types of loops:
-
forloop – Used when the number of iterations is known. -
whileloop – Used when the number of iterations is not known and depends on a condition.
Example:
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
Conclusion:
Loops help reduce code repetition and make programs more efficient by executing the same set of statements multiple times.
Q6. What is the difference between for loop and while loop?
Answer:
for Loop
|
while Loop
|
|---|---|
| A for loop is used when the number of iterations is known in advance. | A while loop is used when the number of iterations is not known and depends on a condition. |
It iterates over a sequence such as a list, tuple, string, or range().
|
It executes repeatedly as long as the given condition remains True. |
Example:
|
Example:
|
Q7. What is the purpose of the range() function?
Answer:
The range() function is used to generate a
sequence of numbers. It is commonly used with the
for loop to execute a block of code a
specific number of times.
Example:
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
Explanation:
The range() function helps control the number
of loop iterations and can generate sequences with a specified
start, stop, and
step value.
Syntax:
range(start, stop, step)
Q8. What is the use of the break statement?
Answer:
The break statement is used to
terminate a loop immediately, even if the loop condition
is still True. When a
break statement is encountered, the control
exits the loop and moves to the next statement after the loop.
Example:
for i in range(1, 6):
if i == 3:
break
print(i)
Output:
1
2
Explanation:
The break statement is useful when a loop
needs to be stopped before completing all its iterations.
Q9. What is the use of the continue statement?
Answer:
The continue statement is used to
skip the current iteration of a loop and continue with
the next iteration. When a
continue statement is encountered, the
remaining statements inside the loop for the current iteration are skipped.
Example:
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1
2
4
5
Explanation:
The continue statement is useful when
certain iterations need to be skipped without terminating the entire
loop.
Q10. What is Nested if?
Answer:
A Nested if is an
if statement placed inside another
if statement. The inner
if statement is executed only if the
condition of the outer if statement is
True.
Example:
age = 20
if age >= 18:
if age <= 60:
print("You are an Adult.")
Output:
You are an Adult.
Explanation:
A Nested if statement is useful when
multiple conditions need to be checked in a step-by-step manner before
executing a block of code.
3 Marks Question
Q1. Explain the if-else statement with an example.
Answer:
The if-else statement is a
decision-making statement in Python. It is used to
execute one block of code when a condition is
True and another block of code when the condition is
False.
The if block is executed if the condition
evaluates to True. Otherwise, the
else block is executed.
Syntax:
if condition:
# Statements executed if the condition is True
else:
# Statements executed if the condition is False
Example:
age = 16
if age >= 18:
print("Eligible to Vote")
else:
print("Not Eligible to Vote")
Output:
Not Eligible to Vote
Explanation:
In the above program, the value of age is
16, which is less than 18. Therefore,
the condition age >= 18 is
False, so the else block is executed and
the message "Not Eligible to Vote" is displayed.
Q2. Explain the if-elif-else ladder with an example.
Answer:
The if-elif-else ladder is a
decision-making statement used to check
multiple conditions one after another. The program
executes the block of code associated with the
first condition that evaluates to True.
If none of the conditions are true, the
else block is executed.
Syntax:
if condition1:
# Statements
elif condition2:
# Statements
elif condition3:
# Statements
else:
# Statements
Example:
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")
Output:
Grade B
Explanation:
In the above program, the value of marks is
75. The first condition
(marks >= 90) is False, but the second
condition (marks >= 75) is
True. Therefore, the statement
"Grade B" is displayed, and the remaining conditions
are skipped.
Q3. Explain the working of for loop with an example.
Answer:
The for loop is an
iteration (looping) statement used to execute a block
of code repeatedly for each item in a sequence such as a
list, tuple,
string, or range().
It is generally used when the number of iterations is known in advance.
Syntax:
for variable in sequence:
# Statements
Example:
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
Explanation:
In the above program, the
range(1, 6) function generates the numbers
1, 2, 3, 4, and 5. The loop variable
i takes each value one by one, and the
print() function displays it. The loop
stops automatically after printing 5.
Q4. Explain the working of while loop with an example.
Answer:
The while loop is an
iteration (looping) statement used to execute a block
of code repeatedly as long as a specified condition remains
True. It is generally used when the number of
iterations is not known in advance and depends on a
condition.
Syntax:
while condition:
# Statements
# Update the condition
Example:
i = 1
while i <= 5:
print(i)
i += 1
Output:
1
2
3
4
5
Explanation:
In the above program, the variable i is initialized to
1. The loop executes as long as the condition
i <= 5 is True. After each iteration,
the value of i is increased by 1 using
i += 1. When i becomes
6, the condition becomes False, and
the loop terminates.
Q5. Differentiate between break and continue statements.
Answer:
Both break and
continue are jump statements
used to control the execution of loops. However, they perform different
functions.
break Statement
|
continue Statement
|
|---|---|
| Terminates the loop immediately. | Skips the current iteration and continues with the next iteration. |
| Control moves to the first statement after the loop. | Control returns to the beginning of the loop for the next iteration. |
| Used when the loop needs to stop before completing all iterations. | Used when only a particular iteration needs to be skipped. |
Example: break
|
Example: continue
|
Example of break:
for i in range(1, 6):
if i == 3:
break
print(i)
Output:
1
2
Example of continue:
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1
2
4
5
Q6. Write a Python program to print numbers from 1 to 10 using for loop.
Answer:
The following Python program uses a
for loop along with the
range() function to print numbers from
1 to 10.
Program:
# Program to print numbers from 1 to 10 using for loop
for i in range(1, 11):
print(i)
Output:
1
2
3
4
5
6
7
8
9
10
Explanation:
-
The
range(1, 11)function generates the numbers from 1 to 10. -
The
forloop iterates through each number in the sequence. -
The
print()function displays each number on a new line.
Q7. Write a Python program to find the sum of first 10 natural numbers.
Answer:
The following Python program uses a
for loop to calculate the
sum of the first 10 natural numbers.
Program:
# Program to find the sum of first 10 natural numbers
sum = 0
for i in range(1, 11):
sum = sum + i
print("Sum =", sum)
Output:
Sum = 55
Explanation:
-
The variable
sumis initialized to 0. -
The
range(1, 11)function generates numbers from 1 to 10. -
During each iteration, the current value of
iis added tosum. -
After the loop completes, the
print()function displays the total sum of the first 10 natural numbers, which is 55.
Q8. Write a Python program to check whether a number is positive or negative.
Answer:
The following Python program accepts a number from the user and checks
whether it is positive, negative, or
zero using the if-elif-else statement.
Program:
# Program to check whether a number is positive or negative
num = int(input("Enter a number: "))
if num > 0:
print("Positive Number")
elif num < 0:
print("Negative Number")
else:
print("Zero")
Sample Output:
Enter a number: 25
Positive Number
Explanation:
-
The
input()function accepts a number from the user. -
The
int()function converts the input from a string to an integer. - If the number is greater than 0, the program displays "Positive Number".
- If the number is less than 0, the program displays "Negative Number".
- If the number is neither positive nor negative, it must be 0, so the program displays "Zero".
Q9. Write a Python program to print even numbers between 1 and 20.
Answer:
The following Python program uses a
for loop and the
modulus (%) operator to print all
even numbers between 1 and
20.
Program:
# Program to print even numbers between 1 and 20
for i in range(1, 21):
if i % 2 == 0:
print(i)
Output:
2
4
6
8
10
12
14
16
18
20
Explanation:
-
The
range(1, 21)function generates numbers from 1 to 20. -
The
forloop checks each number one by one. -
The condition
i % 2 == 0checks whether the number is divisible by 2. -
If the condition is True, the number is even and is
displayed using the
print()function.
Q10. Explain Nested Loop with an example.
Answer:
A Nested Loop is a loop placed inside another loop. The inner loop executes completely for each iteration of the outer loop.
Python allows both for loops and
while loops to be nested inside one
another.
Syntax:
for outer_variable in sequence:
for inner_variable in sequence:
# Statements
Example:
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Explanation:
- The outer loop runs from 1 to 3.
- For each iteration of the outer loop, the inner loop also runs from 1 to 3.
-
The values of
iandjare printed in each iteration, producing all possible combinations.


