Learn Python's for loop statement in 3 minutes - life is like summer flowers, Python prolongs life

Python's for loop statement is similar to C++'s range for statement, similar to C#'s foreach loop traversal;
if you have not dabbled in the above two programming languages, please look forward from the back!
C++ range for statement:

string s = {
    
     "HelloWorld!" };
for (auto str : s) {
    
    
    cout <<"+" << str << endl;
  }

Output result:
insert image description here

C#'s foreach loop traverses:

string s = "HelloWorld!";
foreach(var str in s)
{
    
    
    Console.WriteLine($"#{str}");
}   

Output result:
insert image description here

Python's for loop statement:

s="HelloWorld!"
for str in s:
    print('p',str)

Output:
insert image description here
No amount of textual explanation is as good as a three-paragraph example.
Three pieces of code once again prove one thing: "Life is like a summer flower, Python prolongs life!"

Guess you like

Origin blog.csdn.net/baidu_38495508/article/details/122292629