zoukankan      html  css  js  c++  java
  • Python循环

                  循环

     python中的循环控制语句有for语句和while语句,可用来遍历某一对象。其中for语句附带的else块,主要用于处理for语句中包含的break语句

    for循环

    for语句的格式如下:

    for <> in <对象集合>:

    if <条件>:

           break

    else   if <条件>:

           continue

       <其他语句>

    else:

       <>

     1 for looper in [1,2,3,4,5]:                 #循环输出单一语句
     2     print("I Love HaoHao!")
     3     
     4 for looper in [1,2,3,4,5]:                 #循环输出循环次数
     5     print(looper)
     6     
     7 for looper in [1,2,3,4,5]:                 #循环输出与数乘积
     8     print(looper,"Times 8=",looper*8)
     9 
    10 for looper in range(1,5):                  #range()从第一个数据开始,到最后一个数字之前
    11     print (looper,"times 8=",looper*8)
    12     
    13 for looper in range(5):                    #range()简写,只输入上界,默认从零开始
    14     print(looper)
    15 
    16 for letter in "Hi,there":                  #逐个输出字母,print每一次输出完换行
    17         print (letter)
    18 
    19 for i in range(1,10,2):                    #按步长为2循环输出1到10
    20     print(i)    
    21 
    22 for i in range(10,0,-1):                   #按步长为-1,倒序输出10到1
    23     print(i)    
    24 
    25 import time                                #按步长为-1,倒序输出10到1
    26 for i in range(10,0,-1):
    27     print (i)
    28 time.sleep(1)                              #等待一秒
    29 print ("blast off!")

    range()包含第一个数字到最后一个十字之前的整数,默认步长为1;只输入上界默认从0开始。

    Python中要用到时间是要调用time模块,既要有语句“import time”。

     1 print("Multiplication Table")
     2 #Display the number title
     3 print(" ",end=' ')
     4 for j in range(1,10):
     5 print(" ",j,end=' ')
     6 print() #jump to the new line
     7 print("_________________________________________________")
     8 
     9 #Display table body
    10 for i in range(1,10):
    11 print(i,"|",end=' ')
    12 for j in range(1,10):
    13 #Display the product and align properly
    14 print(format(i*j,"4d"),end=' ')
    15 print() #Jump to the new line

    break 在需要时终止for循环,如果for循环未被break终止,则执行else块中的语句。

    continue 跳过位于其后的语句,开始下一轮循环。

    例2:输出星星

     1 numstars=int(input("How many stars do you want?"))              #输出输入数-1颗*
     2 for i in range(1,numstars):
     3     print('*')
     4 
     5 numstars=int(input("How many stars do you want?"))              #输出输入数颗*
     6 for i in range(0,numstars):
     7     print('*')
     8 
     9 numblocks=int(input  ("How many blocks of stars do you want?")) #输出输入数-1颗*
    10 numlines=int(input  ("How many lines in each block?"))   
    11 numstars=int(input  ("How many stars per line?"))   
    12 for blocks in range(0,numblocks):
    13     for lines in range(0,numlines):
    14         for stars in range(0,numstars):
    15             print('*'),
    16         print
    17     print

    注意:for循环的缩进量应该严格控制,否则会产生错误结果。

    while循环

    for语句的格式如下:

    while  <对象集合>:

             <>

    else: 

        <>

    例3:求两个整数的最大公因子(Prompt the user to enter two integers)

     1 n1=eval(input("Enter first integer:"))
     2 n2=eval(input("Enter second integer:"))
     3 
     4 gcd=1
     5 k=2
     6 while k<=n1 and k<=n2:
     7 if n1%k==0 and n2%k==0:
     8 gcd=k
     9 k+=1
    10 
    11 print("The greatest common divisor for",n1,"and",n2,"is",gcd)

    在 python 中,while … else 在循环条件为 false 时执行 else 语句块。

    判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。

    注意:无限循环(死循环)可以使用 CTRL+C 来中断循环。

  • 相关阅读:
    40个Java集合面试问题和答案
    jsp九大内置对象:request、response、session、application、out、page、pageContext、config、exception
    JAVA多线程和并发基础面试问答
    线程的五大状态
    人物与动物快速抠图技巧
    实时监听数据库变化
    inter jion与WITH (NOLOCK) 排他锁(脏读)
    Win定时任务未正常运行的bug(win Server2019)
    HttpWebRequest与HttpWebResponse的关系
    A5M2 sql自动换行快捷键
  • 原文地址:https://www.cnblogs.com/zyh19980816/p/11907789.html
Copyright © 2011-2022 走看看