zoukankan      html  css  js  c++  java
  • python学习06循环

    '''
    while
    '''
    '''
    while 布尔表达式:冒号不能省略
    '''
    '''
    1+2+3+...+10
    '''
    i=1
    sum1=0
    while i<=10:
    sum1+=i
    i+=1
    print(sum1)
    '''
    python 中的没有 i++ ,如果写了会报语法错误。

    但是python 中有 --i,++i,+-i,-+i,他们不是实现-1操作的,仅仅是作为判断运算符号,类似数学中的负负得正

    i = 2

    print ++i //2

    print -+i //-2

    print +-i //-2

    print --i //2

    python 中没有 && ,!, || 这3个运算符,在逻辑表达式中写成这3个会抱逻辑错误的。要实现同样的功能,要写成 and,not,or

    返回值 2 and 3 返回3

    返回值 2 or 3 返回2

    返回值 not 2 and 3 返回 False
    '''
    #三酷猫钓鱼记录查找,python3.6.3版本下执行
    fish_record='鲫鱼5条、鲤鱼8条、鲢鱼7条、草鱼2条、黑鱼6条、乌龟1只'
    print(len(fish_record))
    record_len=len(fish_record)
    i=0
    while i<record_len:
    if fish_record[i:i+2]=='乌龟':
    if int(fish_record[i+2])/2==0:
    print("找到乌龟了,是%d只,偶数"%(int(fish_record[i+2])))
    else:
    print("找到乌龟了,是%d只,奇数"%(int(fish_record[i+2])))
    i+=5

    '''
    for
    '''
    '''
    for v1 in 范围:


    '''
    s=0
    for i in range(10):
    print(i)
    s+=i
    else:
    print(str(s)+ ' is a consequence')
    '''
    range()是python的内建范围函数;range(9)代表0、1、2、3、4、5、6、7、8这九个数字的集合
    '''
    for i in range(1,5,2):#在0,1,2,3,4中每隔一个数取一个数,2为步长
    print(i)
    '''
    break:跳出所有循环
    continue:跳出本次循环
    '''

    str1='Tom is a student'
    for i in range(len(str1)):
    print('for循环了%d次'%(i+1))
    if str1[i:i+3]=='Tom':
    print('OK')
    #break
    continue
    print('ok2')
  • 相关阅读:
    AcWing 1135. 新年好 图论 枚举
    uva 10196 将军 模拟
    LeetCode 120. 三角形最小路径和 dp
    LeetCode 350. 两个数组的交集 II 哈希
    LeetCode 174. 地下城游戏 dp
    LeetCode 面试题 16.11.. 跳水板 模拟
    LeetCode 112. 路径总和 递归 树的遍历
    AcWing 1129. 热浪 spfa
    Thymeleaf Javascript 取值
    Thymeleaf Javascript 取值
  • 原文地址:https://www.cnblogs.com/wsxcode/p/12198674.html
Copyright © 2011-2022 走看看