0% found this document useful (0 votes)
6 views

For Loop

The document discusses for loops in Python, including using them to iterate over sequences like lists and strings, and examples of printing numbers in a range, list elements, string characters, and generating random letters.

Uploaded by

Daniela Calpe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

For Loop

The document discusses for loops in Python, including using them to iterate over sequences like lists and strings, and examples of printing numbers in a range, list elements, string characters, and generating random letters.

Uploaded by

Daniela Calpe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Loop Statement (For Loop)

A for loop in Python is a way of repeating a block of code for a certain


number of times or for each item in a sequence. For example, you can use a for loop
to print the numbers from 1 to 6, or to print the elements of a list or a string. A for
loop has the following syntax:

Here, variable is a name that you can choose, and sequence is an object that can be
iterated over, such as a list, a tuple, a range, or a string. (The # symbol indicates a
comment, which is ignored by Python.)

The for loop works by assigning the first item of the sequence to the variable,
then executing the block of code inside the loop. Then, it assigns the next item of the
sequence to the variable, and repeats the process until there are no more items left
in the sequence.

This for loop prints the numbers from 0 to 5:

The output is:

Here, i is the variable name that we chose, and range(6) is a built-in function
that returns a sequence of numbers from 0 to 5 (remember that index always starts
at 0). The print(i) statement inside the loop prints the value of i on each iteration.

Additional:
1. I know, most of you have a boyfriend or girlfriend, so how about your gf/bf wants
you to message "I miss you!" 12 times? So here it is.
The output is:

That can be made to 100.

2.
So

what if your gf/bf is in doubt, asking if it's really 12/100? You can put numbers before
the message.

The output is:

Remember, that index always starts at zero (0).

3. You can add a separator to make it more organize.


The output is:

4. If you really want it to start with 1, you can add “+ 1” after the “number”.

The output is:

You can also use a for loop to iterate over a list of strings:
Example:

The output is:


Here, fruit is the variable name that we chose, and fruits is a list of strings.
The print(fruit) statement inside the loop prints the value of fruit on each iteration.

For loop that iterates over a list of integers.


Example:

The output is:

So here, what it should print is 1, 2, 3, 4, 5, but since there is "number * 2" written on
it, it times 1, 2, 3, 4, 5 by 2, so 2, 4, 6, 8, 10 is the result.

You can also use a for loop to iterate over the characters of a string:
Example:
The output is:

So here, it will print the value (Python) but each character in each line.

Example 2:
You can print random letters.

The output is:

3. You can create a word with your value (which is the random letter) as long as the
words you chose are only different in the first letter. For example - cat, bat, rat, etc.

The output is:

You might also like