Python introductory basics, about for loop learning, just read this one (dry goods)

Preface


The for loop is a type of loop used many in python development and requires proficiency.

Use scenarios of for loop

  • The for loop is used to repeat a specific number of operations
  • The for loop is mainly used to traverse, loop, lists, collections, dictionaries, files, and even custom classes or functions.

Example demonstration of for loop operation list

  • Use for loops are often combined with if statements to traverse elements, modify elements, delete elements, and count the number of elements in the list.

The for loop is used to traverse the entire list

#for循环主要用来遍历、循环、序列、集合、字典
Fruits = ['apple','orange','banana','grape']

for fruit in Fruits:
    print(fruit)
print("结束遍历")
结果演示:
    apple
    orange
    banana
    grape
    结速遍历

The for loop is used to modify the elements in the list

#for循环主要用来遍历、循环、序列、集合、字典
#把banana改为Apple
Fruits=['apple','orange','banana','grape']
for i in range(len(Fruits)):
    if Fruits[i] == 'banana':
        Fruits[i] ='apple'
print(Fruits)
结果演示:['apple', 'orange', 'apple', 'grape']

3. The for loop is used to delete elements in the list

Fruits=['apple','orange','banana','grape']
for i in  Fruits:
    if i == 'banana':
        Fruits.remove(i)
print(Fruits)
结果演示:['apple', 'orange', 'grape']

The for loop counts the number of an element in the list

#统计apple的个数
Fruits = ['apple','orange','banana','grape','apple']
count = 0
for i in  Fruits:
    if i=='apple':
        count+=1
print("Fruits列表中apple的个数="+str(count)+"个")
结果演示:Fruits列表中apple的个数=2

Note: You can also use Fruit.count(object) for certain data statistics in the list

The for loop realizes the multiplication of 1 to 9

sum=1
for i in list(range(1,10)):
    sum *= i
print("1*2...*9=" + str(sum))
结果演示:1*2...*10=362880

Iterate over the string

for str in 'abc':
    print(str)

结果演示:
a
b
c

Iterate over collection objects

for str in {
    
    'a',2,'bc'}:
    print(str)

结果演示:
a
2
bc

Iterate over files

for content in open("D:\\test.txt"):
    print(content)

结果演示:
朝辞白帝彩云间,千里江陵一日还。
两岸猿声啼不住,轻舟已过万重山。

Iterate over the dictionary

for key,value in {
    
    "name":'伤心的辣条',"age":22}.items():
    print("键---"+key)
    print("值---"+str(value))

结果演示:
键---name
值---伤心的辣条
键---age
值---22

If the article is helpful to you, please reach out to make a fortune and give me a like. Thank you for your support. Your likes are my motivation for continuous updating.


Finally: benefits

In the technology industry, you must improve your technical skills and enrich your practical experience in automation projects. This will be very helpful for your career planning in the next few years and the depth of your testing technology.

In the interview season of Golden 9th and Silver 10th, job-hopping season, organizing interview questions has become my habit for many years! The following is my collection and sorting in recent years, the whole is organized around [software testing], the main content includes: python automation test exclusive video, Python automation details, a full set of interview questions and other knowledge content.

For software testing friends, it should be the most comprehensive and complete interview preparation warehouse. In order to better organize each module, I also refer to many high-quality blog posts and projects on the Internet, and strive not to miss every knowledge point. Friends relied on these contents to review, and got offers from big factories such as BATJ. This warehouse has also helped many software test learners, and I hope it can help you too!

May you and I meet and you will find something! Welcome to follow the WeChat public account: [Sad Spicy Article] Receive a 216-page software test engineer interview book for free. And the corresponding video learning tutorials are free to share!

Guess you like

Origin blog.csdn.net/weixin_50271247/article/details/112897923