美文网首页
Python中的for循环

Python中的for循环

作者: 晃里晃荡 | 来源:发表于2018-04-11 23:06 被阅读6次

for循环的格式

for 临时变量 in 列表或字符串等:
    满足条件时执行的代码
else:
    不满足条件时执行的代码

举例说明

import time

play = "ssamba"

for temp in play:
    print("%S"%temp)
    time.sleep(1)

注意:for语法中in后面往往跟的是一个可以容纳多个数据的变量对象,比如列表元组字典字符串。

错误例子

import time

for temp in 100:
    print("%S"%temp)
    time.sleep(1)

最后结果会有这么一个报错信息:TypeError: 'int' object is not iterable

相关文章

  • 我的python学习笔记-第十天

    循环语句 Python中的循环语句有 for 和 while。 while 循环 Python中while语句的一...

  • 012.Python循环语句

    Python 循环语句 1. 概述 Python中的循环语句有 for 和 while。 Python循环语句的控...

  • Lesson 021 —— python 循环语句

    Lesson 021 —— python 循环语句 Python中的循环语句有 for 和 while。 循环可以...

  • python 循环语句

    本次将为大家介绍Python循环语句的使用。Python中的循环语句有 for 和 while。Python循环语...

  • python 基础 - 循环语句

    python 循环语句 Python中的循环语句有 for 和 while。Python循环语句的控制结构图如下所...

  • Python3 & 循环语句

    Python 提供了 for 循环和 while 循环(在 Python 中没有 do..while 循环)。 W...

  • 第五周python学习

    Python提供了for循环和while循环(在Python中没有do..while循环): 循环类型描述 whi...

  • 彻底理解python中的yield函数

    大家都知道python中的循环结构,那么我们分析下python中while和for循环的本质是如何实现的。 循环的...

  • 14、python循环语句

    本章节将为大家介绍Python循环语句的使用。Python中的循环语句有 for 和 while。 while循环...

  • Python3 循环

    Python中的循环语句有 for 和 while。 while循环 Python中while语句的一般形式: 同...

网友评论

      本文标题:Python中的for循环

      本文链接:https://www.haomeiwen.com/subject/ympkhftx.html