Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The use of loops in python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "the use of loops in python". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the use of loops in python"!

Cycle

Friends = ['iris','alice','bob','vince','alex']

Let's first introduce the loop, specifically, for...in.... Cycle

Now we need to use a loop to print out all the elements of the list (print it vertically) the specific code is as follows

Friends = ['iris','alice','bob','vince','alex']

For i in friends:

Print (I)

It is worth noting that the red mark here can use the name you want, friend,good_friend, I. It's all right. I specifically refers to item. It corresponds to the elements in the list.

In this way, the names in the list are printed in turn.

Okay, now let's take a closer look at the meaning of each line of code, for for...in.... Cycle to an in-depth study.

For i in friends: the purpose of this line of code is to get the elements in the list in turn and store them in I in turn.

Next, the print function prints out the names in order.

Now, let's do more.

We want to print out in a loop: person name + is my good friend.

Again, we still use for...in.... Loop, but we have made some improvements to the print function.

Of course we can do it at for...in.... Add more than one statement that needs to be printed below. Of course, at for...in... The statement indented under the statement follows the loop. If it is a statement without indentation, it will be printed at the end of the loop.

Iris is my good friend

I can't wait to meet my friend, Iris.

Alice is my good friend

I can't wait to meet my friend, Alice.

Bob is my good friend

I can't wait to meet my friend, Bob.

Vince is my good friend

I can't wait to meet my friend, Vince.

Alex is my good friend

I can't wait to meet my friend, Alex.

Bye

This is the output of the above code to see if it is the same as what I said above.

Here are a few points that need to be reminded

1) Don't forget for...in... Followed by a colon

2) Don't forget to indent

3) do not indent unnecessarily

At this point, I believe you have a deeper understanding of "the use of loops in python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report