zoukankan      html  css  js  c++  java
  • Python之for循环

    1.遍历字符串中每个字符的for循环

    name="Ted"
    for character in name:
    print(character)

    2. for遍历列表元素
    shows=["GOT",
    "You"
    "Know"
    ]
    for show in shows:
    print(show)

    3.for遍历元组中的元素
    coms=(
    "World",
    "always",
    "sunshine"
    )
    for com in coms:
    print(com)
    4.for循环遍历字典元素
    people={
    "Key1":"value1",
    "Key2":"value2",
    "Key3":"value3"
    }
    for peo in people:
    print(peo)
    5.使用for循环修改可变且可迭代对象中的元素
    people={
    "Key1":"value1",
    "Key2":"value2",
    "Key3":"value3"
    }
    for peo in people:
    print(peo)
    由于访问可迭代对象中索引和元素是很常见的操作,python提供了专门的语法
    tv=[
    "GOT","Narcos","Vice"
    ]

    for i,show in enumerate(tv):
    new=tv[i]
    new=new.upper()
    tv[i]=new
    print(tv)

    6.使用两个for循环获取两个不同列表中的所有字符串,然后将每个字符大写,并放入一个新的列表中
    tv=[
    "GOT",
    "Narcos",
    "Vice"
    ]
    coms=[
    "Arrsted Development",
    "friends",
    "Always Sunny"
    ]
    all_shows=[]

    for show in tv:
    show=show.upper()
    all_shows.append(show)

    for show in coms:
    show =show.upper()
    all_shows.append(show)

    print(all_shows)



  • 相关阅读:
    hadoop基础
    数据库基础知识
    sqoop基础
    大数据之常用linux常用命令
    zooKeeper基础
    impala基础
    Hbase基础
    Commitlint 提交规范类型
    理解JS闭包
    JS函数作用域及作用域链理解
  • 原文地址:https://www.cnblogs.com/JacquelineQA/p/13022375.html
Copyright © 2011-2022 走看看