0% found this document useful (0 votes)
5 views1 page

Loops

The document explains the while and for loops in Python, detailing their syntax, usage, and common pitfalls. It provides examples of how to implement a while loop for counting down and a for loop for iterating over a list. Additionally, it discusses the range() function, illustrating its use with various examples to generate sequences of numbers.

Uploaded by

trailforshits
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)
5 views1 page

Loops

The document explains the while and for loops in Python, detailing their syntax, usage, and common pitfalls. It provides examples of how to implement a while loop for counting down and a for loop for iterating over a list. Additionally, it discusses the range() function, illustrating its use with various examples to generate sequences of numbers.

Uploaded by

trailforshits
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

The while Loop

Exploring the while Loop


Let's dive into the world of the while loop and understand its
purpose and usage.

Syntax and Usage

Syntax and Usage

The while loop in Python allows you to repeatedly execute


a block of code as long as a certain condition is true.
Here's the basic syntax:

while condition:

# Code to be executed

The condition is evaluated before each iteration. If the


condition is true, the code block is executed. This process
continues until the condition becomes false.

Example: Counting Down

Example: Counting Down


Let's say we want to count down from 5 to 1 using a

while loop:

count = 5

while count > 0:

print(count)

count -= 1

This code will output:

As long as the condition count > 0 is true, the loop will continue
to execute, decrementing the value of count by 1 and printing its
value.

Common Pitfalls

Common Pitfalls

Be careful not to create an infinite loop by forgetting to update


the condition inside the loop. Make sure the condition eventually
becomes false
Ensure that the condition is properly defined and will eventually
evaluate to false. Otherwise, the loop may never execute or
terminate prematurely
Avoid relying solely on the while loop for iteration. In many cases,
the for loop is more appropriate and easier to understand.
The for Loop

The for Loop

Learn about the syntax and applications of the for loop in


Python.

The for loop is a powerful construct in Python that allows you to iterate over a sequence of elements.
It is commonly used to perform repetitive tasks or iterate through collections like lists, tuples, and
strings.
The syntax of the for loop in Python is as follows:

for item in sequence:

# Code block to be executed for each item

Let's break down the components of the for loop:


item: This is a variable that represents each item in the sequence. You can choose any valid
variable name
sequence: This is the collection of elements that you want to iterate over. It can be a list, tuple,
string, or any other iterable object
Code block: This is the set of instructions that will be executed for each item in the sequence. It
is indented under the for loop statement.

Here's an example that demonstrates how to use the for loop to iterate over a list:

fruits = ['apple', 'banana', 'orange']

for fruit in fruits:

print(fruit)

This code will output:

apple

banana

orange

The for loop is a versatile tool in Python that can be used in various scenarios. It allows you to
automate repetitive tasks, process collections of data, and perform complex iterations. With
practice, you'll become proficient in utilizing the for loop to its full potential.

The range() Function with for Loop

Exploring the range() function


The range() function in Python is a powerful tool that can be
used in combination with the for loop to iterate over a
sequence of numbers. It allows you to generate a sequence
of numbers based on a specified start, stop, and step value.

H ow does the range() function work?

The range() function takes up to three arguments:


start: The starting value of the sequence. If not specified,
it defaults to 0
stop: The ending value of the sequence. The range()
function generates numbers up to, but not including, this
value
step: The increment between each number in the
sequence. If not specified, it defaults to 1.

Example usage:

Let's take a look at some examples to understand how the


range() function works:

Example 1:
U sing the range() function to generate a sequence of numbers:

for num in range(5):

print(num)

This code will output:

Example 2:
U sing the range() function with a specified start and stop
value:

for num in range(2, 6):

print(num)

This code will output:

Example 3:
U sing the range() function with a specified start, stop, and
step value:

for num in range(1, 10, 2):

print(num)

This code will output:

You might also like