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

Loop Concept

Uploaded by

Arnav Rajesh
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 views9 pages

Loop Concept

Uploaded by

Arnav Rajesh
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/ 9

DAY-3

Topics Covered:
• Loop concept (for,while)
• Break, Continue statement

In Python, loops are structures that allow you to repeatedly execute a


block of code. There are two main types of loops: `for` loops and
`while` loops.

`for` loops:
A `for` loop is used for iterating over a sequence (that is either a list,
tuple, dictionary, string, or range). The basic syntax is as follows:

Here, `variable` takes on each value in the `sequence`, and the


indented code block is executed for each iteration.

Example:

(prints numbers from 1 to 5)


Range function
The `range()` function in Python is commonly used with `for` loops to
generate a sequence of numbers. Here's more information about the
`range()` function and its use in `for` loops:

Basic Syntax:

The `range()` function has three forms:

1. `range(stop)`: Generates a sequence of numbers from 0 up to (but


not including) the specified `stop` value.

Output:

2. `range(start, stop)`: Generates a sequence of numbers from `start`


up to (but not including) the specified `stop` value.

2
Output:

3. `range(start, stop, step)`: Generates a sequence of numbers from


`start` up to (but not including) the specified `stop` value, with a
specified `step` between each number.

3
Output:

Important Points:

• The `start` parameter is optional and defaults to 0.


• The `step` parameter is optional and defaults to 1.
• The `stop` value is not included in the generated sequence.
• All three parameters must be integers.

Use in `for` Loops:

The `range()` function is often used to control the number of iterations


in a `for` loop. It provides a concise way to generate a sequence of
numbers and iterate over them.

4
Output:

Efficient Memory Usage:

`range()` is memory-efficient because it doesn't create a list of all


values in advance. Instead, it generates values on-the-fly, making it
suitable for large ranges.

Using `range()` with `len()`:

You can combine `range()` with `len()` to iterate over the indices of a
sequence (list, string, etc.).

5
This allows you to access both the index and the corresponding
element in the sequence.

Understanding the flexibility and efficiency of the `range()` function is


crucial for writing efficient and readable `for` loops in Python.

6
`while` Loops:

A `while` loop repeatedly executes a block of code as long as a


specified condition is true. The basic syntax is:

Example:

Loop Control Statements:

• `break` statement: Terminates the loop and transfers control to


the statement immediately following the loop.

• `continue` statement: Skips the rest of the code inside the loop
for the current iteration and continues with the next iteration.

7
• `pass` statement: Acts as a null operation, meaning nothing
happens when it executes. It is often used as a placeholder where
syntactically some code is required but you don't want to
execute any statements.

Loops are fundamental for iterating over data, processing elements,


and performing repetitive tasks in Python programs. Understanding
how to use loops efficiently is crucial for writing concise and effective
code.

8
Sample Program

1)Write a python program to print pattern

A)

2) Write a python program to print pattern with limit

(if the limit is 5)


A)

You might also like