[go: up one dir, main page]

0% found this document useful (0 votes)
8 views4 pages

Day 15 Python Answers - 59533843 - 2025 - 04 - 26 - 06 - 18

Python questions answers

Uploaded by

anshu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Day 15 Python Answers - 59533843 - 2025 - 04 - 26 - 06 - 18

Python questions answers

Uploaded by

anshu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

HPSC PGT(CS)- Python Subjective Series

Day 15
Syllabus Coverage

Flow of Control

- Use of indentation.

- Sequential, conditional, and iterative flow control.

- Conditional statements: `if`, `if-else`, `if-elif-else`.

- Simple programs: absolute value, sorting 3 numbers, divisibility of a number.

- Iterative statements: `for` loop, `while` loop, `range` function.

- Flowcharts for loops.

- `break` and `continue` statements, nested loops.

- Suggested programs: generating patterns, summation of series, factorial of a number.

Question of the day:


1. Explain the use of break and continue statements in loops with
examples. 5M
2. What is a nested loop? Give an example where nested loops are useful.
5M
1. Use of break and continue Statements in Loops

break Statement

• Purpose: Terminates the loop immediately when encountered.

• Use Case: Exiting a loop when a certain condition is met.

Example:

for num in range(1, 10):

if num == 5:

break # Loop stops when num=5

print(num)

Output:

2
3

(Loop exits at num=5 and does not print further.)

continue Statement

• Purpose: Skips the current iteration and moves to the next.

• Use Case: Bypassing specific values in a loop.

Example:

for num in range(1, 6):

if num == 3:

continue # Skips num=3

print(num)

Output:

(3 is skipped, but the loop continues.)

2. Nested Loops & Their Use Cases

Definition:

A nested loop is a loop inside another loop. The inner loop completes all its iterations for
each iteration of the outer loop.

Example (Printing a 2D Pattern):

for i in range(3): # Outer loop (rows)

for j in range(3): # Inner loop (columns)

print(f"({i},{j})", end=" ")

print() # New line after each row

Output:

(0,0) (0,1) (0,2)

(1,0) (1,1) (1,2)

(2,0) (2,1) (2,2)


Where Nested Loops Are Useful:

1. Pattern Printing (e.g., pyramids, matrices).

2. Multi-dimensional Data Processing (e.g., 2D lists, tables).

3. Grid-based Operations (e.g., board games like Tic-Tac-Toe).

4. Nested Searching (e.g., comparing elements in two lists).

Program of the day:


1. Print a pyramid pattern: 10M
*
***
*****
*******

Pyramid Pattern Program in Python


Approach:

1. Use nested loops:

o Outer loop → Controls the rows.

o First inner loop → Prints leading spaces (for left alignment).

o Second inner loop → Prints the stars (*).

2. Key Logic:

o Spaces decrease as we move down the rows.

o Stars increase in odd numbers (1, 3, 5, ...).

Program:

n = 4 # Number of rows

for i in range(1, n + 1):

# Print leading spaces

for j in range(n - i):

print(" ", end=" ")

# Print stars
for k in range(2 * i - 1):

print("*", end=" ")

# Move to the next line

print()

Output:

***

*****

*******

Explanation:

1. Loop 1 (i) → Runs for each row (1 to 4).

2. Loop 2 (j) → Prints (n - i) spaces (e.g., 4-1=3 spaces in Row 1).

3. Loop 3 (k) → Prints (2*i - 1) stars (e.g., 2*1-1=1 star in Row 1, 2*2-1=3 in Row 2, etc.).

4. print() → Moves to the next line after each row.

Alternative (Using String Multiplication):

n=4

for i in range(1, n + 1):

print(" " * (n - i) + "* " * (2 * i - 1))

Output :

***

*****

*******

You might also like