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

Traversal with a for loop的更多相关文章

  1. Think Python - Chapter 8 - Strings

    8.1 A string is a sequenceA string is a sequence of characters. You can access the characters one at ...

  2. 《Think Python》第8章学习笔记

    目录 8.1 字符串是一个序列(A string is a sequence) 8.2 len 8.3 用一个 for 循环进行遍历(Traversal with a for loop) 8.4 字符 ...

  3. Atitit  循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate).

    Atitit  循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate). 1.1. 循环算是最基础的概念, 凡是重复执行一段代码, 都可以称之为循环. ...

  4. 循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate)的区别

    表示“重复”这个含义的词有很多, 比如循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate). 循环算是最基础的概念, 凡是重复执行一段代码, 都可以称 ...

  5. 003_循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate)的区别

    表示“重复”这个含义的词有很多, 比如循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate). 循环算是最基础的概念, 凡是重复执行一段代码, 都可以称 ...

  6. LintCode Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Given: 1 / \ 2 3 / \ 4 5 re ...

  7. 从event loop规范探究javaScript异步及浏览器更新渲染时机

    异步的思考 event loops隐藏得比较深,很多人对它很陌生.但提起异步,相信每个人都知道.异步背后的“靠山”就是event loops.这里的异步准确的说应该叫浏览器的event loops或者 ...

  8. 浏览器组成、线程及event loop

    浏览器组成 User interface: a. Every part of the browser display, except the window. b. The address bar, b ...

  9. Why should I avoid blocking the Event Loop and the Worker Pool?

    Don't Block the Event Loop (or the Worker Pool) | Node.js https://nodejs.org/en/docs/guides/dont-blo ...

随机推荐

  1. Chrome的JSON View插件

    Chrome的JSON View插件 学习了:http://www.cnplugins.com/zhuanti/five-chrome-json-plugins.html 下载了:http://www ...

  2. 【面试】-Java基础知识

    1.Java的工作原理 1) Java源程序(.java)须要通过编译器编译成字节码(.class)文件; 2) Java程序的跨平台主要指字节码能够在不论什么具有Java虚拟机的设备上运行: 3) ...

  3. Sping框架中的注解详解

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  4. 安卓 使用Gradle生成正式签名apk文件

    1. 进入app中的build.gradle下面进行配置 2.进入Gradle下面选择clean和assembleRelese,双击 3.生成成功,前往查看 4.加密更安全

  5. ios问题bug收录——1

     **[2713:59682] *** Assertion failure in -[MBProgressHUD show:], /Users/lu/Desktop/****/Pods/MBProgr ...

  6. AndroidStudio EventBus报错解决方法its super classes have no public methods with the @Subscribe

    首先说明,以前我用eventBus的jar包写得项目demo,前几天就写了一个EventBus的实例,这次我没用jar包,直接用gradle引用的,可是demo写完了,报错: its super cl ...

  7. JavaScript学习——使用JS实现首页轮播图效果

    1.相关技术 获取元素 document.getElementById(“id 名称”) 事件(onload) 只能写一次并且放到body标签中 定时操作:setInterval(“changeImg ...

  8. [ Linux ] user, password, sudoers

    - 1. Add Linux user,  setup password http://linux.vbird.org/linux_basic/0410accountmanager.php - 2. ...

  9. eclipse oxygen离线安装activiti

    我用的最新版本的eclipse oxygen,结果通过help-->install new software-->add 一直报找不到,后来网上查找的离线安装,终于成功了,在这里和大家分享 ...

  10. ZBrush中独特功能Projection Master

    Projection Master可以理解为投影大师,它是ZBrush®的一个独特功能,该功能可以让用户使用所有的2D和2.5D笔刷工具在3D模型上进行雕刻.纹理绘制和其他的操作. 简单来说,Proj ...