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
Traversal with a for loop的更多相关文章
- 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 ...
- 《Think Python》第8章学习笔记
目录 8.1 字符串是一个序列(A string is a sequence) 8.2 len 8.3 用一个 for 循环进行遍历(Traversal with a for loop) 8.4 字符 ...
- Atitit 循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate).
Atitit 循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate). 1.1. 循环算是最基础的概念, 凡是重复执行一段代码, 都可以称之为循环. ...
- 循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate)的区别
表示“重复”这个含义的词有很多, 比如循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate). 循环算是最基础的概念, 凡是重复执行一段代码, 都可以称 ...
- 003_循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate)的区别
表示“重复”这个含义的词有很多, 比如循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate). 循环算是最基础的概念, 凡是重复执行一段代码, 都可以称 ...
- LintCode Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Given: 1 / \ 2 3 / \ 4 5 re ...
- 从event loop规范探究javaScript异步及浏览器更新渲染时机
异步的思考 event loops隐藏得比较深,很多人对它很陌生.但提起异步,相信每个人都知道.异步背后的“靠山”就是event loops.这里的异步准确的说应该叫浏览器的event loops或者 ...
- 浏览器组成、线程及event loop
浏览器组成 User interface: a. Every part of the browser display, except the window. b. The address bar, b ...
- 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 ...
随机推荐
- 今天修了一个bug,关于debug日志的问题
是别人的代码,很诡异. 就是开了debug日志,没问题. 关了debug日志,就出问题. 开始我以为是debug日志拖慢了速度,所以有一些竞态环境的影响. 后来发现是在debug日志里面有一些side ...
- 15 个经常使用的 SQL Server 高级语法
1.case-end (详细的值) case后面有值,相当于c#中的switch case 注意:case后必须有条件,而且when后面必须是值不能为条件. -----------------case ...
- 扩展MARA 加入Z字段BAPI_TE_MARA
1. 在MARA中APPEND新的结构 2.在BAPI_TE_MARA中APPEND新的结构 (可是这里不能有QUAN,CURR,DEC等数据类型) 3.在BAPI_TE_MARAX中APPEND新的 ...
- m_Orchestrate learning system---六、善用组件插件的好处是什么
m_Orchestrate learning system---六.善用组件插件的好处是什么 一.总结 一句话总结: 1.面包屑导航是什么? 知道它是什么自然就知道它怎么用了 2.表格里面的栏目能能点 ...
- pgsql数据库备份还原记
今天又搞了一个pgsql 的备份还原,差一点没有成功,以前总是想当然的用,没认真想背后的东西,也没对过程中的疑问做记录,所以后面也没什么印象,常见常新,这次既然又遇到就总结一下. 之前操作pgsql数 ...
- spring中bean标签factory-method和factory-bean)详解工厂方法(factory-method和factory-bean)
转自:http://blog.sina.com.cn/s/blog_6d3c1ec601019f3j.html A.factory-method The name of a factory metho ...
- elementUI 易错点
1.element table里面添加单选时,如果存在下拉框的筛选功能,那么每次下拉框筛选条件变化时 都得清空之前选中的信息,如果不数据更新后如果更新后的数据跟之前选中的相同 则会无法选中
- vue+Element实现tree树形数据展示
组件: Element(地址:http://element.eleme.io/#/zh-CN/component/tree):Tree树形控件 <el-tree ref="expand ...
- atom安装插件失败 latex
用atom写latex 链接 http://www.cnblogs.com/schaepher/p/5934184.html 但在gui下安装插件失败 按照以下步骤解决了 安装gitbash cd . ...
- POJ 2386 Lake Counting【BFS】
题意:给出一个矩形,问有多少块连通的W 当找到W的时候,进行广搜,然后将搜过的W变成点,直到不能再搜,进行下一次广搜,最后搜的次数即为水塘的个数 看的PPT里面讲的是种子填充法. 种子填充算法: 从多 ...