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

How to use for Loop in python Foundation

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "how to use the for cycle on the basis of python", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the for cycle in the basis of python" article.

I. for cycle

Loop according to the number of times a variable is assigned

For item in ["tom", "bob", "jerry", "li"]: # in can be followed by list, number of loops and number of list elements are the same print ("hello" + item) for item in "python": # in can be followed by string, number of loops and string length print ("hello" + item) for item in ("tom", "bob", "jerry", "li"): # in can be followed by tuple Print ("hello" + item) for item in {"tom": "123"," jerry ":" 456 "}: # in can be the same as dictionary, the number of cycles and the number of keys are the same print (" hello "+ item)

Second, expand the value of the output dictionary using for loop valueadict= {"tom": "123"," jerry ":" 456 "} for item in adict: print

Third, the advantages of for cycle

Do not follow the integer after in

We can introduce rang (10) to generate a list of length 10

For item in range (10): print (item)

Result output 0-9

Optimization: range (1BI 11)

For item in range (1meme 11): print (item)

4. For implementation accumulates sum=0for i in range (1 sum 11): sum+=i if (iTunes 3): # here you can judge that the amount you add is the amount you add to it (sum)

5. Calculate even numbers within 10 and sum=0for i in range (1jue 11): if I% 2 cycles 0: continue sum+=iprint (sum) 6, calculate odd numbers within 10 and sum=0for i in range (1pm 11): if I% 2 numbers numbers 0: # if not (I% 2) continue sum+=iprint (sum) these are the contents of this article entitled "how to use for loops in python Foundation". I believe you all have some understanding. I hope the content shared by the editor will be helpful to all of you. If you want to know more about the relevant knowledge, please follow the industry information channel.

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

Development

Wechat

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

12
Report