0% found this document useful (0 votes)
7 views3 pages

Loops in Python

The document discusses the importance of incrementing the indexing variable 'i' in a while loop to avoid infinite loops. It also provides examples of using for loops to iterate over a string and using the range() function to print numbers within a specified range. Additionally, it mentions that the range() function can accept start, stop, and step arguments for more control over the sequence of numbers.

Uploaded by

raganchannel
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)
7 views3 pages

Loops in Python

The document discusses the importance of incrementing the indexing variable 'i' in a while loop to avoid infinite loops. It also provides examples of using for loops to iterate over a string and using the range() function to print numbers within a specified range. Additionally, it mentions that the range() function can accept start, stop, and step arguments for more control over the sequence of numbers.

Uploaded by

raganchannel
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

Note: remember to increment i, or else the loop will continue forever.

The while loop requires relevant variables to be ready, in this example we need to define an indexing
variable, i, which we set to 1.

Output:
For Loop:

Iterating over a string (character by character):

for char in "Hello":

print(char)

Iterating using range() (for a specific number of times):

# Prints numbers from 0 up to (but not including) 5

for i in range(5):

print(i)

The range() function can take start, stop, and step arguments to control the sequence of numbers generated.

You might also like