for循环

像while循环一样,for可以完成循环的功能。

在Python中 for循环可以遍历任何序列的项目,如一个列表或者一个字符串等。

for循环的格式

    for 临时变量 in 列表或者字符串等可迭代对象:
    循环满足条件时执行的代码

demo1

name = 'itheima'

for x in name:
    print(x)

运行结果如下:

i
t
h
e
i
m
a

demo2

>>> for x in name:
        print(x)
        if x == 'l':
            print("Hello world!")

运行结果如下:

h
e
l
Hello world!
l
Hello world!
o

demo3

# range(5) 在python就业班中进行讲解会牵扯到迭代器的知识,
# 作为刚开始学习python的我们,此阶段仅仅知道range(5)表示可以循环5次即可
for i in range(5):
    print(i)

'''
效果等同于 while 循环的:

i = 0
while i < 5:
    print(i)
    i += 1
'''

运行结果如下:

0
1
2
3
4

2.12 for循环的更多相关文章

  1. JavaScript 中的12种循环遍历方法

    原文:JavaScript 中的12种循环遍历方法 题目:请介绍 JavaScript 中有哪些循环和遍历的方法,说说它们的应用场景和优缺点? 1.for 循环 let arr = [1,2,3];f ...

  2. JavaScript 中的常用12种循环遍历(数组或对象)的方法

    1.for 循环 let arr = [1,2,3]; for (let i=0; i<arr.length; i++){ console.log(i,arr[i]) } // 0 1 // 1 ...

  3. javascript中的12种循环遍历方法1

    1:for循环 let arr = [1,2,3]; for(let i =0;i<arr.length;i++){ console.log(i,arr[i]) } //for循环是js中最常用 ...

  4. javascript中的12种循环遍历方法

    1.for (自定义条件) 循环 let arr = [1,2,3]; for(let i =0;i<arr.length;i++){ console.log(i,arr[i]) } 2.for ...

  5. Scala中的If判断&While&For循环

    If 判断: object TestScalaIf { def main(args: Array[String]): Unit = { // val resutlt = judge1(-100) // ...

  6. for 循环 和 Array 数组对象

    博客地址:https://ainyi.com/12 for 循环 和 Array 数组对象方法 for for-in for-of forEach效率比较 - 四种循环,遍历长度为 1000000 的 ...

  7. JavaScript是如何工作的:事件循环和异步编程的崛起 + 5种使用 async/await 更好地编码方式!

    摘要: 深度理解JS事件循环!!! 原文:JavaScript是如何工作的:事件循环和异步编程的崛起+ 5种使用 async/await 更好地编码方式! 作者:前端小智 Fundebug经授权转载, ...

  8. android 自定义无限循环播放的viewPager。轮播ViewPager。实现循环播放 广告,主题内容,活动,新闻内容时。

    前言 实际项目需要一个 播放广告的控件,可能有多个广告图片.每个一段时间更换该图片.简单来说,就是一个 “循环播放图片”的控件. 间隔时间更换图片 一般来说,图片切换时需要有动画效果 需要支持手势,用 ...

  9. JavaScript的控制语句和循环语句和函数的总结

    10.控制语句---if语句 10_1:if-else语句 if(表达式){ 语句1: .... }else{ 语句1: .... }; 示例: var a = 1; if (a > 0){ a ...

随机推荐

  1. 强大的scrollReveal库,炫酷的页面缓入效果。

    首先我问来看一下这个强大的插件能做出什么效果,下面是我找的一个网站: http://kepler.gl/#/, 接下来看看官网给出的效果:https://scrollrevealjs.org/. 是不 ...

  2. python小白——进阶之路——day3天-———运算符

    (1)算数运算符:  + - * / // % ** (2)比较运算符:  > < >= <= == != (3)赋值运算符:  = += -= *= /= //= %= ** ...

  3. xml转对象,对象转xml工具类

    package com.dq.schooldomain.utils; import com.thoughtworks.xstream.XStream; import com.thoughtworks. ...

  4. 使用offsetof对结构体指针偏移操作

    题目来自于COMP20003 Tutorial 2: Program m ing Challenge 2.2 The technology stack at Hidebound Inc. uses a ...

  5. DOTween 相关API效果

    1,首先看一遍完整Tween路径 2,操作 DoPlay->DoRestart,DoRestart是从调用时刻重新开始开始执行Tween 3,操作 DoPlay->DoReWind,DoR ...

  6. OpenStack-Keystone(2)

    一. Keystone 概述 管理用户及其权限 维护OpenStack Services的Endpoint Authentication(认证)和 Authorization(授权) 1.验证用户 验 ...

  7. 【学习总结】GirlsInAI ML-diary day-12-for循环

    [学习总结]GirlsInAI ML-diary 总 原博github链接-day12 认识for循环执行 ps: range()函数 python range() 函数可创建一个整数列表,一般用在 ...

  8. [官网]Using PuTTY

    Previous | Contents | Next Chapter 3: Using PuTTY Section 3.1: During your session Section 3.1.1: Co ...

  9. MyBatis基础:MyBatis关联查询(4)

    1. MyBatis关联查询简介 MyBatis中级联分为3中:association.collection及discriminator. ◊ association:一对一关联 ◊ collecti ...

  10. block,inline,inline-block区别

    block:多個元素豎直排列,每個元素單獨占一行,寬高可以設置,padding.margin可以設置: inline:多個元素占一行,一行放不下了,才轉入下一行,寬高不能設置,水平的padding.m ...