0% found this document useful (0 votes)
8 views

Python Loops With Pattern Problems

The document provides an overview of control statements in Python, focusing on loops such as For and While loops, including their syntax and usage. It explains how While loops execute until a condition is false, while For loops iterate over sequences, utilizing the range() function for generating number sequences. Additionally, it covers control statements like break, continue, and pass, and includes guidance on pattern printing using loops.

Uploaded by

bushu khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python Loops With Pattern Problems

The document provides an overview of control statements in Python, focusing on loops such as For and While loops, including their syntax and usage. It explains how While loops execute until a condition is false, while For loops iterate over sequences, utilizing the range() function for generating number sequences. Additionally, it covers control statements like break, continue, and pass, and includes guidance on pattern printing using loops.

Uploaded by

bushu khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Control Statements

Loops in Python – For, While and Nested Loops


Loops in Python are used to repeat actions efficiently. The main types are For
loops (counting through items) and While loops (based on conditions).
Additionally, Nested Loops allow looping within loops for more complex tasks.

While Loop in Python


Python While Loop is used to execute a block of statements repeatedly until a
given condition is satisfied. When the condition becomes false, the line
immediately after the loop in the program is executed.

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:

While Loop Flowchart

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.

Infinite while Loop in Python


Here, the value of the condition is always True. Therefore, the body of the loop
is run infinite times until the memory is full.

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.

For Loop Example:

Flowchart of Python For Loop

Control Statements 4
A for loop repeats statements as long as the last item in the range has not been
reached yet.

Using range() with For Loop-


The range() function is commonly used in for loops to generate a sequence of
numbers.
The
range() function returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and stops before a specified number.
Example-

In the given example, we are printing the number from 0 to 4.

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).

3. step: (Optional) The difference between each number in the sequence.


Default is 1.

Python range (stop)


When the user call range() with one argument, the user will get a series of
numbers that starts at 0 and includes every whole number up to, but not
including, the number that the user has provided as the stop.

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).

Specifying start and stop :

Control Statements 7
Output:

2
3
4
5

Python range (start, stop, step)


When the user call range() with three arguments, the user can choose not only
where the series of numbers will start and stop, but also how big the difference
will be between one number and the next.

Using start , stop , and step :

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!

The loop iterates through each number in the list.

When the number 7 is found, it prints a confirmation message and executes


break, exiting the loop immediately.

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

NOTE:- PYTHON DOES NOT HAVE DO-WHILE LOOP.

Pattern Printing-

Control Statements 12
1. Problem ko samajhna
Sabse pehle, pattern ko dhyan se dekho.

Rows aur columns ka structure samajh lo.

Kitni rows hain?

Har row me kitne characters honge?

Space (spaces before stars or numbers) kis tarah arrange hote hain?

2. Identify pattern type


Right-aligned ya Left-aligned?

Stars ya numbers?

Increasing ya decreasing numbers/stars?

3. Decide loops
Outer loop: Yeh rows ko handle karega.

Agar increasing rows hain, toh outer loop 1 to n chalega.

Agar decreasing rows hain, toh outer loop n to 1 chalega.

Inner loop: Yeh columns ko handle karega.

Har row me kitne elements print hone hain? Yahi inner loop decide
karega.

4. Handling spaces (alignment)


Spaces ka use tab hota hai jab aapko pattern ko aligned rakhna hota hai.

Agar Right-aligned pattern hai, toh spaces pehle print hote hain.

Agar Left-aligned hai, toh spaces baad me nahi chahiye.

5. Use end=" " to avoid new line


default me new line print karta hai, lekin agar aap chahte ho ki sab
print()

kuch ek line me print ho, toh end=" " ka use karo.

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.

1.Left Aligned Star Pattern-

Output-

2.Right Aligned Star Pattern-

Output-

Control Statements 14
3.Number Right Aligned Pattern-

Output-

4.Reversed Number Pattern-

Control Statements 15
Output-

5.Square Star Pattern-

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-

9.Alternate Numbers 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

You might also like