Python Ppt
Python Ppt
PYTHON
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
TYPES OF
LOOPS IN
PYTHON!
PAGE 3
A for loop in Python is a control flow
statement used to iterate over a
sequence of elements (like a list,
tuple, string, or dictionary) and
execute a block of code for each
element. It's a powerful tool for
FOR
performing repetitive tasks
efficiently.
LOOP
Python For Loop Syntax
for var in iterable:
# statements
pass
PAGE 4
Flowchart of Python For Loop
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 in the program is
WHILE
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 example, a for loop can Nested
be inside a while loop or vice versa. Loops in
Nested Loops Syntax:
Python
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 condition is
met. They are essential for
LOOP
efficiently performing repetitive CONTROL
tasks like processing arrays, STATEMENTS
generating 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.
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.
Example code:
Output:
for i in range(1, 10):
if i == 5: 1234
break
print(i)
PAGE 13
Thank you!
PAGE 14