What is FOR destination?

FOR destination is a command in the FOR loop syntax. It specifies where the loop variable takes its values from.

In the FOR loop, a loop variable is used to iterate over a set of elements or values. The FOR destination specifies the source of these values.

The syntax of FOR loop with FOR destination is:

```

for identifier in destination

do

# commands

done

```

Here,

* destination is the FOR destination, which can be a list, tuple, string, set, dictionary, file object, range object, etc.

* identifier is the loop variable that takes values from the destination.

* The do keyword introduces the loop body.

* The commands inside the loop body are executed repeatedly for each value in the destination.

* The done keyword marks the end of the loop.

Here are some examples of FOR loop with different FOR destinations:

```python

Iterate over the elements in a list

numbers = [1, 2, 3, 4]

for number in numbers:

print(number)

Iterate over a range of numbers

for number in range(5):

print(number)

Iterate over the characters in a string

word = "Hello"

for letter in word:

print(letter)

Iterate over the lines in a file

with open("myfile.txt", "r") as file:

for line in file:

print(line)

```

In these examples, the FOR destination can be a list, range object, string, or file object. The loop variable takes its values from the specified destination, and the commands inside the loop body are executed repeatedly for each value.

Copyright Wanderlust World © https://www.ynyoo.com