Let’s use a for loop to print out each name in a list of magicians
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician)
alice
david
carolina
For every magician in the list of magicians, print the magician’s name.
When you’re using loops for the first time, keep in mind that the set of
steps is repeated once for each item in the list, no matter how many items
are in the list. If you have a million items in your list, Python repeats these
steps a million times—and usually very quickly
Using singular and plural names can help
you identify whether a section of code is working with a single element from
the list or the entire list.
magician is singular name and magicians is plural name
(Plural names are when we talk about more than one person or thing.
For example, if we talk about one dog, we say “dog,” but if we talk about two or more dogs, we say “dogs.” Another example is if we talk about one person named Tom, we say “Tom,” but if we talk about two or more people named Tom, we say “Toms.” So, plural names are just like adding an “s” at the end of a name to talk about more than one person or thing.)
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.
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")
The newline (“\n”) in the second
print() call inserts a blank line after each pass through the loop
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.
Any lines of code after the for loop that are not indented are executed
once without repetition. Let’s write a thank you to the group of magicians
as a whole, thanking them for putting on an excellent show
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!