How do I use a C-style for loop in Python?

后端 未结 7 1627
南旧
南旧 2020-12-01 06:17

I want to use the traditional C-style for loop in Python. I want to loop through characters of a string, but also know what it is, and be able to jump through characters (e.

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 06:38

    In C:

    for(int i=0; i<9; i+=2)
    {
        dosomething(i);
    }
    

    In python3:

    for i in range(0, 9, 2):
        dosomething(i)
    

    You just express the same idea in different languages.

提交回复
热议问题