Learn Python
- Python basic
- Introduction to File Handling
- Basics of List Comprehension
- Introduction to Matplotlib
- Classes and Objects
- Introduction to Functions
- Python Numbers
- Creating Basic Plots
- Opening and closing files
- Function parameters and arguments
- Advanced Techniques
- Attributes and Methods
- Python Strings
- Scope and lifetime of variables
- Advanced Plotting
- Reading from files
- Performance and Limitations
- Encapsulation
- Python List
- Specialized Plots
- Writing to files
- Return statement and output
- Inheritance
- Python Tuple
- Advanced Customization
- Working with different file formats
- Lambda Functions
- Polymorphism
- Practical Applications
- Higher-Order Functions
Flow control
Looping through a List
Looping through a list means iterating over each element in the list one by one. This is commonly done using a for loop, which allows you to access and manipulate each item in the list during each iteration.
1. Using a for loop to iterate over a List:
You can loop through a list directly using a for loop:
1. Using a for loop to iterate over a List:
You can loop through a list directly using a for loop:
fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)
- The list fruits contains three items: apple, banana, and cherry.
- The for loop iterates over the list, and for each iteration, the variable fruit takes the value of the current item in the list.
- The print(fruit) statement outputs the current item during each iteration.
Output:
apple banana cherry
2. Looping through a list using range() and index:
You can also loop through the list by using range() and the length of the list to access items by their index.
Example:
fruits = ["apple", "banana", "cherry"] for i in range(len(fruits)): print(fruits[I])
- range(len(fruits)) generates a sequence of numbers from 0 to len(fruits) - 1 (i.e., the indices of the list).
- The loop variable i takes these index values, and fruits[i] accesses the corresponding item in the list.
Output:
apple banana cherry
3. Using a while loop to iterate over a List:
You can use a while loop to iterate through a list by keeping track of the index manually.
Example:
fruits = ["apple", "banana", "cherry"] i = 0 while i < len(fruits): print(fruits[i]) i += 1
- The loop continues as long as i is less than the length of the list.
- Each iteration prints the element at index i and increments i by 1 to move to the next item.
Output:
apple banana cherry
4. break and continue in a List loop:
You can use the break and continue statements to control the flow of the loop:
- break: Exits the loop early.
- continue: Skips the current iteration and moves to the next one.
Example using break:
fruits = ["apple", "banana", "cherry", "orange"] for fruit in fruits: if fruit == "cherry": break # Stop the loop when fruit is "cherry" print(fruit)
Output:
apple banana
Example using continue:
fruits = ["apple", "banana", "cherry", "orange"] for fruit in fruits: if fruit == "banana": continue # Skip the current iteration when fruit is "banana" print(fruit)
Output:
apple cherry orange
5. Looping through a list with else:
An optional else clause can be added to a loop. The else block will execute once the loop finishes, unless it is stopped by a break statement.
Example:
fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) else: print("Finished looping through the list.")
Output:
apple banana cherry Finished looping through the list.
However, if the loop is terminated with a break, the else block will not be executed.
Example with break:
fruits = ["apple", "banana", "cherry"] for fruit in fruits: if fruit == "banana": break print(fruit) else: print("Finished looping through the list.")
Output:
apple
6. Use cases:
- You can use a loop to calculate the sum of all items in a list of numbers:
numbers = [1, 2, 3, 4, 5] total = 0 for num in numbers: total += num print("Sum:", total)
Output:
Sum: 15
- You can also modify items in a list while looping through it by using the index:
numbers = [1, 2, 3, 4, 5] for i in range(len(numbers)): numbers[i] = numbers[i] * 2 print(numbers)
Output:
[2, 4, 6, 8, 10]
7. When to use looping through Lists:
- Accessing each item in a list.
- Searching for a specific value in a list.
- Modifying or updating items in a list.
- Aggregating data (e.g., finding the sum, product, or average).
- Filtering items (e.g., removing items that meet a certain condition).
It's time to take a quiz!
Test your knowledge and see what you've just learned.
What will be the output of the following code? fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)
Aapple
banana
cherry
Bapple, banana, cherry
CApple
Banana
Cherry
DApple Banana Cherry
Check Answer
What will be the output of the following code? fruits = ["apple", "banana", "cherry"] for i in range(len(fruits)): print(fruits[i])
DApple Banana Cherry
Aapple
banana
cherry
Bapple, banana, cherry
CApple
Banana
Cherry
Check Answer
What will be the output of the following code? fruits = ["apple", "banana", "cherry"] i = 0 while i < len(fruits): print(fruits[i]) i += 1
Aapple
banana
cherry
Bapple, banana, cherry
CApple
Banana
Cherry
DApple Banana Cherry
Check Answer
What will be the output of the following code? fruits = ["apple", "banana", "cherry", "orange"] for fruit in fruits: if fruit == "cherry": break print(fruit)
Dbanana
cherry
Aapple
banana
Bapple, banana
Capple
banana
cherry
Check Answer
What will be the output of the following code? fruits = ["apple", "banana", "cherry", "orange"] for fruit in fruits: if fruit == "banana": continue print(fruit)
Aapple
cherry
orange
Bapple
banana
cherry
orange
Capple
cherry
Dbanana
orange
Check Answer
What will be the output of the following code? fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) else: print("Finished looping through the list.")
CFinished looping through the list.
Dapple
banana
cherry
Stopped early
Aapple
banana
cherry
Finished looping through the list.
Bapple
banana
cherry
Check Answer