美文网首页
数据团Python_6. 条件判断及循环语句

数据团Python_6. 条件判断及循环语句

作者: 00e6a8fd618f | 来源:发表于2017-04-06 03:18 被阅读5次

    6. 条件判断及循环语句

    6.1 什么是语句

    >>> #赋值语句
    >>> x = 1
    >>> x = [1, 3, 5]
    >>> x = {'a':'b'}
    >>> 
    >>> #运行函数/方法
    >>> print('a')
    a
    >>> str(123)
    '123'
    >>> list('abc')
    ['a', 'b', 'c']
    >>> dict(m = 'a', n = 'b')
    {'n': 'b', 'm': 'a'}
    >>> list(range(10)) #函数运行
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> x = list(range(10))
    >>> x
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> x.copy() #方法
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> x.remove(1)
    >>> x
    [0, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> 
    >>> #if语句
    >>> x = 10
    >>> if x > 5 :
        print('大')
    
        
    大
    >>> 
    >>> #for循环(迭代)
    >>> m = list(range(20))
    >>> m
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
    >>> for i in m:
        print(i)
    
        
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    >>> 
    >>> n = tuple(range(20))
    >>> n
    (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
    >>> for i in n :
        print(i)
    
        
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    >>> b = {'a':1, 'b':2}
    >>> for i in b:
        print(i)
    
        
    a
    b
    >>> for (k,v) in b.items() :
        print('{}-{}'.format(k,v))
    
        
    a-1
    b-2
    >>> 
    >>> #while语句(循环)
    >>> x = 1
    >>> while x < 10:
        print(x)
        x = x + 1
    
        
    1
    2
    3
    4
    5
    6
    7
    8
    9
    >>> 
    >>> #def函数
    >>> def f():
        print('hello world')
    
        
    >>> f()
    hello world
    >>> 
    >>> #模块语句import
    >>> 
    >>> #类class
    >>> 
    >>> x = 1
    >>> y = 2
    >>> z = 3
    >>> print(x + y + z)
    6
    

    6.2 if语句

    x = 12
    if x <18 :
        print('18岁以下不宜观看')
    
    #具有相同缩进的代码将被视为代码块
    
    y = 20
    if y >18 :
        print('hehe')
    
    #input()函数,该函数的输入为字符串
    m = input('m=')
    print(m)
    
    score = input('score=')
    score1 = float(score)
    if score1>=60 :
        print('及格')
    else: #注意缩进
        print('重修')
        
    score = input('score=')
    score1 = float(score)
    if score1 >= 85 :
        print('A')
    elif score1 >= 60: #注意elif
        print('B')
    else:
        print('C')
    
    #作业
    st = input('请输入语句:')
    if eval(st) == True: #eval()执行括号内语句
        print('真')
    else:
        print('假')
    
    n = int(input('请猜数字:'))
    m = 50
    if n == m :
        print('Good!')
    elif n > m :
        print('大了')
    else:
        print('小了')
    

    6.3 for 循环

    #前边的丢了
    #前边的丢了
    #前边的丢了
    k = ['a', 'b', 'c']
    v = [1, 2, 3]
    #[['a':1], ['b':2], ['c':3]]
    m = []
    for i in range(len(k)):
        m.append([k[i],v[i]])
    m2 = dict(m)
    print(m)
    
    #阶层运算
    n = int(input('阶层运算n = '))
    m = 1
    
    for i in range(1, n+1) :
        m = m * i
    print(m)
    

    6.4 while循环

    while True : #死循环
        print('stop , Hit the friendly army !')
    #if while 对比
    n = int(input('n = '))
    
    if n < 18 :
        print('Too tender')
    
    while n <18 :
        print('too tender')
        break #停止运行
    
    #求和0-100
    x = 0
    s = 0
    
    while x < 100 :
         s = s + x
         x = x +1
    
    print(s)
    
    #continue继续下一循环
    lst = list(range(1,6))
    s = 0
    
    for i in lst :
        if i%2 != 0 : #验证为是否为奇数
            s = s + i
        else :
            continue
    
    print(s)
    

    6.5 多重循环

    #for while 相互嵌套, if辅助判断
    #乘法口诀表
    for i in range(1, 10) :
        for j in range(1, 10) :
            m = i * j
            print(str(i) + '*' + str(j) + '=' + str(m))
        print('\n')
    
    st1 = 'abc'
    st2 = '123'
    
    n = len(st1)
    m = []
    
    for i in range(n) :
        for j in range(n) :
            m.append(st1[i] + st2[j])
    
    print(m)
    
    for i in range(n) :
        m.append(st1[i] + st2[i])
    
    print(m)
    
    st = input('随机输入一行字符串:')
    x = input('输入想查找的字母x = ')
    
    m = []
    l = len(st)
    
    for i in range(l) :
        al = st[i]
        if al == x :
            m.append(i)
        else:continue
    
    print(m)
    
    print('输入a或者b')
    
    x = input('a燕麦面板,b普通面包 : ')
    y = input('a要番茄酱,b不要番茄酱:')
    z = input('a打包,b不打包 : ')
    
    if x == 'a'  :
        if y == 'a' :
            if z == 'a' :
                print('燕麦面包+番茄酱+打包')
            else :
                print('燕麦面包+番茄酱+不打包')
            
    
    

    相关文章

      网友评论

          本文标题:数据团Python_6. 条件判断及循环语句

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