• Python



     Python 3  - for 循环

    这次将为大家介绍 Python 3 中的 for 循环语句的使用

    for 循环的一般格式如下:

    for <variable> in <sequence>:

        <statements>

    else:

        <statements>

    表示临时变量,in后面跟着待遍历的数据 Python 3 中 for循环可以遍历任何序列的项目,如一个列表或者一个字符串。

    # 例一:

    for x in 'abc':

        print(x,end='')

    #Python 3 结果:abc

    for y in [1, 2, 3, 'hello']:

        print(y,end=' ')

    #Python 3 结果:1 2 3 python

    for z in (2, 3, 4):

        print(z)

    #Python 3 结果:2 3 4

    dic = {'name':'zhangsan','age':10}

    for k in dic:

        print(k,end='')

    #Python 3 结果:nameage

    #keys() 获取字典中所有的key

    for k in dic.keys():

        print(k,end='')

    #Python 3 结果:nameage

    #values()获取字典中所有的value

    for v in dic.values():

        print(v)

    #Python 3 结果:zhangsan 10

    #items() 得到字典中的所有键值对

    for k, v in dic.items():

        print(k, v)

    #Python 3 结果:name zhangsan age 10

    # 例二

    #range() 函数得到整数序列

    #比如range(10),表示0-9

    for i in range(10):

        print(i,end=' ')

    #Python 3 结果:0 1 2 3 4 5 6 7 8 9

    # 第一个参数指定开始值,第二个参数表示结束值

    #生成的整数序列,不包含第二个参数的值

    for i in range(1, 11):

        print(i,end=' ')

    #Python 3 结果:1 2 3 4 5 6 7 8 9 10

    # 第三个参数表示步进值,不写默认 1

    for i in range(1, 11, 2):

        print(i,end=' ')

    #Python 3 结果:1 3 5 7 9

    #转换为列表类型

    l = list(range(10))

    print(l,end='')

    #Python 3 结果:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    # 例三:

    #for 1-100 的和

    sumV = 0

    for i in range(1, 101):

        sumV += i

    print(sumV)

    #Python 3 结果:5050

    # 例四:

    for i in range(1, 5):

        for j in range(1, 5):

            print('(',end='')

            print(i,end='')

            print(',',end='')

            print(j,end='')

            print(') ',end='')

        #也可写成 print('(',i,',',j,')',end='')

        print('')

    Python 3 结果:

                                    (1,1) (1,2) (1,3) (1,4)

                      (2,1) (2,2) (2,3) (2,4)

                      (3,1) (3,2) (3,3) (3,4)

                      (4,1) (4,2) (4,3) (4,4)

    例五

    输出以下图形

    *

    **

    ***

    ****

    *****

    需要双重循环:

    外循环控制行数

    内循环控制每行打印的内容

    for i in range(1, 6):

        for j in range(1, i + 1):

            print('*', end='')

        print('')

    例六 以上图形的另一种方法:

    for i in range(1, 6):

        print('*'*i)

    break 语句可以跳出 for 的循环体。

    如果你从 for 循环中终止,任何对应的循环 else 块将不执行。

    for letter in 'python':  # 第一个实例

        if letter == 'o':

            break

        print('当前字母为 :', letter)

    Python 3 结果:

    当前字母为 : p

    当前字母为 : y

    当前字母为 : t

    当前字母为 : h

    var = 10  # 第二个实例

    while var > 0:

        print('当期变量值为 :', var)

        var = var - 1

        if var == 5:

            break

    Python 3 结果:

    当期变量值为 : 10

    当期变量值为 : 9

    当期变量值为 : 8

    当期变量值为 : 7

    当期变量值为 : 6

    continue语句被用来告诉Python跳过当前循环块中的剩余语句,

    然后继续进行下一轮循环

    for letter in 'python':  # 第一个实例

        if letter == 't':  # 字母为 t 时跳过输出

            continue

        print('当前字母 :', letter)

    Python 3 结果:

    当前字母 : p

    当前字母 : y

    当前字母 : h

    当前字母 : o

    当前字母 : n

    循环语句可以有 else 子句,它在穷尽列表(for循环)

    条件变为 false (while循环)导致循环终止时被执行,但循环被break终止时不执行

    # 例:

    for n in range(2, 8):

        for x in range(2, n):

            if n % x == 0:

                print(n, '等于', x, '*', n//x)

                break

        else:

            print(n, ' 是质数')

    Python 3 结果:

    2  是质数

    3  是质数

    4 等于 2 * 2

    5  是质数

    6 等于 2 * 3

    7  是质数

    Python pass是空语句,是为了保持程序结构的完整性。
    pass 不做任何事情,一般用做占位语句.

    # 例:

    for letter in 'Python':

        if letter == 't':

            pass

        print('执行 pass ')

        print('当前字母 :', letter)

    Python 3 结果:

    当前字母 : P

    当前字母 : y

    执行 pass

    当前字母 : t

    当前字母 : h

    当前字母 : o

    当前字母 : n

    欢迎关注小婷儿的博客:https://blog.csdn.net/u010986753

    有问题请在博客下留言或加QQ群:483766429 或联系作者本人 QQ 87605025

    OCP培训说明连接:https://mp.weixin.qq.com/s/2cymJ4xiBPtTaHu16HkiuA

    OCM培训说明连接:https://mp.weixin.qq.com/s/7-R6Cz8RcJKduVv6YlAxJA

    小婷儿的python正在成长中,其中还有很多不足之处,随着学习和工作的深入,会对以往的博客内容逐步改进和完善哒。

    小婷儿的python正在成长中,其中还有很多不足之处,随着学习和工作的深入,会对以往的博客内容逐步改进和完善哒。

    小婷儿的python正在成长中,其中还有很多不足之处,随着学习和工作的深入,会对以往的博客内容逐步改进和完善哒。

    重要的事说三遍。。。。。。


    欢迎关注小婷儿的博客:
        文章内容来源于小婷儿的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解     有趣的事,Python永远不会缺席!
        如需转发,请注明出处:小婷儿的博客python    https://www.cnblogs.com/xxtalhr/
        博客园:https://www.cnblogs.com/xxtalhr/
        CSDN:https://blog.csdn.net/u010986753
    有问题请在博客下留言或加作者:
         微信:tinghai87605025
         QQ :87605025
         python QQ交流群:py_data 483766429

    培训说明:
         OCP培训说明连接:https://mp.weixin.qq.com/s/2cymJ4xiBPtTaHu16HkiuA
         OCM培训说明连接:https://mp.weixin.qq.com/s/7-R6Cz8RcJKduVv6YlAxJA
         小婷儿的python正在成长中,其中还有很多不足之处,随着学习和工作的深入,会对以往的博客内容逐步改进和完善哒。重要的事多说几遍。。。。。。
  • 相关阅读:
    eclipse编写js代码没有提示
    eclipse安装阿里编码规约插件
    eclipse汉化
    异常: Recieved SHUTDOWN signal from Resourcemanager ,Registration of NodeManager failed, Message from ResourceManager: NodeManager from localhost doesn't satisfy minimum allocations, Sending SHUTDOWN s
    异常: Call From * 9000 failed on connection exception: java.net.ConnectException: Connection refused: no further information; For more details see: http://wiki.apache.org/hadoop/ConnectionRefused
    异常:android.os.NetworkOnMainThreadException
    异常:getHibernateFlushMode is not valid without active transaction; nested exception is org.hibernate.HibernateException: getHibernateFlushMode is not valid without active transaction getHibernateFlu
    异常: Bean named 'org.springframework.transaction.interceptor.TransactionInterceptor#0' is expected to be of type 'org.aopalliance.aop.Advice' but was actually of type 'org.springframework.transaction.i
    JVM GC算法
    设计模式-抽象工厂模式
  • 原文地址:https://www.cnblogs.com/pythonbao/p/9037406.html
Copyright © 2020-2023  润新知