Iterating Through a Python List Using For Loops

 
I
t
e
r
a
t
e
 
t
h
r
o
u
g
h
 
a
P
y
t
h
o
n
 
L
i
s
t
 
u
s
i
n
g
 
a
f
o
r
 
l
o
o
p
 
Iterate through a Python List
 
Starting at the first element, go through the list until
the last element.
 
animals = [“cat”, “rat”, “bat”, “bird”]
 
M
E
T
H
O
D
 
1
:
 
I
t
e
r
a
t
e
 
o
v
e
r
 
a
 
L
i
s
t
 
u
s
i
n
g
 
f
o
r
 
l
o
o
p
 
animals = [‘cat’, ‘rat’, ‘bat’, ‘bird’]
 
for 
pet
 in animals:
 
print(pet)
 
M
E
T
H
O
D
 
1
:
 
I
t
e
r
a
t
e
 
o
v
e
r
 
a
 
L
i
s
t
 
u
s
i
n
g
 
f
o
r
 
l
o
o
p
 
 
supplies = [‘pens’, ‘staplers’, ‘binders’, ‘pencils’]
 
for 
item
 in supplies:
 
print(item)
 
M
E
T
H
O
D
 
1
:
 
I
t
e
r
a
t
e
 
o
v
e
r
 
a
 
L
i
s
t
 
u
s
i
n
g
 
f
o
r
 
l
o
o
p
 
 
scores = [80, 90, 95, 75]
 
for 
num
 in scores:
 
print(num)
 
M
E
T
H
O
D
 
2
:
 
I
t
e
r
a
t
e
 
o
v
e
r
 
a
 
L
i
s
t
 
u
s
i
n
g
 
a
 
f
o
r
 
l
o
o
p
u
s
i
n
g
 
R
a
n
g
e
 
 
animals = [‘cat’, ‘rat’, ‘bat’, ‘bird’]
 
for 
pet
 in 
range(4):
 
print(animals[pet])
 
 
M
E
T
H
O
D
 
3
:
 
I
t
e
r
a
t
e
 
o
v
e
r
 
a
 
L
i
s
t
 
u
s
i
n
g
 
a
 
f
o
r
 
l
o
o
p
u
s
i
n
g
 
l
e
n
 
F
u
n
c
t
i
o
n
 
animals = [‘cat’, ‘rat’, ‘bat’, ‘bird’]
 
for 
pet
 in range(
len(animals)
):
 
print(animals[pet])
Slide Note
Embed
Share

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.

  • Python Basics
  • For Loop
  • List Iteration
  • Programming
  • Python Tutorial

Uploaded on Jul 23, 2024 | 2 Views


Download Presentation

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.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

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.

E N D

Presentation Transcript


  1. Iterate through a Python List using a for loop for loop

  2. Iterate through a Python List Starting at the first element, go through the list until the last element. animals = [ cat , rat , bat , bird ]

  3. 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

  4. 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)

  5. 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)

  6. 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])

  7. 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])

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#