zoukankan      html  css  js  c++  java
  • Python中的判断、循环 if...else,while

    if...else语句:

    a=3; b=3; 
    if a == b :
    print(a,b)
    elif a <= b :
    print(str(a) + " is less than " + str(b))
    else :
    print(str(a) + " is greater than " + str(b))

    ###################################

    n = 3
    if (n >= 0 and n <= 8) or (n >= 12 and n <= 25): 
    print("hhh")

    ##或者 

    n = 3
    if (n >= 0 and n <= 8) or (n >= 12 and n <= 25): print("hhh")

    一个字符串也相当于是一个数组,通过for循环可以将每个字符输出

    for循环,相当于foreach
    c="cc";d="dd";e="ee";f="ff"
    pa = [c,d,e,f]
    for p in pa:
    print("current p is:",p)

    使用下标索引:
    c="cc";d="dd";e="ee";f="ff"
    pa = [c,d,e,f]
    for index in range(len(pa)):
    print("current p is:",pa[index])

    或者:
    c="cc";d="dd";e="ee";f="ff"
    pa = [c,d,e,f]
    for index in range(2,4):
    print("current p is:",pa[index])


    使用while循环:
    c="cc";d="dd";e="ee";f="ff"
    pa = [c,d,e,f]
    i=0
    while(i<len(pa)):
    print("current p is:",pa[i])
    i=i+1


    while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环

  • 相关阅读:
    github提交用户权限被拒
    vue数据响应式的一些注意点
    总结一下做移动端项目遇到的坑
    react-router
    promise-async-await
    递归函数
    Linux基础
    所有的数据处理都是map-reduce
    Mac下配置JAVA_HOME
    MySQL高级
  • 原文地址:https://www.cnblogs.com/dreamer-fish/p/3818408.html
Copyright © 2011-2022 走看看