Python Loops With Pattern Problems
Python Loops With Pattern Problems
count = 0
while (count < 3):
count+=1
print("Hello Geek")
Output
Hello Geek
Hello Geek
Hello Geek
Control Statements 1
While Loop Syntax 🔄
condition: This is a boolean expression. If it evaluates to True, the code
inside the loop will execute.
statement(s): These are the statements that will be executed during each
iteration of the loop.
Example:
Control Statements 2
The while loop will continue running the code block as long as the condition
evaluates to True. Each time the loop executes, the condition is checked
again. If it is True, the loop continues; if it is False, the loop terminates, and
the program moves to the next statement after the loop.
age = 28
Control Statements 3
# the test condition is always Truewhile age > 19:
print('Infinite Loop')
For Loops
Python For Loops are used for iterating over a sequence like lists, tuples,
strings, and ranges.
For loop allows you to apply the same operation to every item within loop.
Using For Loop avoid the need of manually managing the index.
For loop can iterate over any iterable object, such as dictionary, list or any
custom iterators.
Control Statements 4
A for loop repeats statements as long as the last item in the range has not been
reached yet.
Control Statements 5
for i in range(5):
print(i, end=" ")
print()
Output:
01234
Parameters:
1. start: (Optional) The starting value of the sequence. Default is 0.
2. stop: The end value of the sequence (not included in the range).
Control Statements 6
Python range (start, stop)
When the user call range() with two arguments, the user gets to decide not
only where the series of numbers stops but also where it starts, so the user
doesn’t have to start at 0 all the time. Users can use range() to generate a
series of numbers from X to Y using range(X, Y).
Control Statements 7
Output:
2
3
4
5
Control Statements 8
Output:
1
3
5
7
9
Explanation: The loop starts at 1, stops before 10, and increments by 2 each
time.
Reverse Range:
Output:
10
9
8
7
6
5
4
3
2
1
Explanation: The loop starts at 10, stops before 0, and decrements by 1 each
time.
Control Statements 9
Break Statement-
The break statement in Python is used to exit or “break” out of a loop (either a
for or while loop) prematurely, before the loop has iterated through all its items
or reached its condition. When the break statement is executed, the program
immediately exits the loop, and the control moves to the next line of code after
the loop.
a = [1, 3, 5, 7, 9, 11]
val = 7
for i in a:
if i == val:
print(f"Found at{i}!")
breakelse:
print(f"not found")
Output
Found at 7!
If the loop completes without finding the number, the else block is
executed.
Control Statements 10
continue statement
The continue statement is a loop control statement that forces the execution of
the next iteration of the loop while skipping the rest of the code inside the loop
for the current iteration only.
while True:
...
if x == 10:
continue
print(x)
Pass Statement-
Pass statement in Python is a null operation or a placeholder. It is used when a
statement is syntactically required but we don’t want to execute any code. It
does nothing but allows us to maintain the structure of our program.
def fun():
pass# Placeholder, no functionality yet
# Call the function
fun()
Explanation:
function fun() is defined but contains the pass statement, meaning it does
nothing when called.
Control Statements 11
program continues execution without any errors and the message is printed
after calling the function.
for i in range(5):
if i == 3:
pass
print(i)
Output
0
1
2
3
4
Pattern Printing-
Control Statements 12
1. Problem ko samajhna
Sabse pehle, pattern ko dhyan se dekho.
Space (spaces before stars or numbers) kis tarah arrange hote hain?
Stars ya numbers?
3. Decide loops
Outer loop: Yeh rows ko handle karega.
Har row me kitne elements print hone hain? Yahi inner loop decide
karega.
Agar Right-aligned pattern hai, toh spaces pehle print hote hain.
6. Reverse patterns
Agar reverse pattern chahiye, toh aap outer loop ko decreasing direction
me run kar sakte ho ( n to 1 ), aur inner loop ko decreasing order me print kar
Control Statements 13
sakte ho.
Output-
Output-
Control Statements 14
3.Number Right Aligned Pattern-
Output-
Control Statements 15
Output-
Output-
Control Statements 16
6.Reversed Star Pattern-
6.Pyramid Pattern-
Control Statements 17
7.Reversed Pyramid Pattern-
Control Statements 18
8.Right Pyramid Pattern-
Control Statements 19
10.Sandglass Pattern-
Control Statements 20
11.Diamond Pattern-
Control Statements 21
12.Palindrome Pyramid Pattern-
Control Statements 22
13.Palindrome Diamond Pyramid Number Pattern-
Control Statements 23