【py code everyday 】2023-03-02

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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值