Loops in Python2.0
Loops in Python2.0
IN
PYTHO
N
BY – CHARU , MADHUR , ANSHUL , KUNAL
PAGE 1
WHAT ARE LOOPS??
A loop is a command that repeats a section of code until a desired outcome is achieved.
Loops are a fundamental programming concept that are used to: Save time,
Reduce errors, Make code more organized and manageable, Create dynamic
programs that can do many different things, and Shorten code that could be
hundreds of lines.
We can also say that a loop is use to repeat an instruction multiple times until
a condition goes wrong.
PAGE 2
FOR LOOP
TYPES
OF WHILE LOOP
LOOPS IN
PYTHON!
NESTED LOOPS
PAGE 3
A for loop in Python is a control flow FOR
statement used to iterate over a
sequence of elements (like a list, LOOP
tuple, string, or dictionary) and
execute a block of code for each
element. It's a powerful tool for
for var in iterable:
PAGE 5
Example with List, Tuple, String, and Dictionary
Iteration Using for Loops in Python
PAGE 6
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 WHILE
in the program is executed. LOOP
Syntax of while loop in
Python
while expression:
statement(s)
PAGE 7
Flowchart of Python While
Loop
PAGE 8
Examples of While Loop with else
statement:
PAGE 9
Python programming language
allows to use one loop inside
another loop which is called nested
loop.
loop nesting is that we can put any
type of loop inside of any other
type of loops in Python. For Nested
example, a for loop can be inside a
while loop or vice versa.
Loops in
Python
Nested Loops Syntax:
while expression:
while expression:
statement(s)
statement(s)
PAGE 10
Example: This Python code uses nested ‘for' loops to create a triangular
pattern of numbers. It iterates from 1 to 4 and, in each iteration, prints
the current number multiple times based on the iteration number. The
result is a pyramid-like pattern of numbers
PAGE 11
Loop control statements
are used to repeatedly execute
a block of code until a specific LOOP
condition is met. They are
essential for efficiently
CONTROL
performing repetitive tasks like STATEMENT
processing arrays, generating S
patterns, or automating
calculations.
PAGE 12
CONTINUE STATEMENT:
The continue statement in Python returns the control to the beginning of the loop. Jumps to the
next iteration of the loop, skipping any remaining code in the current iteration.
Output:
Example code:
for i in range(1, 10): 13579
if i % 2 == 0: # Skip even numbers
continue
print(i)
BREAK STATEMENT:
The break statement in Python brings control out of the loop . Immediately terminates the loop,
even if the loop condition is still true.
Output:
Example code:
for i in range(1, 10): 1234
if i == 5:
break
print(i) PAGE 13
Thank you!
PAGE 14