Iterating Through a Python List Using For Loops
This tutorial provides detailed methods on how to iterate through a Python list using for loops. It covers techniques such as looping through elements directly, using range, and leveraging the len() function. Each method is explained with examples and visuals to help you understand the process effectively.
Download Presentation
![](/assets/img/so-down.gif)
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
Iterate through a Python List using a for loop for loop
Iterate through a Python List Starting at the first element, go through the list until the last element. animals = [ cat , rat , bat , bird ]
METHOD 1: METHOD 1: Iterate over a List using for loop for loop animals = [ cat , rat , bat , bird ] for pet in animals: print(pet) pet 0 1 2 3 animals[0] animals[1] animals[2] animals[3] animals cat rat bat bird
METHOD 1: METHOD 1: Iterate over a List using for loop for loop item supplies 0 1 2 3 supplies[0] supplies[1] supplies[2] supplies[3] pens staplers binders pencils supplies = [ pens , staplers , binders , pencils ] for item in supplies: print(item)
METHOD 1: METHOD 1: Iterate over a List using for loop for loop num scores 0 1 2 3 scores[0] scores[1] scores[2] scores[3] 80 90 95 75 scores = [80, 90, 95, 75] for num in scores: print(num)
METHOD 2: METHOD 2: Iterate over a List using a for loop using Range Range pet animals cat for loop 0 1 2 3 animals[0] animals[1] animals[2] animals[3] rat bat bird animals = [ cat , rat , bat , bird ] for pet in range(4): print(animals[pet])
METHOD 3: METHOD 3: Iterate over a List using a for loop using len len Function for loop pet 0 1 2 3 animals[0] animals[1] animals[2] animals[3] animals cat rat bat bird animals = [ cat , rat , bat , bird ] for pet in range(len(animals)): print(animals[pet])