zoukankan      html  css  js  c++  java
  • 流程控制--for序列

    In [2]: list1 = [1,2,3,4]
    
    In [3]: for i in list1:
       ...:     print i
       ...:
    1
    2
    3
    4
    
    In [4]: for i in list1:
        print i,
       ...:
    1 2 3 4
    //range(头,尾,走多少步取值)
    In [7]: range(0,10)
    Out[7]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    In [8]: range(0,10,1)
    Out[8]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    In [9]: range(0,10,2)
    Out[9]: [0, 2, 4, 6, 8]
    
    In [10]: range(0,10,3)
    Out[10]: [0, 3, 6, 9]
    
    In [11]: range(0,10,4)
    Out[11]: [0, 4, 8]
    [root@localhost test1]# vim 8.py
    //ADD
    #!/usr/bin/python
    
    for i in [i**2 for i in range(1,11) if i % 2 != 0]:
        print i,
    
    [root@localhost test1]# python 8.py
    1 9 25 49 81
    
    ==================================
    [root@localhost test1]# vim 9.py
    //add
    #!/usr/bin/python
    
    sum = 0
    for i in range(1,101):
        sum += i
    
    print sum
    
    [root@localhost test1]# python 9.py
    5050
    /* xrange() 可以生成迭代的对象 ,
    
        但是如果直接执行时, 不会显示任何。
    
        需要在for循环语句中使用,才能发挥作用。
    */
    In [1]: for i in xrange(10):
       ...:     print i
       ...:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    In [2]: for i in xrange(10):
        print i,
       ...:
    0 1 2 3 4 5 6 7 8 9
    
    In [3]: xrange(10)
    Out[3]: xrange(10)
    //两种方式新建字典
    In [4]: dic = {'a':1, 'b':2}
    
    In [5]: dic1 = dic.fromkeys('abc',10)
    
    In [6]: dic
    Out[6]: {'a': 1, 'b': 2}
    
    In [7]: dic1
    Out[7]: {'a': 10, 'b': 10, 'c': 10}
    
    In [8]: for k in dic:
       ...:     print k
       ...:
    a
    b
    
    //取keys
    In [9]: for k in dic1:
        print k
       ...:
    a
    c
    b
    
    //并取values
    In [11]: for k in dic1:
        print k, dic1[k]
       ....:
    a 10
    c 10
    b 10
    
    //格式化的输出
    In [12]: for k in dic1:
        print "%s --> %s" % (k, dic1[k])
       ....:
    a --> 10
    c --> 10
    b --> 10
    //两种的区别
    In [13]: for i in dic1.items():print i
    ('a', 10)
    ('c', 10)
    ('b', 10)
    
    In [14]: for k, v in dic1.iteritems():print k, v
    a 10
    c 10
    b 10
    /* 利用循环做 “九九乘法表” */
    [root@localhost test1]# vim 10.py
    //ADD
    #!/usr/bin/python
    
    for i in xrange(1,10):
        for j in xrange(1, i+1):
            print "%sx%s=%s" % (j, i, j*i),
        print
    
    /* 这里利用 逗号 来进行分行,并且打印出来 */
    
    [root@localhost test1]# python 10.py
    1x1=1
    1x2=2 2x2=4
    1x3=3 2x3=6 3x3=9
    1x4=4 2x4=8 3x4=12 4x4=16
    1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
    1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
    1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
    1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56
    1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63
  • 相关阅读:
    iOS cannot find folder xcdatamodeld Xcode 7
    ios swift generator 文章推荐
    ios swift 2 新的OptionSetType使用方法
    Maven 教程(11)— Maven远程仓库的各种配置
    Maven 教程(10)— Maven依赖详解
    Maven 教程(9)— Maven坐标详解
    Maven设置MAVEN_OPTS环境变量
    Maven 教程(7)— Maven使用的最佳实践
    Maven 教程(6)— Maven之pom.xml文件简单说明
    Maven 教程(5)— Maven目录结构及常用命令说明
  • 原文地址:https://www.cnblogs.com/frankielf0921/p/5841096.html
Copyright © 2011-2022 走看看