diff --git "a/docs/for-loop/\351\251\254\347\216\211\345\251\2672023221402017" "b/docs/for-loop/\351\251\254\347\216\211\345\251\2672023221402017" new file mode 100644 index 0000000000000000000000000000000000000000..2e276356e9f6e2b3b30a34b5c53c8029328acece --- /dev/null +++ "b/docs/for-loop/\351\251\254\347\216\211\345\251\2672023221402017" @@ -0,0 +1,184 @@ + + +### Beginner's Tutorial on For Loops in Python + +A **for loop** is a control flow statement that allows you to execute a block of code multiple times. In Python, a for loop is typically used to iterate over items of a sequence (like a list, tuple, string, or dictionary) or other iterable objects. + +#### Basic Structure + +Here's the basic structure of a for loop in Python: + +```python +for item in iterable: + # Code block to be executed for each item + print(item) +``` + +- `item` is a variable that represents the current element in the iterable. +- `iterable` is the sequence or collection of items you want to iterate over. +- The block of code indented under the for loop will be executed once for each item in the iterable. + +#### Examples + +1. **Iterating over a List** + +```python +fruits = ['apple', 'banana', 'cherry'] + +for fruit in fruits: + print(fruit) +``` + +Output: +``` +apple +banana +cherry +``` + +2. **Iterating over a Range** + +You can use the `range()` function to generate a sequence of numbers. + +```python +for i in range(5): + print(i) +``` + +Output: +``` +0 +1 +2 +3 +4 +``` + +3. **Iterating over a String** + +```python +word = "hello" + +for letter in word: + print(letter) +``` + +Output: +``` +h +e +l +l +o +``` + +4. **Iterating over a Dictionary** + +When iterating over a dictionary, you can either get the keys, the values, or both (key-value pairs). + +```python +person = {'name': 'Alice', 'age': 30, 'city': 'New York'} + +# Iterating over keys +for key in person: + print(key) + +# Iterating over values +for value in person.values(): + print(value) + +# Iterating over key-value pairs +for key, value in person.items(): + print(f"{key}: {value}") +``` + +Output: +``` +name +age +city +Alice +30 +New York +name: Alice +age: 30 +city: New York +``` + +5. **Nested For Loops** + +You can nest one for loop inside another for loop. This is useful when you need to iterate over two or more iterables. + +```python +matrix = [ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9] +] + +for row in matrix: + for element in row: + print(element, end=' ') + print() # Move to the next line after each row +``` + +Output: +``` +1 2 3 +4 5 6 +7 8 9 +``` + +#### Break and Continue Statements + +- **break**: Immediately terminates the loop, and the execution continues with the next statement after the loop. +- **continue**: Skips the current iteration of the loop and proceeds with the next iteration. + +```python +# Using break +for i in range(10): + if i == 5: + break + print(i) + +# Output: 0 1 2 3 4 + +# Using continue +for i in range(10): + if i % 2 == 0: + continue + print(i) + +# Output: 1 3 5 7 9 +``` + +#### Enumerate Function + +The `enumerate()` function can be used to get both the index and the value of an iterable during iteration. + +```python +fruits = ['apple', 'banana', 'cherry'] + +for index, fruit in enumerate(fruits): + print(f"Index: {index}, Fruit: {fruit}") +``` + +Output: +``` +Index: 0, Fruit: apple +Index: 1, Fruit: banana +Index: 2, Fruit: cherry +``` + +#### Summary + +- For loops are used to iterate over sequences and other iterable objects. +- The basic structure is `for item in iterable:`. +- You can use `range()` to generate a sequence of numbers. +- You can iterate over dictionaries using `.keys()`, `.values()`, and `.items()`. +- Use `break` to terminate the loop early and `continue` to skip the current iteration. +- The `enumerate()` function provides both index and value during iteration. + +--- + +This tutorial should help you get started with for loops in Python. Happy coding! \ No newline at end of file