Python- Day 10- Working With List

Nidhi Ashtikar
3 min readAug 11, 2024

--

Looping Through an Entire List:

For Loop:

  • Automates repetitive tasks with list items.
  • Example: for magician in magicians: print(magician).
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)

>>

alice
david
carolina

Iterate All Items:

  • Loop retrieves and processes each item.
  • Example: Print each magician’s name in a list.
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")

>>

Alice, that was a great trick!
David, that was a great trick!
Carolina, that was a great trick!

Loop Execution:

  • Repeat the steps for each list item.
  • Example: Personalized messages using loops.
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")

>>

Alice, that was a great trick!
I can't wait to see your next trick, Alice.

David, that was a great trick!
I can't wait to see your next trick, David.

Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.

Post-Loop Action:

  • Code after loop executes once.
  • Example: Thanking all magicians after individual messages.
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")
print("Thank you, everyone. That was a great magic show!")


>>

Alice, that was a great trick!
I can't wait to see your next trick, Alice.

David, that was a great trick!
I can't wait to see your next trick, David.

Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.

Thank you, everyone. That was a great magic show!

Avoiding Indentation Errors in Python

Forgetting to Indent:

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician) # Should be indented

#Error :
File "magicians.py", line 3
print(magician)
^
IndentationError: expected an indented block after 'for' statement on line 2
  • Error: IndentationError: expected an indented block after 'for' statement
  • Fix: Indent the print(magician) line.

Forgetting to Indent Additional Lines:

for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n") # Should be indented


#OutPut

Alice, that was a great trick!
David, that was a great trick!
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
  • Issue: Only the last magician gets the second message because the line is not inside the loop.
  • Fix: Indent the second print statement.

Indenting Unnecessarily:

message = "Hello Python world!"
print(message) # Unnecessary indent

#Error :

File "hello_world.py", line 2
print(message)
^
IndentationError: unexpected indent
  • Error: IndentationError: unexpected indent
  • Fix: Remove the unnecessary indent.

Indenting After the Loop:

for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")
print("Thank you everyone, that was a great magic show!") # Should not be indented


>>

Alice, that was a great trick!
I can't wait to see your next trick, Alice.
Thank you everyone, that was a great magic show!

David, that was a great trick!
I can't wait to see your next trick, David.
Thank you everyone, that was a great magic show!

Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
Thank you everyone, that was a great magic show
  • Issue: The thank you message is printed for each magician.
  • Fix: Unindent the last print statement.

Forgetting the Colon:

for magician in magicians  # Missing colon
print(magician)


#Error :

File "magicians.py", line 2
for magician in magicians
^
SyntaxError: expected ':'
  • Error: SyntaxError: expected ':'
  • Fix: Add the colon after for magician in magicians.

If you found this guide helpful then do click on 👏 the button.

Follow for more Learning like this 😊

If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!

Thanks for spending your valuable time learning to enhance your knowledge!

--

--

Nidhi Ashtikar
Nidhi Ashtikar

Written by Nidhi Ashtikar

Experienced AWS DevOps professional with a passion for writing insightful articles.

No responses yet