Chapter 6
Chapter 6
R
L
VSA 1 MARK SA 2 MARKS LA 3 MARKS ESSAY 5 MARKS TOTAL
N
1 MCQ 1 NIL 1 08 MARKS
EE
CHAPTER SYNOPSIS
H
AT
Introduction
M
Explanation: This program takes two integer inputs from the user, calculates their difference, and prints the
O
Selection
• Syntax:
if condition:
statement(s)
183
August 24, 2024
```python
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
R
Explanation: This program checks if the user’s age is 18 or above to determine voting eligibility. If the condition
L
(age >= 18) is true, it prints “Eligible to vote”; otherwise, it prints “Not eligible to vote.”
N
• if..else Statement: Provides two paths based on whether the condition is true or false.
EE
• Syntax:
if condition:
statements-1
H
AT
else:
statements-2
M
Explanation: This program calculates the positive difference between two input numbers by comparing them
H
and subtracting the smaller number from the larger one. It demonstrates the use of an if..else statement to
O
• syntax:
if condition1:
statements-1
elif condition2:
statements-2
.
.
.
elif condition-n:
statements-n
else:
statements-else
R
# Program to create a four function calculator
result = 0
val1 = float(input("Enter value 1: "))
L
val2 = float(input("Enter value 2: "))
N
op = input("Enter any one of the operator (+,-,*,/): ")
if op == "+":
EE
result = val1 + val2
elif op == "-":
result = val1 - val2
elif op == "*":
H
AT
result = val1 * val2
elif op == "/":
if val2 == 0:
M
_Explanation: This program performs basic arithmetic operations (+, -, *, /) on two input numbers based on the
M
user’s choice of operator. It demonstrates the use of if..elif..else statements to handle multiple conditions._
A
H
Indentation
O
• Indentation: Python uses indentation to define blocks of code. Proper indentation is crucial to avoid
M
syntax errors.
else:
print("Second number is larger")
print("Bye Bye")
Explanation: This program compares two numbers and prints which one is larger. The example emphasizes the
importance of indentation in Python to define blocks of code within if and else statements.
Repetition
R
• Iteration: Repetition of a set of statements using looping constructs.
• For Loop: Executes a block of code a specific number of times.
L
• Syntax:
N
for variable in range or sequence:
EE
statements
Explanation: This program uses a for loop to iterate over each character in the string ‘PYTHON’ and prints
them. It demonstrates the basic usage of the for loop.
ED
print(num)
A
Explanation: This program iterates over a list of numbers and prints each number. It shows how the for loop
H
Explanation: This program checks each number in the list to see if it’s even and prints the even numbers. It
combines a for loop with an if statement to filter elements based on a condition.
Explanation: This program prints the multiples of 10 for numbers from 1 to 4 using the range function in a for
loop. It demonstrates how to use the range function to generate a sequence of numbers.
range() Function
R
• range() Function: Generates a sequence of numbers. Useful for looping a specific number of times.
L
• Syntax:
N
range(start,stop,step)
EE
Example: Range Function (Program 6-10)
Explanation: This program uses the range function to print numbers from 1 to 5. The range function generates
a sequence of numbers from 1 to 5, which the for loop iterates over.
ED
print(i)
M
Explanation: This program prints numbers from 5 to 1 in reverse order using the range function with a negative
A
step. It demonstrates how the range function can be customized to generate sequences with different steps.
H
while Loop
O
M
• while Loop: Executes a block of code as long as the condition is true. Useful for situations where the
number of iterations is not known in advance.
• Syntax:
while condition:
statements
i = 5
while i > 0:
print(i)
i -= 1
Explanation: This program uses a while loop to count down from 5 to 1. The loop continues to execute as long
as the condition (i > 0) is true.
Example: Sum of First N Natural Numbers (Program 6-13)
# Using while loop to find sum of first n natural numbers
R
n = int(input("Enter a number: "))
sum = 0
L
i = 1
while i <= n:
N
sum += i
EE
i += 1
print("Sum of first", n, "natural numbers is", sum)
H
Explanation: This program calculates the sum of the first n natural numbers using a while loop. The loop
iterates from 1 to n, adding each number to the sum.
AT
break and continue Statements
M
• break Statement: Terminates the loop prematurely. Useful for breaking out of a loop based on a condi-
ED
tion.
if i == 6:
break
A
print(i)
H
Explanation: This program uses a for loop to print numbers from 1 to 10 but breaks out of the loop when i
O
equals 6. The break statement exits the loop when the condition (i == 6) is met.
M
• continue Statement: Skips the current iteration and continues with the next. Useful for skipping specific
iterations based on a condition.
Explanation: This program uses a for loop to print odd numbers from 1 to 10 by skipping even numbers. The
continue statement skips the current iteration if the condition (i % 2 == 0) is true.
Nested Loops
A loop inside another loop. Useful for iterating over multi-dimensional structures like matrices
R
for i in range(1, 4):
for j in range(1, 4):
L
print(i, j)
N
Explanation: This program demonstrates nested loops by printing pairs of numbers from 1 to 3.
EE
Example: Multiplication Table (Program 6-17)
Explanation: This program uses nested loops to print a multiplication table from 1 to 5.
ED
a) Execution flow
M
b) Control flow
A
c) Flow of control
H
d) Program sequence
O
a) Curly brackets
b) Parentheses
c) Indentation
d) Semicolon
Answer: c) Indentation
R
4. What is used to implement decision-making in Python?
L
a) for loop
b) if..else statement
N
c) while loop
EE
d) switch statement
a) Skips an iteration
M
a) Recursive loop
b) Nested loop
c) Inner loop
d) Infinite loop
R
Answer: a) ‘for i in range(3): for j in range(2): print(j)‘
L
9. What happens if indentation is incorrect in a Python program?
N
b) Program skips the indented block
EE
c) Syntax error
d) Indentation error
H
AT
Answer: c) Syntax error
b) Using indentation
ED
Python Code:
H
num1 = 5
O
num2 = 6
M
print(“Bye”)
else:
print(“Bye Bye”)
a) Syntax error
d) ‘Bye Bye‘
R
Python Code:
L
for i in range(1, 6):
if i == 3:
N
continue
EE
print(i)
a) 1 2 3 4 5
H
AT
b) 1 2 4 5
c) 1 2 4 5 6
M
d) 2 4 6
Answer: b) 1 2 4 5
ED
13. Which statement is used to skip the rest of the code inside a loop for the current iteration only?
a) break
M
b) pass
M
c) skip
A
d) continue
H
Answer: d) continue
O
Python Code:
while True:
print(“Infinite loop”)
a) for loop
b) finite loop
c) infinite loop
d) nested loop
15. Which of the following is the correct syntax for an if..else statement in Python?
R
d) ‘if condition: { statements } else: { statements }‘
L
18. Which of the following programs will print the sum of all positive numbers entered by the user until
N
a negative number is entered?
EE
a)
Python Code:
sum = 0
H
AT
while True:
if num < 0:
ED
break
sum += num
M
print(“Sum:”, sum)
b)
M
Python Code:
A
sum = 0
H
while True:
O
if num < 0:
continue
sum += num
print(“Sum:”, sum)
c)
Python Code:
sum = 0
sum += num
print(“Sum:”, sum)
R
d)
Python Code:
L
sum = 0
N
while num >= 0:
EE
num = int(input(“Enter a number:”))
if num < 0:
break H
AT
sum += num
M
print(“Sum:”, sum)
Answer: a)
ED
Python Code:
sum = 0
M
while True:
M
if num < 0:
A
break
H
sum += num
O
print(“Sum:”, sum)
M
19. In a nested loop, how many times will the inner loop execute if the outer loop runs 3 times and the
inner loop runs 2 times each iteration?
a) 3
b) 6
c) 2
d) 5
Answer: b) 6
b) By using parentheses
c) By using indentation
d) By using semicolons
R
Answer: c) By using indentation
L
21. What type of error will you get if the indentation is not consistent in Python?
a) Logical error
N
b) Syntax error
EE
c) Runtime error
d) Semantic error
H
AT
Answer: b) Syntax error
Python Code:
for i in range(5):
ED
if i == 2:
continue
M
print(i)
M
a) 0 1 2 3 4
A
b) 1 2 3 4 5
H
c) 0 1 3 4
O
d) 0 1 3 4 5
M
Answer: c) 0 1 3 4
a) Using operators
c) Using functions
d) Using variables
c) A secondary condition
R
Answer: a) Code that runs if the condition is false
L
25. What does the following code do?
Python Code:
N
x=5
EE
y = 10
if x < y:
H
AT
print(“x is less than y”)
else:
M
c) Prints nothing
M
d) Syntax error
M
d) To generate an error
27. Which of the following is true about the break statement in a loop?
Python Code:
for i in range(4):
R
print(i)
L
if i == 2:
break
N
a) 0 1 2 3
EE
b) 0 1 2
c) 0 1 3
H
AT
d) 0 1
Answer: b) 0 1 2
M
b) Skips the rest of the code inside the loop for the current iteration
Answer: b) Skips the rest of the code inside the loop for the current iteration
A
30. What is the difference between a for loop and a while loop?
H
a) For loop is used for definite iteration, while loop is used for indefinite iteration
O
b) For loop has a fixed number of iterations, while loop runs indefinitely
M
c) For loop is used for selection, while loop is used for repetition
d) There is no difference
Answer: a) For loop is used for definite iteration, while loop is used for indefinite iteration
Python Code:
count = 0
count += 1
print(count)
a) 0 1 2 3 4 5
b) 1 2 3 4 5
c) 0 1 2 3 4
R
d) 1 2 3 4
L
Answer: b) 1 2 3 4 5
32. How many times will the loop run in the following code?
N
Python Code:
EE
for i in range(3):
for j in range(2):
H
AT
print(i, j)
a) 3 times
M
b) 6 times
c) 2 times
ED
d) 5 times
Answer: b) 6 times
M
a) exit
A
b) break
H
c) continue
O
d) stop
M
Answer: b) break
Python Code:
i=0
while i < 4:
i += 1
print(i)
if i == 2:
continue
a) 1 2 3 4
b) 1 2 4
c) 1 2 3 4 5
R
d) 1 2 2 3 4
L
Answer: a) 1 2 3 4
N
a) To create infinite loops
EE
b) To break out of a loop
H
c) To perform repeated operations on each element of a sequence
AT
d) To skip iterations in a loop
36. Which statement correctly describes the functionality of break and continue in Python?
Answer: a) break terminates the loop, continue skips the current iteration
A
Python Code:
O
for i in range(3):
M
for j in range(2):
if j == 1:
break
print(i, j)
a) 0 0 0 1 1 0 1 1 2 0 2 1
b) 0 0 1 0 2 0
c) 0 0 0 1 1 0 2 0 2 1
d) 0 0 1 0 1 1 2 0 2 1
Answer: b) 0 0 1 0 2 0
38. Which of the following is a correct syntax for a while loop in Python?
R
c) ‘while condition: statements‘
L
d) ‘while (condition): statements‘
N
39. What happens when the break statement is executed inside a nested loop?
EE
a) Exits all loops
40. Which of the following is the correct syntax for a for loop in Python?
ED
b) Using indentation
c) Using parentheses
d) Using semicolons
Python Code:
count = 0
print(count)
count += 1
a) 0 1 2 3
b) 1 2 3
R
c) 0 1 2
L
d) 0 1
Answer: c) 0 1 2
N
43. Which of the following statements will break out of a loop?
EE
a) exit
b) continue
H
AT
c) stop
d) break
M
Answer: d) break
b) To skip the rest of the code inside the loop for the current iteration
M
Answer: b) To skip the rest of the code inside the loop for the current iteration
H
Python Code:
M
for i in range(3):
for j in range(2):
if j == 1:
break
print(i, j)
R
Python Code:
L
for i in range(5):
if i == 3:
N
break
EE
print(i)
else:
H
AT
print(“Completed”)
a) 0 1 2 Completed
M
b) 0 1 2
c) 0 1 2 3 4 Completed
ED
d) 0 1 2 3 Completed
Answer: b) 0 1 2
M
Python Code:
A
for i in range(2):
H
for j in range(2):
O
print(i, j)
M
a) 0 0 1 0
b) 0 0 1 1
c) 0 0 0 1 1 0 1 1
d) 0 0 1 0 1 1
Answer: c) 0 0 0 1 1 0 1 1
a) It runs indefinitely
R
Python Code:
for i in range(3):
L
if i == 1:
N
break
EE
print(i)
else:
print(“Loop completed”) H
AT
a) 0 1 Loop completed
M
b) 0 Loop completed
c) 0 1
ED
d) 0
Answer: d) 0
M
c) By using indentation
H
• Answer: The flow of control in programming refers to the order in which individual statements,
instructions, or function calls are executed or evaluated. In Python, this can be implemented using
control structures such as selection and repetition .
• Answer: In Python, a sequence refers to the execution of statements one after another in a specific
order. This is the simplest form of flow control where the program runs from the beginning to the
end, following the sequence of statements written in the code .
• Answer: A selection structure in Python is a control structure that allows the execution of certain
R
parts of the code based on the evaluation of a condition. The primary selection structures are if,
if..else, and if..elif..else statements .
L
4. How is indentation used in Python?
N
• Answer: Indentation in Python is used to define the level of nesting of statements. It is crucial
EE
because it indicates a block of code, such as the body of a function, loop, or conditional statement.
Proper indentation ensures code readability and logical grouping of statements .
if condition:
statement(s)
ED
This structure evaluates the condition, and if it is true, the indented statements are executed .
M
age = 20
H
print("Eligible to vote")
else:
M
This code checks if the age is 18 or older and prints the appropriate message .
• Answer: The elif statement in Python allows you to check multiple conditions sequentially. It
stands for “else if” and is used to test several conditions one after another, executing the block of
code associated with the first true condition:
if condition1:
statement(s)
elif condition2:
statement(s)
else:
statement(s)
• Answer: Python handles nested if statements by allowing an if or else statement inside another if
R
or else statement. This enables more complex decision-making processes. For example:
L
num = 10
if num >= 0:
N
if num == 0:
print("Zero")
EE
else:
print("Positive number")
else:
print("Negative number")
H
AT
9. What is the purpose of the break statement in loops?
• Answer: The break statement in Python is used to exit a loop prematurely when a specific condition
M
is met. It is useful for terminating the loop early and transferring control to the statement immediately
following the loop .
ED
• Answer: The continue statement in Python is used to skip the current iteration of a loop and proceed
to the next iteration. It is useful for skipping certain conditions without terminating the entire loop .
M
• Answer: Repetition structures in Python are implemented using loops, such as for loops and while
H
loops. These structures allow a block of code to be executed repeatedly based on a condition or a
sequence of values .
O
M
12. What are the two types of control structures supported by Python?
• Answer: The two types of control structures supported by Python are selection and repetition. Se-
lection structures include if, if..else, and if..elif..else statements, while repetition structures include
for and while loops .
• Answer: A selection structure would be used in a scenario where a decision needs to be made based
on a condition. For example, determining if a user is eligible to vote based on their age:
• Answer: Python enforces proper indentation by treating the level of indentation as part of its syntax.
Improper indentation will result in a syntax error, ensuring that the code blocks are clearly defined
R
and organized .
L
15. How do you read input from the user in Python?
N
• Answer: To read input from the user in Python, you use the input() function. For example:
EE
name = input("Enter your name: ")
print("Hello, " + name)
H
16. What is the importance of the if condition in an if..else structure?
AT
• Answer: The if condition in an if..else structure is crucial because it determines which block of
code will be executed. If the condition is true, the code within the if block is executed; otherwise,
M
• Answer: A simple calculator program in Python can be implemented using if..else statements to han-
dle different operations. For example: “‘python num1 = float(input(“Enter first number:”)) num2
M
if operation == “add”: result = num1 + num2 elif operation == “subtract”: result = num1 - num2 elif operation
A
== “multiply”: result = num1 * num2 elif operation == “divide”: result = num1 / num2 else: result = “Invalid
operation”
H
print(“Result:”, result)
O
M
1. **Write a Python program to find the positive difference between two numbers.**
* **Answer:** Here is a Python program to find the positive difference between two numbers:
```python
R
L
2. Describe the role of control structures in Python programming.
• Answer: Control structures in Python programming manage the flow of execution based on condi-
N
tions and loops. They include:
EE
– Selection structures: if, if..else, and if..elif..else statements that execute code blocks based on
boolean conditions.
H
– Repetition structures: for and while loops that repeat code blocks multiple times.
AT
– Control transfer: break, continue, statements that alter the flow within loops and conditional
statements.
M
– Defines blocks of code: Indentation is used to group statements into blocks that form loops,
M
– Ensures readability: Proper indentation makes the code more readable and maintainable.
– Syntax requirement: Python enforces indentation as part of its syntax; incorrect indentation
A
• Answer: Nested if statements are if statements inside another if statement. This allows for checking
M
if num > 0:
print("The number is positive.")
if num % 2 == 0:
print("It is an even number.")
else:
5. What are the key differences between if, if..else, and if..elif statements?
if condition:
R
# code block
L
• if..else statement: Executes a block of code if a condition is true; otherwise, executes another block of
N
code.
EE
if condition:
# code block if condition is true
else:
# code block if condition is false H
AT
• if..elif..else statement: Checks multiple conditions sequentially, executing the corresponding block for
M
the first true condition. The else block executes if no conditions are true.
ED
if condition1:
elif condition2:
M
A
else:
O
M
• Answer:
if num > 0:
print("The number is positive.")
• Answer: Logical operators in Python (and, or, not) are used in selection structures to combine multiple
conditions:
R
– and: Returns True if all conditions are true. “‘ if condition1 and condition2:
# code block
L
N
* * **or****:** Returns True if at least one condition is true.
EE
if condition1 or condition2:
# code block
H
AT
* **not****:** Inverts the boolean value of a condition.
M
if not condition:
# code block
ED
* **Answer:** Multiple conditions can be handled using elif to create a chain of conditions:
A
```python
H
if num > 0:
M
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
– Handles all other cases: It executes code when none of the preceding if or elif conditions are
true.
– Provides default actions: Ensures that there is a defined action or output for inputs that don’t
match any specific condition.
R
for i in range(5):
L
print(i)
N
• while loop: Repeats as long as a condition is true.
EE
i = 0
while i < 5:
print(i)
i += 1
H
AT
11. How does the break statement alter the flow of a loop?
– Exits the loop prematurely: Immediately terminates the loop when encountered, regardless
ED
– Used to avoid unnecessary iterations: Helps in cases where the desired outcome is achieved
before the loop completes its natural cycle.
M
– Improves efficiency: Can reduce the number of iterations, making the loop more efficient.
M
if i == 5:
H
break
O
print(i)
M
if i == 5:
break
print(i)
# Output: 0, 1, 2, 3, 4
R
• continue: Skips the current iteration and proceeds with the next iteration.
L
for i in range(10):
if i == 5:
N
continue
EE
print(i)
# Output: 0, 1, 2, 3, 4, 6, 7, 8, 9
H
13. Write a Python program to print the first ten natural numbers using a loop.
AT
• Answer:
print(i)
5 MARKS QUESTIONS
ED
1. Write a Python program to create a simple calculator that performs basic arithmetic operations.
M
result = 0
val1 = float(input("Enter value 1: "))
M
if op == "+":
H
elif op == "-":
if val1 > val2:
M
This program accepts two numbers and an operator from the user, performs the corresponding arithmetic oper-
ation, and handles errors like division by zero.
R
The flow of control refers to the order in which individual statements, instructions, or function calls are executed
L
or evaluated within a program. It is implemented using control structures, which include:
N
• Sequential: Executes statements one after another.
EE
• Selection: Uses conditions to decide which statement to execute (e.g., if, elif, else).
print("Process")
print("End")
ED
Example of selection:
M
print("Eligible to vote")
H
else:
O
Example of repetition:
for i in range(5):
print(i)
3. How can you implement a nested loop structure in Python? Provide an example.
A nested loop is a loop within another loop. Python allows any type of loop (for/while) to be nested inside
another loop.
for i in range(3):
for j in range(2):
R
print(j + 1)
L
print("Out of inner loop")
N
print("Out of outer loop")
EE
Output:
2
M
1
M
1
H
2
O
4. Describe the importance of control structures in managing the flow of a Python program.
Control structures are essential in programming as they dictate the flow of control through the program, enabling
decision making, looping, and branching. They help in:
• Structuring code: By organizing code into logical blocks, making it more readable and maintainable.
Examples include:
R
Control structures ensure that programs can handle complex logic and repetitive tasks efficiently, making the
L
code more robust and flexible.
N
5. Write a detailed explanation of the different types of selection structures in Python.
EE
Selection structures allow the execution of specific blocks of code based on certain conditions. Python supports
the following selection structures:
H
1. if statement: Executes a block of code if the condition is true.
AT
if condition:
M
# block of code
2. if..else statement: Executes one block of code if the condition is true and another block if it is false.
ED
if condition:
M
3. if..elif..else statement: Checks multiple conditions in sequence and executes the corresponding block of
H
if condition1:
# block of code if condition1 is true
elif condition2:
# block of code if condition2 is true
else:
# block of code if none of the above conditions are true
Example:
R
6. How does Python handle multiple conditions using if..elif..else? Provide a code example.
Python handles multiple conditions using the if..elif..else statement by checking each condition in sequence. The
L
first condition that evaluates to true will execute its corresponding block of code, and the rest will be skipped.
N
Example:
EE
signal = input("Enter the colour: ")
if signal == "red" or signal == "RED":
print("STOP")
elif signal == "orange" or signal == "ORANGE":
H
AT
print("Be Slow")
elif signal == "green" or signal == "GREEN":
print("Go!")
M
else:
print("Invalid input")
ED
This program takes a traffic signal color as input and prints the corresponding action.
7. Describe the use of loops in Python with examples of for and while loops.
M
Loops in Python are used to execute a block of code repeatedly until a certain condition is met.
M
for loop: Iterates over a sequence (like a list, tuple, dictionary, set, or string).
A
for i in range(5):
H
print(i)
O
Output:
M
count = 0
print(count)
count += 1
Output:
R
0
L
2
N
3
EE
4
Loops are fundamental for tasks that require repetition, such as processing items in a list, reading lines from a
file, or implementing complex iterative algorithms.
H
AT
9. Write a Python program to print all prime numbers between 1 and 50.
is_prime = True
for i in range(2, int(num ** 0.5) + 1):
ED
if num % i == 0:
is_prime = False
break
M
if is_prime:
print(num)
M
This program checks each number from 2 to 50 to see if it has any divisors other than 1 and itself, and prints the
A
number if it is prime.
H
Indentation in Python is crucial as it defines the structure and flow of the code. Unlike many other programming
languages that use braces to define blocks of code, Python uses indentation to group statements. Each level of
M
indentation corresponds to a level of nesting. Python will throw an error if the indentation is not consistent. For
example:
num1 = 5
num2 = 6
if num1 > num2: # Block1
print("first number is larger")
print("Bye")
else: # Block2
In this example, the correct indentation ensures that the statements under if and else are part of their respective
blocks.
12. Provide a detailed explanation of the continue statement and its applications with examples.
The continue statement in Python is used to skip the rest of the code inside a loop for the current iteration only.
The loop does not terminate but continues with the next iteration. This is useful in situations where you want
R
to avoid executing part of the loop when a certain condition is met but still continue with the loop.
Example:
L
for num in range(2, 10):
N
if num % 2 == 0:
EE
continue
print(num) H
AT
Output:
M
5
ED
9
M
In this example, the loop skips the print statement when the number is even and continues with the next iteration
.
M
15. Write a Python program to simulate a basic login system, validating user input for username and
A
password.
H
correct_username = "admin"
O
correct_password = "password123"
M
print("Login successful!")
else:
This program prompts the user to enter a username and password and checks if they match the predefined correct
credentials. If they do, it prints a success message; otherwise, it prints an error message.
R
L
EXERCISES (CHAPTER END QUESTIONS)**
N
1. What is the difference between else and elif construct of if statement?
EE
• The else construct provides an alternative set of statements that execute if none of the preceding if
or elif conditions are true. The elif construct, short for “else if,” allows for additional conditions to
H
be checked if the previous if or elif condition is false. Essentially, elif adds more conditional checks,
AT
while else handles the case where no conditions are met .
• The range() function in Python is used to generate a sequence of numbers. It is often used in for
loops to iterate over a sequence of numbers. The function can take up to three arguments: start, stop,
ED
print(i)
M
This will output: 0 2 4 6 8 In this example, range(0, 10, 2) generates numbers from 0 to 9, incrementing by 2
.
A
• The break statement is used to exit a loop immediately, regardless of the loop’s condition. The
O
continue statement skips the rest of the code inside the loop for the current iteration and moves to
M
for i in range(5):
if i == 3:
break
print(i)
Output: 0 1 2
Example of continue:
for i in range(5):
if i == 3:
continue
print(i)
Output: 0 1 2 4 In the first example, the loop exits when i is 3. In the second example, the iteration is skipped
when i is 3 .
R
• An infinite loop is a loop that never terminates because its condition is always true. This can lead
to a program hanging indefinitely. Example: “‘python while True:
L
print(“This is an infinite loop”)
N
This loop will print "This is an infinite loop" endlessly because the condition True is always
EE
5. **Find the output of the following program segments:**
H
AT
(i)
```python
M
a = 110
while a > 100:
ED
print(a)
a -= 2
M
Output: 20 22 24 26 28 (iii)
O
country = 'INDIA'
for i in country:
M
print(i)
Output: I N D I A
(iv)
i = 0
sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i = i + 2
print(sum)
Output: 12 (v)
R
print(x * y)
L
Output: 2 3 4 4 6 8 6 9 (vi)
var = 7
N
while var > 0:
EE
print('Current variable value: ', var)
var = var - 1
if var == 3:
break
H
AT
else:
if var == 6:
var = var - 1
M
continue
print("Good bye!")
ED
Output: Current variable value: 7 Current variable value: 5 Good bye! Current variable value: 4 Good bye!
Current variable value: 3
M
Important Questions
M
5. What is Repetition Control Structure? Explain its types with syntax and examples in Python.
M
R
L
N
EE
H
AT
M
ED
M
M
A
H
O
M