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
最新文章
- wampServer图标为橙色无法启动原因之一
- 【转】js判断手机访问网页
- NYOJ题目34韩信点兵
- 学习mongo系列(十一)关系
- 为什么高手离不了Linux系统?这就是我的理由
- UIKit&#160;框架之UISearchController
- bom type:Phantom
- UVAlive11324 The Largest Clique(scc+dp)
- C#操作Excel总结
- 头皮发麻的HTML课时一
- MQTT——订阅报文
- Mysql服务器SQL模式 (官方精译)
- 关于SELECT LAST_INSERT_ID()的使用规则
- LOJ #6285 分块入门9
- 纯C:url base64
- C# WinForm 菜单项的大小、高宽的手动控制
- Swift 实践篇之链式 UI 代码
- C#之#if #endif的简单用法
- 解决eclipse快捷键Ctrl+Alt+Down冲突问题办法
- Tsung测试之配置文件