1. Loop Statement
Loops are control structures used to execute the same task multiple times. They are mainly implemented using for loops and while loops, each suitable for different situations.
- For Statement: A for loop is suitable when the number of iterations is known in advance.
- While Statement: Executes code repeatedly while a condition is true.
2. While Statement
A while loop is a control structure that repeatedly executes a code block as long as a specific condition is true. It continues to execute the code as long as the given condition remains true, and stops when the condition becomes false.
3. For Statement
A for loop is a control flow statement in Python used to iterate over a sequence (such as lists, tuples, strings, etc.) and perform repetitive tasks for each item in the sequence. For loops are primarily used when you have a predefined set of elements to iterate over.
- Element: Variable to which each item of the sequence is assigned during the execution of the for loop.
- Sequence: Collection of data to iterate over (e.g., lists, tuples, strings).
- Action: Represents the task to be performed for each element.
3-1. range() function
The range() function is a built- in function in Python used to generate a sequence of sequential integers. It is commonly used with for loops to efficiently perform repetitive tasks within a specified range. The sequence of sequential integers generated by the range() function is not stored in memory but generated as needed, making it efficient for use in large-scale iterations.
3-2. enumerate() function
The enumerate() function is a built- in function in Python used to retrieve both the index and value while iterating through a sequence using a loop. It is commonly used with for loops to keep track of the order while simultaneously accessing both the value and its corresponding index within the loop. The enumerate() function generates an iterator that returns tuples of (index, value).
3-3. zip() function
The zip() function is a built-in function in Python used to pair up multiple iterable objects in parallel. It returns an iterator that aggregates elements from each iterable object into tuples. It is commonly used when you want to process multiple lists or tuples in parallel.
4. Nested Loops
Nested loops in Python refer to a structure where one loop is contained within another loop. Nested loops operate by executing the inner loop in its entirety for each iteration of the outer loop. This enables the processing of multidimensional data or performing complex tasks.
'Python(Eng. ver)' 카테고리의 다른 글
12. User-Defined Function (0) | 2024.03.15 |
---|---|
11. Collections and Loops (0) | 2024.03.15 |
9. Conditional Statements in Python (0) | 2024.03.14 |
8. Operator in Python (0) | 2024.03.14 |
7. Python collection type - Dictionary (0) | 2024.03.14 |