zoukankan      html  css  js  c++  java
  • Traversal with a for loop

    A lot of computations involve processing a string one character at a time. Often they start at the beginning, select each character in turn, do something to it, and continue until the end. The pattern of processing is called traversal. One way to write a traversal is with a while statement:

    index = 0
    while index < len(fruit):
        letter = fruit[index]
        print letter
        index = index + 1

    This loop traverses the string and displays each letter one a line by itself.
    Another way to write a traversal is with a for loop:

    for char in fruit:
        print char

    Each time through the loop, the next character in the string is assigned to the variable char. The loop continues until no characters are left.
    The following example shows how to use concatenation (string addition) and a for loop generate an abecedarian series (that is, in alphabetical order).

    from Thinking in Python

  • 相关阅读:
    初试 Elastic Search
    索引分类
    Nginx
    LINQ入门
    CSS学习
    Keras 安装
    火车进站
    2016年网易笔试编程题2
    Java GC
    linux 安装 mysql
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/3799591.html
Copyright © 2011-2022 走看看