# For Loops

## For Loops

### Iterate through code like `lists`, `tuple`, `dictionary`:

```python
# Itterate though a list
farm_animals = ['goat', 'horse', 'chicken', 'cow', 'dog']

# The 'animal' variable is defined in for loop:
for animal in farm_animals:
	
	print(animal) # Will print each element in the list
	
	sentence = animal + " is safe in our farm"
	print(sentence) # Will print the sentence which each animal in list
	
	print('hello world') # This will run as many times as items in the list
```

{% hint style="info" %}
`Note:` This runs exactly the same way with `tuple`
{% endhint %}

### Iterating through `string`:

```python
greeting = "hello my name is tim"

for char in greeting:
	print(char) # prints each character separately
	
# End this look once we come accros the letter "N":
for char in greeting:
	if char == "n":
		break # If you want to move to next part of code but "break" out of the loop
	print(char) 
```

**The "break" argument is used to get out of a loop early and move on to the next part of the code that you are running**

**The "exit" argument is used to stop the code completely**

**The "continue" argument prevents the next lines of code in the loop to run but restarts the loop:**

```python
for char in greeting:
	if char == "n":
		continue
	print(char) # Will not print the n character but will print the rest after encountering "n"
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.arkannis.net/programming/courses/python-pcap-31-03-course/control-flow/for-loops.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
